1
0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-09-18 23:02:57 +00:00

Add Github stars by repo

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-06-01 22:14:38 +02:00
parent 820c81f62f
commit 76da5c2921
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E

View File

@ -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", "<user>", "<repo>", "<tag or \"latest\">", "<asset>"},
},
{
ServiceName: "Github stars by repository",
DemoPath: "/github/stars/atom/atom",
Arguments: []string{"stars", "<user>", "<repo>"},
},
}
}
@ -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: