1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-11-08 13:20:02 +00:00

add GitHub latest-release integration

This commit is contained in:
Knut Ahlers 2016-07-06 18:54:43 +02:00
parent d8da4cd926
commit 5a7e7c5b50
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -30,6 +30,11 @@ func (g githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList
DemoPath: "/github/latest-tag/Luzifer/badge-gen", DemoPath: "/github/latest-tag/Luzifer/badge-gen",
Arguments: []string{"latest-tag", "<user>", "<repo>"}, Arguments: []string{"latest-tag", "<user>", "<repo>"},
}, },
{
ServiceName: "GitHub latest release",
DemoPath: "/github/latest-release/lastpass/lastpass-cli",
Arguments: []string{"latest-release", "<user>", "<repo>"},
},
} }
} }
@ -44,6 +49,8 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl
title, text, color, err = g.handleLicense(ctx, params[1:]) title, text, color, err = g.handleLicense(ctx, params[1:])
case "latest-tag": case "latest-tag":
title, text, color, err = g.handleLatestTag(ctx, params[1:]) title, text, color, err = g.handleLatestTag(ctx, params[1:])
case "latest-release":
title, text, color, err = g.handleLatestRelease(ctx, params[1:])
default: default:
err = errors.New("An unknown service command was called") err = errors.New("An unknown service command was called")
} }
@ -51,6 +58,38 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl
return return
} }
func (g githubServiceHandler) handleLatestRelease(ctx context.Context, params []string) (title, text, color string, err error) {
path := strings.Join([]string{"repos", params[0], params[1], "releases"}, "/")
text, err = cacheStore.Get("github_latest_release", path)
if err != nil {
r := []struct {
TagName string `json:"tag_name"`
}{}
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
return
}
if len(r) > 0 {
text = r[0].TagName
} else {
text = "None"
}
cacheStore.Set("github_latest_release", path, text, 10*time.Minute)
}
title = "release"
color = "blue"
if regexp.MustCompile(`^v?0\.`).MatchString(text) {
color = "orange"
}
return
}
func (g githubServiceHandler) handleLatestTag(ctx context.Context, params []string) (title, text, color string, err error) { func (g githubServiceHandler) handleLatestTag(ctx context.Context, params []string) (title, text, color string, err error) {
path := strings.Join([]string{"repos", params[0], params[1], "tags"}, "/") path := strings.Join([]string{"repos", params[0], params[1], "tags"}, "/")