1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-12-24 18:31:16 +00:00
badge-gen/service_travis.go

68 lines
1.3 KiB
Go
Raw Permalink Normal View History

2016-06-28 19:04:13 +00:00
package main
import (
"encoding/json"
"errors"
"net/http"
"strings"
)
func init() {
2016-06-28 21:56:22 +00:00
registerServiceHandler("travis", travisServiceHandler{})
}
type travisServiceHandler struct{}
func (t travisServiceHandler) GetDocumentation() serviceHandlerDocumentation {
return serviceHandlerDocumentation{
ServiceName: "Travis-CI",
DemoPath: "/travis/Luzifer/password",
Arguments: []string{"<user>", "<repo>", "[branch]"},
}
}
2016-06-28 19:04:13 +00:00
2016-06-28 21:56:22 +00:00
func (t travisServiceHandler) Handle(params []string) (title, text, color string, err error) {
if len(params) < 2 {
err = errors.New("You need to provide user and repo")
2016-06-28 19:04:13 +00:00
return
2016-06-28 21:56:22 +00:00
}
if len(params) < 3 {
params = append(params, "master")
}
path := strings.Join([]string{"repos", params[0], params[1], "branches", params[2]}, "/")
var resp *http.Response
resp, err = http.Get("https://api.travis-ci.org/" + path)
if err != nil {
return
}
defer resp.Body.Close()
r := struct {
File string `json:"file"`
Branch struct {
State string `json:"state"`
} `json:"branch"`
}{}
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
return
}
title = "travis"
text = r.Branch.State
if text == "" {
text = "unknown"
}
color = map[string]string{
"unknown": "9f9f9f",
"passed": "4c1",
"failed": "e05d44",
"canceled": "9f9f9f",
}[text]
return
2016-06-28 19:04:13 +00:00
}