diff --git a/service_github.go b/service_github.go index d539d81..b12b33d 100644 --- a/service_github.go +++ b/service_github.go @@ -37,6 +37,10 @@ type githubAsset struct { Downloads int64 `json:"download_count"` } +type githubRepo struct { + StargazersCount int64 `json:"stargazers_count"` +} + type githubServiceHandler struct{} func (g githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList { @@ -71,6 +75,11 @@ func (g githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList DemoPath: "/github/downloads/atom/atom/v1.8.0/atom-amd64.deb", Arguments: []string{"downloads", "", "", "", ""}, }, + { + ServiceName: "Github stars by repository", + DemoPath: "/github/stars/atom/atom", + Arguments: []string{"stars", "", ""}, + }, } } @@ -89,6 +98,8 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl title, text, color, err = g.handleLatestRelease(ctx, params[1:]) case "downloads": title, text, color, err = g.handleDownloads(ctx, params[1:]) + case "stars": + title, text, color, err = g.handleStargazers(ctx, params[1:]) default: err = errors.New("An unknown service command was called") } @@ -96,6 +107,27 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl return } +func (g githubServiceHandler) handleStargazers(ctx context.Context, params []string) (title, text, color string, err error) { + path := strings.Join([]string{"repos", params[0], params[1]}, "/") + + text, err = cacheStore.Get("github_repo_stargazers", path) + + if err != nil { + r := githubRepo{} + + if err = g.fetchAPI(ctx, path, nil, &r); err != nil { + return + } + + text = metricFormat(r.StargazersCount) + cacheStore.Set("github_repo_stargazers", path, text, 10*time.Minute) + } + + title = "stars" + color = "brightgreen" + return +} + func (g githubServiceHandler) handleDownloads(ctx context.Context, params []string) (title, text, color string, err error) { switch len(params) { case 2: