From 76da5c2921ab6cbed1c357f3685918903e5815d6 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 1 Jun 2018 22:14:38 +0200 Subject: [PATCH] Add Github stars by repo Signed-off-by: Knut Ahlers --- service_github.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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: