refactoring
This commit is contained in:
@@ -1,28 +1,31 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GiteaFetcher struct {
|
||||
URL string
|
||||
BaseURL string
|
||||
Token string
|
||||
Repos []string
|
||||
Cache *Cache
|
||||
}
|
||||
|
||||
func (f GiteaFetcher) Fetch() string {
|
||||
req, _ := http.NewRequest("GET", f.URL, nil)
|
||||
func (f GiteaFetcher) Fetch() []string {
|
||||
var messages []string
|
||||
|
||||
for _, repo := range f.Repos {
|
||||
if repo == "" {
|
||||
continue
|
||||
}
|
||||
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", f.BaseURL, strings.TrimSpace(repo))
|
||||
req, _ := http.NewRequest("GET", giteaURL, nil)
|
||||
if f.Token != "" {
|
||||
req.Header.Set("Authorization", "token "+f.Token)
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var r []struct {
|
||||
Sha string `json:"sha"`
|
||||
@@ -31,14 +34,19 @@ func (f GiteaFetcher) Fetch() string {
|
||||
} `json:"commit"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
}
|
||||
json.NewDecoder(res.Body).Decode(&r)
|
||||
if err := getJSON(req, &r); err != nil {
|
||||
fmt.Printf("Error fetching Gitea repo %s: %v\n", repo, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(r) == 0 {
|
||||
return ""
|
||||
continue
|
||||
}
|
||||
c := r[0]
|
||||
|
||||
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(f.URL), c.Sha) {
|
||||
return fmt.Sprintf("Gitea: %s <%s>", c.Commit.Message, c.HTMLURL)
|
||||
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(giteaURL), c.Sha) {
|
||||
messages = append(messages, fmt.Sprintf("Gitea: %s <%s>", c.Commit.Message, c.HTMLURL))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
21
lib/fetcher.go
Normal file
21
lib/fetcher.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Fetcher is the interface for a fetcher.
|
||||
type Fetcher interface {
|
||||
Fetch() []string
|
||||
}
|
||||
|
||||
func getJSON(req *http.Request, target interface{}) error {
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
return json.NewDecoder(res.Body).Decode(target)
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
@@ -11,15 +10,10 @@ type RedmineFetcher struct {
|
||||
Cache *Cache
|
||||
}
|
||||
|
||||
func (f RedmineFetcher) Fetch() string {
|
||||
func (f RedmineFetcher) Fetch() []string {
|
||||
redmineURL := fmt.Sprintf("%s/issues.json", f.Config.RedmineBaseURL)
|
||||
req, _ := http.NewRequest("GET", redmineURL, nil)
|
||||
req.Header.Set("X-Redmine-API-Key", f.Config.RedmineKey)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var r struct {
|
||||
Issues []struct {
|
||||
@@ -28,16 +22,18 @@ func (f RedmineFetcher) Fetch() string {
|
||||
Subject string
|
||||
}
|
||||
}
|
||||
json.NewDecoder(res.Body).Decode(&r)
|
||||
if err := getJSON(req, &r); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if len(r.Issues) == 0 {
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
|
||||
i := r.Issues[0]
|
||||
if f.Cache.TryUpdate("redmine", i.UpdatedOn) {
|
||||
url := fmt.Sprintf("%s/issues/%d", f.Config.RedmineBaseURL, i.ID)
|
||||
return fmt.Sprintf("Redmine: #%d %s <%s>", i.ID, i.Subject, url)
|
||||
return []string{fmt.Sprintf("Redmine: #%d %s <%s>", i.ID, i.Subject, url)}
|
||||
}
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
@@ -12,17 +11,12 @@ type WikiFetcher struct {
|
||||
Cache *Cache
|
||||
}
|
||||
|
||||
func (f WikiFetcher) Fetch() string {
|
||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ updatedAt title }}}"}`
|
||||
func (f WikiFetcher) Fetch() []string {
|
||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
||||
wikiURL := fmt.Sprintf("%s/graphql", f.Config.WikiBaseURL)
|
||||
req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q)))
|
||||
req.Header.Set("Authorization", "Bearer "+f.Config.WikiToken)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var r struct {
|
||||
Data struct {
|
||||
@@ -35,14 +29,17 @@ func (f WikiFetcher) Fetch() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
json.NewDecoder(res.Body).Decode(&r)
|
||||
if err := getJSON(req, &r); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if len(r.Data.Pages.List) == 0 {
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
u := r.Data.Pages.List[0]
|
||||
if f.Cache.TryUpdate("wiki", u.UpdatedAt) {
|
||||
url := fmt.Sprintf("%s/%s", f.Config.WikiBaseURL, u.Path)
|
||||
return fmt.Sprintf("Wiki: %s <%s>", u.Title, url)
|
||||
return []string{fmt.Sprintf("Wiki: %s <%s>", u.Title, url)}
|
||||
}
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,6 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Fetcher interface {
|
||||
Fetch() string
|
||||
}
|
||||
|
||||
func Runner() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("Warning: .env file not found, using environment variables")
|
||||
@@ -42,13 +38,12 @@ func Runner() {
|
||||
discord := DiscordSender{Config: config}
|
||||
|
||||
var fetchers []Fetcher
|
||||
for _, repo := range config.GiteaRepos {
|
||||
if repo == "" {
|
||||
continue
|
||||
}
|
||||
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", config.GiteaBaseURL, strings.TrimSpace(repo))
|
||||
fetchers = append(fetchers, &GiteaFetcher{URL: giteaURL, Token: config.GiteaToken, Cache: &cache})
|
||||
}
|
||||
fetchers = append(fetchers, &GiteaFetcher{
|
||||
BaseURL: config.GiteaBaseURL,
|
||||
Token: config.GiteaToken,
|
||||
Repos: config.GiteaRepos,
|
||||
Cache: &cache,
|
||||
})
|
||||
|
||||
fetchers = append(fetchers, &WikiFetcher{Config: config, Cache: &cache})
|
||||
fetchers = append(fetchers, &RedmineFetcher{Config: config, Cache: &cache})
|
||||
@@ -57,8 +52,11 @@ func Runner() {
|
||||
fmt.Println("Update")
|
||||
|
||||
for _, f := range fetchers {
|
||||
if msg := f.Fetch(); msg != "" {
|
||||
discord.Send(msg)
|
||||
if msg := f.Fetch(); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
fmt.Println("Sending:", m)
|
||||
discord.Send(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user