2016-06-29 11:50:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2016-07-06 16:35:05 +00:00
|
|
|
"regexp"
|
2016-06-29 11:50:24 +00:00
|
|
|
"strings"
|
2016-06-29 13:13:26 +00:00
|
|
|
"time"
|
2016-06-29 11:50:24 +00:00
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-29 11:50:24 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
const githubCacheDuration = 10 * time.Minute
|
|
|
|
|
2016-06-29 11:50:24 +00:00
|
|
|
func init() {
|
|
|
|
registerServiceHandler("github", githubServiceHandler{})
|
|
|
|
}
|
|
|
|
|
2016-07-06 23:25:09 +00:00
|
|
|
type githubRelease struct {
|
|
|
|
TagName string `json:"tag_name"`
|
|
|
|
Assets []githubAsset `json:"assets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type githubAsset struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Downloads int64 `json:"download_count"`
|
|
|
|
}
|
|
|
|
|
2018-06-01 20:14:38 +00:00
|
|
|
type githubRepo struct {
|
|
|
|
StargazersCount int64 `json:"stargazers_count"`
|
|
|
|
}
|
|
|
|
|
2016-06-29 11:50:24 +00:00
|
|
|
type githubServiceHandler struct{}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
func (githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList {
|
2016-06-29 11:50:24 +00:00
|
|
|
return serviceHandlerDocumentationList{
|
|
|
|
{
|
|
|
|
ServiceName: "GitHub repo license",
|
|
|
|
DemoPath: "/github/license/Luzifer/badge-gen",
|
|
|
|
Arguments: []string{"license", "<user>", "<repo>"},
|
|
|
|
},
|
2016-07-06 16:35:05 +00:00
|
|
|
{
|
|
|
|
ServiceName: "GitHub latest tag",
|
|
|
|
DemoPath: "/github/latest-tag/Luzifer/badge-gen",
|
|
|
|
Arguments: []string{"latest-tag", "<user>", "<repo>"},
|
|
|
|
},
|
2016-07-06 16:54:43 +00:00
|
|
|
{
|
|
|
|
ServiceName: "GitHub latest release",
|
|
|
|
DemoPath: "/github/latest-release/lastpass/lastpass-cli",
|
|
|
|
Arguments: []string{"latest-release", "<user>", "<repo>"},
|
|
|
|
},
|
2016-07-06 23:25:09 +00:00
|
|
|
{
|
|
|
|
ServiceName: "GitHub downloads by repo",
|
|
|
|
DemoPath: "/github/downloads/atom/atom",
|
|
|
|
Arguments: []string{"downloads", "<user>", "<repo>"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ServiceName: "GitHub downloads by release",
|
|
|
|
DemoPath: "/github/downloads/atom/atom/latest",
|
|
|
|
Arguments: []string{"downloads", "<user>", "<repo>", "<tag or \"latest\">"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ServiceName: "GitHub downloads by release and asset",
|
|
|
|
DemoPath: "/github/downloads/atom/atom/v1.8.0/atom-amd64.deb",
|
|
|
|
Arguments: []string{"downloads", "<user>", "<repo>", "<tag or \"latest\">", "<asset>"},
|
|
|
|
},
|
2018-06-01 20:14:38 +00:00
|
|
|
{
|
|
|
|
ServiceName: "Github stars by repository",
|
|
|
|
DemoPath: "/github/stars/atom/atom",
|
|
|
|
Arguments: []string{"stars", "<user>", "<repo>"},
|
|
|
|
},
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:16:15 +00:00
|
|
|
func (githubServiceHandler) IsEnabled() bool { return true }
|
|
|
|
|
2016-06-29 11:50:24 +00:00
|
|
|
func (g githubServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
2023-09-08 12:23:22 +00:00
|
|
|
if len(params) < 2 { //nolint:gomnd
|
|
|
|
err = errors.New("no service-command / parameters were given")
|
|
|
|
return title, text, color, err
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch params[0] {
|
|
|
|
case "license":
|
|
|
|
title, text, color, err = g.handleLicense(ctx, params[1:])
|
2016-07-06 16:35:05 +00:00
|
|
|
case "latest-tag":
|
|
|
|
title, text, color, err = g.handleLatestTag(ctx, params[1:])
|
2016-07-06 16:54:43 +00:00
|
|
|
case "latest-release":
|
|
|
|
title, text, color, err = g.handleLatestRelease(ctx, params[1:])
|
2023-09-08 12:23:22 +00:00
|
|
|
case "downloads": //nolint:goconst
|
2016-07-06 23:25:09 +00:00
|
|
|
title, text, color, err = g.handleDownloads(ctx, params[1:])
|
2018-06-01 20:14:38 +00:00
|
|
|
case "stars":
|
|
|
|
title, text, color, err = g.handleStargazers(ctx, params[1:])
|
2016-06-29 11:50:24 +00:00
|
|
|
default:
|
2023-09-08 12:23:22 +00:00
|
|
|
err = errors.New("an unknown service command was called")
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 20:14:38 +00:00
|
|
|
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 {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2018-06-01 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text = metricFormat(r.StargazersCount)
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_repo_stargazers", path, text, githubCacheDuration), "writing Github repo stargazers to cache")
|
2018-06-01 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "stars"
|
2023-09-08 12:23:22 +00:00
|
|
|
color = colorNameBrightGreen
|
|
|
|
return title, text, color, err
|
2018-06-01 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 23:25:09 +00:00
|
|
|
func (g githubServiceHandler) handleDownloads(ctx context.Context, params []string) (title, text, color string, err error) {
|
|
|
|
switch len(params) {
|
2023-09-08 12:23:22 +00:00
|
|
|
case 2: //nolint:gomnd
|
2016-07-06 23:25:09 +00:00
|
|
|
title, text, color, err = g.handleRepoDownloads(ctx, params)
|
2023-09-08 12:23:22 +00:00
|
|
|
case 3: //nolint:gomnd
|
2016-07-06 23:25:09 +00:00
|
|
|
params = append(params, "total")
|
|
|
|
fallthrough
|
2023-09-08 12:23:22 +00:00
|
|
|
case 4: //nolint:gomnd
|
2016-07-06 23:25:09 +00:00
|
|
|
title, text, color, err = g.handleReleaseDownloads(ctx, params)
|
|
|
|
default:
|
|
|
|
err = errors.New("Unsupported number of arguments")
|
|
|
|
}
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g githubServiceHandler) handleReleaseDownloads(ctx context.Context, params []string) (title, text, color string, err error) {
|
|
|
|
path := strings.Join([]string{"repos", params[0], params[1], "releases", "tags", params[2]}, "/")
|
|
|
|
if params[2] == "latest" {
|
|
|
|
path = strings.Join([]string{"repos", params[0], params[1], "releases", params[2]}, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err = cacheStore.Get("github_release_downloads", path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
r := githubRelease{}
|
|
|
|
|
|
|
|
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sum int64
|
|
|
|
|
|
|
|
for _, rel := range r.Assets {
|
|
|
|
if params[3] == "total" || rel.Name == params[3] {
|
2023-09-08 12:23:22 +00:00
|
|
|
sum += rel.Downloads
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
text = metricFormat(sum)
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_release_downloads", path, text, githubCacheDuration), "writing Github release downloads to cache")
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "downloads"
|
2023-09-08 12:23:22 +00:00
|
|
|
color = colorNameBrightGreen
|
|
|
|
return title, text, color, err
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g githubServiceHandler) handleRepoDownloads(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_repo_downloads", path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
r := []githubRelease{}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
// NOTE: This does not respect pagination!
|
2016-07-06 23:25:09 +00:00
|
|
|
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sum int64
|
|
|
|
|
|
|
|
for _, rel := range r {
|
|
|
|
for _, rea := range rel.Assets {
|
2023-09-08 12:23:22 +00:00
|
|
|
sum += rea.Downloads
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
text = metricFormat(sum)
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_repo_downloads", path, text, githubCacheDuration), "writing Github repo downloads to cache")
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "downloads"
|
2023-09-08 12:23:22 +00:00
|
|
|
color = colorNameBrightGreen
|
|
|
|
return title, text, color, err
|
2016-07-06 23:25:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:54:43 +00:00
|
|
|
func (g githubServiceHandler) handleLatestRelease(ctx context.Context, params []string) (title, text, color string, err error) {
|
2016-07-06 20:54:21 +00:00
|
|
|
path := strings.Join([]string{"repos", params[0], params[1], "releases", "latest"}, "/")
|
2016-07-06 16:54:43 +00:00
|
|
|
|
|
|
|
text, err = cacheStore.Get("github_latest_release", path)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-06 23:25:09 +00:00
|
|
|
r := githubRelease{}
|
2016-07-06 16:54:43 +00:00
|
|
|
|
|
|
|
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 16:54:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 20:54:21 +00:00
|
|
|
text = r.TagName
|
|
|
|
if text == "" {
|
2023-09-08 12:23:22 +00:00
|
|
|
text = "None" //nolint:goconst
|
2016-07-06 16:54:43 +00:00
|
|
|
}
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_latest_release", path, text, githubCacheDuration), "writing Github last release to cache")
|
2016-07-06 16:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "release"
|
2023-09-08 12:23:22 +00:00
|
|
|
color = colorNameBlue
|
2016-07-06 16:54:43 +00:00
|
|
|
|
|
|
|
if regexp.MustCompile(`^v?0\.`).MatchString(text) {
|
|
|
|
color = "orange"
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 16:54:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:35:05 +00:00
|
|
|
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"}, "/")
|
2016-06-29 11:50:24 +00:00
|
|
|
|
2016-07-06 16:35:05 +00:00
|
|
|
text, err = cacheStore.Get("github_latest_tag", path)
|
2016-06-29 11:50:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-07-06 16:35:05 +00:00
|
|
|
r := []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}{}
|
2016-06-29 13:13:26 +00:00
|
|
|
|
2016-07-06 16:35:05 +00:00
|
|
|
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-05 13:48:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:35:05 +00:00
|
|
|
if len(r) > 0 {
|
|
|
|
text = r[0].Name
|
|
|
|
} else {
|
|
|
|
text = "None"
|
2016-06-29 13:13:26 +00:00
|
|
|
}
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_latest_tag", path, text, githubCacheDuration), "writing Github last tag to cache")
|
2016-07-06 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "tag"
|
2023-09-08 12:23:22 +00:00
|
|
|
color = colorNameBlue
|
2016-07-06 16:35:05 +00:00
|
|
|
|
|
|
|
if regexp.MustCompile(`^v?0\.`).MatchString(text) {
|
|
|
|
color = "orange"
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-07-06 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g githubServiceHandler) handleLicense(ctx context.Context, params []string) (title, text, color string, err error) {
|
|
|
|
path := strings.Join([]string{"repos", params[0], params[1], "license"}, "/")
|
|
|
|
|
|
|
|
text, err = cacheStore.Get("github_license", path)
|
2016-06-29 13:13:26 +00:00
|
|
|
|
2016-07-06 16:35:05 +00:00
|
|
|
if err != nil {
|
2016-06-29 13:13:26 +00:00
|
|
|
r := struct {
|
|
|
|
License struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
} `json:"license"`
|
|
|
|
}{}
|
2016-07-06 16:35:05 +00:00
|
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
"Accept": "application/vnd.github.drax-preview+json",
|
|
|
|
}
|
|
|
|
if err = g.fetchAPI(ctx, path, headers, &r); err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-06-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text = r.License.Name
|
2023-09-08 12:23:22 +00:00
|
|
|
logErr(cacheStore.Set("github_license", path, text, githubCacheDuration), "writing Github license to cache")
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "license"
|
|
|
|
color = "007ec6"
|
|
|
|
|
|
|
|
if text == "" {
|
|
|
|
text = "None"
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
return title, text, color, err
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
2016-07-06 16:35:05 +00:00
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
func (githubServiceHandler) fetchAPI(ctx context.Context, path string, headers map[string]string, out interface{}) error {
|
|
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/"+path, nil)
|
|
|
|
for k, v := range headers {
|
|
|
|
req.Header.Set(k, v)
|
2016-07-06 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 12:48:20 +00:00
|
|
|
// #configStore github.username - string - Username for Github auth to increase API requests
|
|
|
|
// #configStore github.personal_token - string - Token for Github auth to increase API requests
|
2016-07-06 16:35:05 +00:00
|
|
|
if configStore.Str("github.personal_token") != "" {
|
|
|
|
req.SetBasicAuth(configStore.Str("github.username"), configStore.Str("github.personal_token"))
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2016-07-06 16:35:05 +00:00
|
|
|
if err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return errors.Wrap(err, "executing HTTP request")
|
2016-07-06 16:35:05 +00:00
|
|
|
}
|
2023-09-08 12:23:22 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing request body (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2016-07-06 16:35:05 +00:00
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
return errors.Wrap(
|
|
|
|
json.NewDecoder(resp.Body).Decode(out),
|
|
|
|
"decoding JSON response",
|
|
|
|
)
|
2016-07-06 16:35:05 +00:00
|
|
|
}
|