2016-06-29 11:50:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2016-06-29 13:13:26 +00:00
|
|
|
"time"
|
2016-06-29 11:50:24 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/context/ctxhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerServiceHandler("github", githubServiceHandler{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type githubServiceHandler struct{}
|
|
|
|
|
|
|
|
func (g githubServiceHandler) GetDocumentation() serviceHandlerDocumentationList {
|
|
|
|
return serviceHandlerDocumentationList{
|
|
|
|
{
|
|
|
|
ServiceName: "GitHub repo license",
|
|
|
|
DemoPath: "/github/license/Luzifer/badge-gen",
|
|
|
|
Arguments: []string{"license", "<user>", "<repo>"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g githubServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
|
|
|
if len(params) < 2 {
|
|
|
|
err = errors.New("No service-command / parameters were given")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch params[0] {
|
|
|
|
case "license":
|
|
|
|
title, text, color, err = g.handleLicense(ctx, params[1:])
|
|
|
|
default:
|
|
|
|
err = errors.New("An unknown service command was called")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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"}, "/")
|
|
|
|
|
2016-06-29 13:13:26 +00:00
|
|
|
text, err = cacheStore.Get("github_license", path)
|
2016-06-29 11:50:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-29 13:13:26 +00:00
|
|
|
req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil)
|
|
|
|
req.Header.Set("Accept", "application/vnd.github.drax-preview+json")
|
|
|
|
|
2016-07-05 13:48:57 +00:00
|
|
|
if configStore.Str("github.personal_token") != "" {
|
|
|
|
req.SetBasicAuth(configStore.Str("github.username"), configStore.Str("github.personal_token"))
|
|
|
|
}
|
|
|
|
|
2016-06-29 13:13:26 +00:00
|
|
|
var resp *http.Response
|
|
|
|
resp, err = ctxhttp.Do(ctx, http.DefaultClient, req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
r := struct {
|
|
|
|
License struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
} `json:"license"`
|
|
|
|
}{}
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
text = r.License.Name
|
|
|
|
cacheStore.Set("github_license", path, text, 10*time.Minute)
|
2016-06-29 11:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "license"
|
|
|
|
color = "007ec6"
|
|
|
|
|
|
|
|
if text == "" {
|
|
|
|
text = "None"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|