refactoring

This commit is contained in:
2026-01-19 16:55:33 +01:00
parent 197a01440c
commit 64750ef1c2
5 changed files with 83 additions and 63 deletions

View File

@@ -1,44 +1,52 @@
package lib package lib
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings"
) )
type GiteaFetcher struct { type GiteaFetcher struct {
URL string BaseURL string
Token string Token string
Repos []string
Cache *Cache Cache *Cache
} }
func (f GiteaFetcher) Fetch() string { func (f GiteaFetcher) Fetch() []string {
req, _ := http.NewRequest("GET", f.URL, nil) var messages []string
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 { for _, repo := range f.Repos {
Sha string `json:"sha"` if repo == "" {
Commit struct { continue
Message string `json:"message"` }
} `json:"commit"` giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", f.BaseURL, strings.TrimSpace(repo))
HTMLURL string `json:"html_url"` req, _ := http.NewRequest("GET", giteaURL, nil)
} if f.Token != "" {
json.NewDecoder(res.Body).Decode(&r) req.Header.Set("Authorization", "token "+f.Token)
if len(r) == 0 { }
return ""
}
c := r[0]
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(f.URL), c.Sha) { var r []struct {
return fmt.Sprintf("Gitea: %s <%s>", c.Commit.Message, c.HTMLURL) Sha string `json:"sha"`
Commit struct {
Message string `json:"message"`
} `json:"commit"`
HTMLURL string `json:"html_url"`
}
if err := getJSON(req, &r); err != nil {
fmt.Printf("Error fetching Gitea repo %s: %v\n", repo, err)
continue
}
if len(r) == 0 {
continue
}
c := r[0]
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
View 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)
}

View File

@@ -1,7 +1,6 @@
package lib package lib
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
) )
@@ -11,15 +10,10 @@ type RedmineFetcher struct {
Cache *Cache Cache *Cache
} }
func (f RedmineFetcher) Fetch() string { func (f RedmineFetcher) Fetch() []string {
redmineURL := fmt.Sprintf("%s/issues.json", f.Config.RedmineBaseURL) redmineURL := fmt.Sprintf("%s/issues.json", f.Config.RedmineBaseURL)
req, _ := http.NewRequest("GET", redmineURL, nil) req, _ := http.NewRequest("GET", redmineURL, nil)
req.Header.Set("X-Redmine-API-Key", f.Config.RedmineKey) 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 { var r struct {
Issues []struct { Issues []struct {
@@ -28,16 +22,18 @@ func (f RedmineFetcher) Fetch() string {
Subject string Subject string
} }
} }
json.NewDecoder(res.Body).Decode(&r) if err := getJSON(req, &r); err != nil {
return []string{}
}
if len(r.Issues) == 0 { if len(r.Issues) == 0 {
return "" return []string{}
} }
i := r.Issues[0] i := r.Issues[0]
if f.Cache.TryUpdate("redmine", i.UpdatedOn) { if f.Cache.TryUpdate("redmine", i.UpdatedOn) {
url := fmt.Sprintf("%s/issues/%d", f.Config.RedmineBaseURL, i.ID) 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{}
} }

View File

@@ -2,7 +2,6 @@ package lib
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
) )
@@ -12,17 +11,12 @@ type WikiFetcher struct {
Cache *Cache Cache *Cache
} }
func (f WikiFetcher) Fetch() string { func (f WikiFetcher) Fetch() []string {
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ updatedAt title }}}"}` q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
wikiURL := fmt.Sprintf("%s/graphql", f.Config.WikiBaseURL) wikiURL := fmt.Sprintf("%s/graphql", f.Config.WikiBaseURL)
req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q))) req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q)))
req.Header.Set("Authorization", "Bearer "+f.Config.WikiToken) req.Header.Set("Authorization", "Bearer "+f.Config.WikiToken)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return ""
}
defer res.Body.Close()
var r struct { var r struct {
Data 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 { if len(r.Data.Pages.List) == 0 {
return "" return []string{}
} }
u := r.Data.Pages.List[0] u := r.Data.Pages.List[0]
if f.Cache.TryUpdate("wiki", u.UpdatedAt) { if f.Cache.TryUpdate("wiki", u.UpdatedAt) {
url := fmt.Sprintf("%s/%s", f.Config.WikiBaseURL, u.Path) 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{}
} }

View File

@@ -11,10 +11,6 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
type Fetcher interface {
Fetch() string
}
func Runner() { func Runner() {
if err := godotenv.Load(); err != nil { if err := godotenv.Load(); err != nil {
log.Println("Warning: .env file not found, using environment variables") log.Println("Warning: .env file not found, using environment variables")
@@ -42,13 +38,12 @@ func Runner() {
discord := DiscordSender{Config: config} discord := DiscordSender{Config: config}
var fetchers []Fetcher var fetchers []Fetcher
for _, repo := range config.GiteaRepos { fetchers = append(fetchers, &GiteaFetcher{
if repo == "" { BaseURL: config.GiteaBaseURL,
continue Token: config.GiteaToken,
} Repos: config.GiteaRepos,
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", config.GiteaBaseURL, strings.TrimSpace(repo)) Cache: &cache,
fetchers = append(fetchers, &GiteaFetcher{URL: giteaURL, Token: config.GiteaToken, Cache: &cache}) })
}
fetchers = append(fetchers, &WikiFetcher{Config: config, Cache: &cache}) fetchers = append(fetchers, &WikiFetcher{Config: config, Cache: &cache})
fetchers = append(fetchers, &RedmineFetcher{Config: config, Cache: &cache}) fetchers = append(fetchers, &RedmineFetcher{Config: config, Cache: &cache})
@@ -57,8 +52,11 @@ func Runner() {
fmt.Println("Update") fmt.Println("Update")
for _, f := range fetchers { for _, f := range fetchers {
if msg := f.Fetch(); msg != "" { if msg := f.Fetch(); len(msg) > 0 {
discord.Send(msg) for _, m := range msg {
fmt.Println("Sending:", m)
discord.Send(m)
}
} }
} }