mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 13:20:02 +00:00
add GitHub latest-tag integration
This commit is contained in:
parent
5101a3bb66
commit
d8da4cd926
1 changed files with 67 additions and 15 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -24,6 +25,11 @@ func (g githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList
|
||||||
DemoPath: "/github/license/Luzifer/badge-gen",
|
DemoPath: "/github/license/Luzifer/badge-gen",
|
||||||
Arguments: []string{"license", "<user>", "<repo>"},
|
Arguments: []string{"license", "<user>", "<repo>"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ServiceName: "GitHub latest tag",
|
||||||
|
DemoPath: "/github/latest-tag/Luzifer/badge-gen",
|
||||||
|
Arguments: []string{"latest-tag", "<user>", "<repo>"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +42,8 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl
|
||||||
switch params[0] {
|
switch params[0] {
|
||||||
case "license":
|
case "license":
|
||||||
title, text, color, err = g.handleLicense(ctx, params[1:])
|
title, text, color, err = g.handleLicense(ctx, params[1:])
|
||||||
|
case "latest-tag":
|
||||||
|
title, text, color, err = g.handleLatestTag(ctx, params[1:])
|
||||||
default:
|
default:
|
||||||
err = errors.New("An unknown service command was called")
|
err = errors.New("An unknown service command was called")
|
||||||
}
|
}
|
||||||
|
@ -43,32 +51,54 @@ func (g githubServiceHandler) Handle(ctx context.Context, params []string) (titl
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"}, "/")
|
||||||
|
|
||||||
|
text, err = cacheStore.Get("github_latest_tag", path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
r := []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err = g.fetchAPI(ctx, path, nil, &r); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(r) > 0 {
|
||||||
|
text = r[0].Name
|
||||||
|
} else {
|
||||||
|
text = "None"
|
||||||
|
}
|
||||||
|
cacheStore.Set("github_latest_tag", path, text, 10*time.Minute)
|
||||||
|
}
|
||||||
|
|
||||||
|
title = "tag"
|
||||||
|
color = "blue"
|
||||||
|
|
||||||
|
if regexp.MustCompile(`^v?0\.`).MatchString(text) {
|
||||||
|
color = "orange"
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (g githubServiceHandler) handleLicense(ctx context.Context, params []string) (title, text, color string, err error) {
|
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"}, "/")
|
path := strings.Join([]string{"repos", params[0], params[1], "license"}, "/")
|
||||||
|
|
||||||
text, err = cacheStore.Get("github_license", path)
|
text, err = cacheStore.Get("github_license", path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil)
|
|
||||||
req.Header.Set("Accept", "application/vnd.github.drax-preview+json")
|
|
||||||
|
|
||||||
if configStore.Str("github.personal_token") != "" {
|
|
||||||
req.SetBasicAuth(configStore.Str("github.username"), configStore.Str("github.personal_token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
var resp *http.Response
|
|
||||||
resp, err = ctxhttp.Do(ctx, http.DefaultClient, req)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
r := struct {
|
r := struct {
|
||||||
License struct {
|
License struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
} `json:"license"`
|
} `json:"license"`
|
||||||
}{}
|
}{}
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"Accept": "application/vnd.github.drax-preview+json",
|
||||||
|
}
|
||||||
|
if err = g.fetchAPI(ctx, path, headers, &r); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,3 +115,25 @@ func (g githubServiceHandler) handleLicense(ctx context.Context, params []string
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g githubServiceHandler) fetchAPI(ctx context.Context, path string, headers map[string]string, out interface{}) error {
|
||||||
|
req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil)
|
||||||
|
|
||||||
|
if headers != nil {
|
||||||
|
for k, v := range headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if configStore.Str("github.personal_token") != "" {
|
||||||
|
req.SetBasicAuth(configStore.Str("github.username"), configStore.Str("github.personal_token"))
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := ctxhttp.Do(ctx, http.DefaultClient, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return json.NewDecoder(resp.Body).Decode(out)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue