fetcher refact
This commit is contained in:
@@ -16,22 +16,24 @@ type GiteaResponse []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GiteaFetcher struct {
|
type GiteaFetcher struct {
|
||||||
BaseURL string
|
BaseFetcher
|
||||||
Token string
|
|
||||||
Repos []string
|
Repos []string
|
||||||
Cache *Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f GiteaFetcher) Fetch() []Entry {
|
func (f *GiteaFetcher) Name() string {
|
||||||
var messages []Entry
|
return "Gitea"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *GiteaFetcher) Fetch() []Entry {
|
||||||
|
var entries []Entry
|
||||||
|
|
||||||
for _, repo := range f.Repos {
|
for _, repo := range f.Repos {
|
||||||
|
repo = strings.TrimSpace(repo)
|
||||||
if repo == "" {
|
if repo == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
repo = strings.TrimSpace(repo)
|
path := fmt.Sprintf("/api/v1/repos/%s/commits?limit=1", repo)
|
||||||
path := fmt.Sprintf("/api/v1/repos/%s/commits", repo)
|
|
||||||
|
|
||||||
headers := map[string]string{}
|
headers := map[string]string{}
|
||||||
if f.Token != "" {
|
if f.Token != "" {
|
||||||
@@ -45,25 +47,29 @@ func (f GiteaFetcher) Fetch() []Entry {
|
|||||||
Headers: headers,
|
Headers: headers,
|
||||||
}
|
}
|
||||||
|
|
||||||
var r GiteaResponse
|
var resp GiteaResponse
|
||||||
|
if err := req.Run(&resp); err != nil {
|
||||||
if err := req.Run(&r); err != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r) == 0 {
|
if len(resp) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
commit := r[0]
|
commit := resp[0]
|
||||||
cacheKey := "gitea_" + url.QueryEscape(f.BaseURL+path)
|
cacheKey := "gitea_" + url.QueryEscape(f.BaseURL+path)
|
||||||
|
|
||||||
if f.Cache.TryUpdate(cacheKey, commit.Sha) {
|
entry := f.TryCreateEntry(
|
||||||
messages = append(messages, Entry{
|
cacheKey,
|
||||||
Title: fmt.Sprintf("📝 [Gitea] - (%s) %s", repo, commit.Commit.Message),
|
commit.Sha,
|
||||||
URL: commit.HTMLURL,
|
fmt.Sprintf("📝 [%s] - (%s) %s", f.Name(), repo, commit.Commit.Message),
|
||||||
})
|
commit.HTMLURL,
|
||||||
|
)
|
||||||
|
|
||||||
|
if entry != nil {
|
||||||
|
entries = append(entries, *entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return messages
|
|
||||||
|
return entries
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,25 @@ type Entry struct {
|
|||||||
|
|
||||||
// Fetcher is the interface for a fetcher.
|
// Fetcher is the interface for a fetcher.
|
||||||
type Fetcher interface {
|
type Fetcher interface {
|
||||||
|
Name() string
|
||||||
Fetch() []Entry
|
Fetch() []Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BaseFetcher contains common fields for all fetchers.
|
||||||
|
type BaseFetcher struct {
|
||||||
|
BaseURL string
|
||||||
|
Token string
|
||||||
|
Cache *Cache
|
||||||
|
}
|
||||||
|
|
||||||
|
// TryCreateEntry checks the cache and creates an Entry if the value has changed.
|
||||||
|
func (b *BaseFetcher) TryCreateEntry(cacheKey, cacheValue, title, url string) *Entry {
|
||||||
|
if b.Cache.TryUpdate(cacheKey, cacheValue) {
|
||||||
|
return &Entry{Title: title, URL: url}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FetcherRequest represents a common HTTP request configuration.
|
// FetcherRequest represents a common HTTP request configuration.
|
||||||
type FetcherRequest struct {
|
type FetcherRequest struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
|
|||||||
@@ -14,38 +14,42 @@ type RedmineResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RedmineFetcher struct {
|
type RedmineFetcher struct {
|
||||||
BaseURL string
|
BaseFetcher
|
||||||
Key string
|
|
||||||
Cache *Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f RedmineFetcher) Fetch() []Entry {
|
func (f *RedmineFetcher) Name() string {
|
||||||
|
return "Redmine"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *RedmineFetcher) Fetch() []Entry {
|
||||||
req := FetcherRequest{
|
req := FetcherRequest{
|
||||||
BaseURL: f.BaseURL,
|
BaseURL: f.BaseURL,
|
||||||
Path: "/issues.json?limit=1&sort=updated_on:desc",
|
Path: "/issues.json?limit=1&sort=updated_on:desc",
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"X-Redmine-API-Key": f.Key,
|
"X-Redmine-API-Key": f.Token,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var r RedmineResponse
|
var resp RedmineResponse
|
||||||
|
if err := req.Run(&resp); err != nil {
|
||||||
if err := req.Run(&r); err != nil {
|
return nil
|
||||||
return []Entry{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.Issues) == 0 {
|
if len(resp.Issues) == 0 {
|
||||||
return []Entry{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
i := r.Issues[0]
|
issue := resp.Issues[0]
|
||||||
if f.Cache.TryUpdate("redmine", i.UpdatedOn) {
|
entry := f.TryCreateEntry(
|
||||||
url := fmt.Sprintf("%s/issues/%d", f.BaseURL, i.ID)
|
"redmine",
|
||||||
return []Entry{{
|
issue.UpdatedOn,
|
||||||
Title: fmt.Sprintf("🎫 [Redmine] - #%d %s", i.ID, i.Subject),
|
fmt.Sprintf("🎫 [%s] - #%d %s", f.Name(), issue.ID, issue.Subject),
|
||||||
URL: url,
|
fmt.Sprintf("%s/issues/%d", f.BaseURL, issue.ID),
|
||||||
}}
|
)
|
||||||
|
|
||||||
|
if entry != nil {
|
||||||
|
return []Entry{*entry}
|
||||||
}
|
}
|
||||||
return []Entry{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,15 @@ type WikiResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WikiFetcher struct {
|
type WikiFetcher struct {
|
||||||
BaseURL string
|
BaseFetcher
|
||||||
Token string
|
|
||||||
Cache *Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f WikiFetcher) Fetch() []Entry {
|
func (f *WikiFetcher) Name() string {
|
||||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
return "WikiJS"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *WikiFetcher) Fetch() []Entry {
|
||||||
|
query := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
||||||
|
|
||||||
req := FetcherRequest{
|
req := FetcherRequest{
|
||||||
BaseURL: f.BaseURL,
|
BaseURL: f.BaseURL,
|
||||||
@@ -34,25 +36,28 @@ func (f WikiFetcher) Fetch() []Entry {
|
|||||||
"Authorization": "Bearer " + f.Token,
|
"Authorization": "Bearer " + f.Token,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
Body: []byte(q),
|
Body: []byte(query),
|
||||||
}
|
}
|
||||||
|
|
||||||
var r WikiResponse
|
var resp WikiResponse
|
||||||
|
if err := req.Run(&resp); err != nil {
|
||||||
if err := req.Run(&r); err != nil {
|
return nil
|
||||||
return []Entry{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.Data.Pages.List) == 0 {
|
if len(resp.Data.Pages.List) == 0 {
|
||||||
return []Entry{}
|
return nil
|
||||||
}
|
}
|
||||||
u := r.Data.Pages.List[0]
|
|
||||||
if f.Cache.TryUpdate("wiki", u.UpdatedAt) {
|
page := resp.Data.Pages.List[0]
|
||||||
url := fmt.Sprintf("%s/%s", f.BaseURL, u.Path)
|
entry := f.TryCreateEntry(
|
||||||
return []Entry{{
|
"wiki",
|
||||||
Title: fmt.Sprintf("📖 [WikiJS] - %s", u.Title),
|
page.UpdatedAt,
|
||||||
URL: url,
|
fmt.Sprintf("📖 [%s] - %s", f.Name(), page.Title),
|
||||||
}}
|
fmt.Sprintf("%s/%s", f.BaseURL, page.Path),
|
||||||
|
)
|
||||||
|
|
||||||
|
if entry != nil {
|
||||||
|
return []Entry{*entry}
|
||||||
}
|
}
|
||||||
return []Entry{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,27 +32,30 @@ func getConfig() Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getFetchers(config *Config, cache *Cache) []Fetcher {
|
func getFetchers(config *Config, cache *Cache) []Fetcher {
|
||||||
var fetchers []Fetcher
|
return []Fetcher{
|
||||||
|
&GiteaFetcher{
|
||||||
fetchers = append(fetchers, &GiteaFetcher{
|
BaseFetcher: BaseFetcher{
|
||||||
BaseURL: config.GiteaBaseURL,
|
BaseURL: config.GiteaBaseURL,
|
||||||
Token: config.GiteaToken,
|
Token: config.GiteaToken,
|
||||||
Repos: config.GiteaRepos,
|
|
||||||
Cache: cache,
|
Cache: cache,
|
||||||
})
|
},
|
||||||
|
Repos: config.GiteaRepos,
|
||||||
fetchers = append(fetchers, &WikiFetcher{
|
},
|
||||||
|
&WikiFetcher{
|
||||||
|
BaseFetcher: BaseFetcher{
|
||||||
BaseURL: config.WikiBaseURL,
|
BaseURL: config.WikiBaseURL,
|
||||||
Token: config.WikiToken,
|
Token: config.WikiToken,
|
||||||
Cache: cache,
|
Cache: cache,
|
||||||
})
|
},
|
||||||
fetchers = append(fetchers, &RedmineFetcher{
|
},
|
||||||
|
&RedmineFetcher{
|
||||||
|
BaseFetcher: BaseFetcher{
|
||||||
BaseURL: config.RedmineBaseURL,
|
BaseURL: config.RedmineBaseURL,
|
||||||
Key: config.RedmineKey,
|
Token: config.RedmineKey,
|
||||||
Cache: cache,
|
Cache: cache,
|
||||||
})
|
},
|
||||||
|
},
|
||||||
return fetchers
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCache() Cache {
|
func getCache() Cache {
|
||||||
|
|||||||
Reference in New Issue
Block a user