refactoring
This commit is contained in:
@@ -1,28 +1,31 @@
|
|||||||
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
|
||||||
|
|
||||||
|
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 != "" {
|
if f.Token != "" {
|
||||||
req.Header.Set("Authorization", "token "+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 {
|
var r []struct {
|
||||||
Sha string `json:"sha"`
|
Sha string `json:"sha"`
|
||||||
@@ -31,14 +34,19 @@ func (f GiteaFetcher) Fetch() string {
|
|||||||
} `json:"commit"`
|
} `json:"commit"`
|
||||||
HTMLURL string `json:"html_url"`
|
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 {
|
if len(r) == 0 {
|
||||||
return ""
|
continue
|
||||||
}
|
}
|
||||||
c := r[0]
|
c := r[0]
|
||||||
|
|
||||||
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(f.URL), c.Sha) {
|
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(giteaURL), c.Sha) {
|
||||||
return fmt.Sprintf("Gitea: %s <%s>", c.Commit.Message, c.HTMLURL)
|
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
|
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{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user