FetcherRequest
This commit is contained in:
@@ -29,15 +29,25 @@ func (f GiteaFetcher) Fetch() []Entry {
|
|||||||
if repo == "" {
|
if repo == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", f.BaseURL, strings.TrimSpace(repo))
|
|
||||||
req, _ := http.NewRequest("GET", giteaURL, nil)
|
repo = strings.TrimSpace(repo)
|
||||||
|
path := fmt.Sprintf("/api/v1/repos/%s/commits", repo)
|
||||||
|
|
||||||
|
headers := map[string]string{}
|
||||||
if f.Token != "" {
|
if f.Token != "" {
|
||||||
req.Header.Set("Authorization", "token "+f.Token)
|
headers["Authorization"] = "token " + f.Token
|
||||||
|
}
|
||||||
|
|
||||||
|
req := FetcherRequest{
|
||||||
|
BaseURL: f.BaseURL,
|
||||||
|
Path: path,
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Headers: headers,
|
||||||
}
|
}
|
||||||
|
|
||||||
var r GiteaResponse
|
var r GiteaResponse
|
||||||
|
|
||||||
if err := getJSON(req, &r); err != nil {
|
if err := req.Run(&r); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,8 +56,9 @@ func (f GiteaFetcher) Fetch() []Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commit := r[0]
|
commit := r[0]
|
||||||
|
cacheKey := "gitea_" + url.QueryEscape(f.BaseURL+path)
|
||||||
|
|
||||||
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(giteaURL), commit.Sha) {
|
if f.Cache.TryUpdate(cacheKey, commit.Sha) {
|
||||||
messages = append(messages, Entry{
|
messages = append(messages, Entry{
|
||||||
Title: fmt.Sprintf("📝 [Gitea] - (%s) %s", repo, commit.Commit.Message),
|
Title: fmt.Sprintf("📝 [Gitea] - (%s) %s", repo, commit.Commit.Message),
|
||||||
URL: commit.HTMLURL,
|
URL: commit.HTMLURL,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +18,33 @@ type Fetcher interface {
|
|||||||
Fetch() []Entry
|
Fetch() []Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func getJSON(req *http.Request, target interface{}) error {
|
// FetcherRequest represents a common HTTP request configuration.
|
||||||
|
type FetcherRequest struct {
|
||||||
|
BaseURL string
|
||||||
|
Path string
|
||||||
|
Method string
|
||||||
|
Headers map[string]string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the HTTP request and decodes the JSON response into target.
|
||||||
|
func (r FetcherRequest) Run(target interface{}) error {
|
||||||
|
url := r.BaseURL + r.Path
|
||||||
|
|
||||||
|
var body io.Reader
|
||||||
|
if r.Body != nil {
|
||||||
|
body = bytes.NewBuffer(r.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(r.Method, url, body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range r.Headers {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -20,13 +20,18 @@ type RedmineFetcher struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f RedmineFetcher) Fetch() []Entry {
|
func (f RedmineFetcher) Fetch() []Entry {
|
||||||
redmineURL := fmt.Sprintf("%s/issues.json", f.BaseURL)
|
req := FetcherRequest{
|
||||||
req, _ := http.NewRequest("GET", redmineURL, nil)
|
BaseURL: f.BaseURL,
|
||||||
req.Header.Set("X-Redmine-API-Key", f.Key)
|
Path: "/issues.json?limit=1&sort=updated_on:desc",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Headers: map[string]string{
|
||||||
|
"X-Redmine-API-Key": f.Key,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var r RedmineResponse
|
var r RedmineResponse
|
||||||
|
|
||||||
if err := getJSON(req, &r); err != nil {
|
if err := req.Run(&r); err != nil {
|
||||||
return []Entry{}
|
return []Entry{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@@ -26,14 +25,21 @@ type WikiFetcher struct {
|
|||||||
|
|
||||||
func (f WikiFetcher) Fetch() []Entry {
|
func (f WikiFetcher) Fetch() []Entry {
|
||||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
||||||
wikiURL := fmt.Sprintf("%s/graphql", f.BaseURL)
|
|
||||||
req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q)))
|
req := FetcherRequest{
|
||||||
req.Header.Set("Authorization", "Bearer "+f.Token)
|
BaseURL: f.BaseURL,
|
||||||
req.Header.Set("Content-Type", "application/json")
|
Path: "/graphql",
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Authorization": "Bearer " + f.Token,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
Body: []byte(q),
|
||||||
|
}
|
||||||
|
|
||||||
var r WikiResponse
|
var r WikiResponse
|
||||||
|
|
||||||
if err := getJSON(req, &r); err != nil {
|
if err := req.Run(&r); err != nil {
|
||||||
return []Entry{}
|
return []Entry{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user