mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-09 13:50:03 +00:00
Add "travis" badge service
This commit is contained in:
parent
774f01afb2
commit
af87192ec9
1 changed files with 55 additions and 0 deletions
55
service_travis.go
Normal file
55
service_travis.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerServiceHandler("travis", func(params []string) (title, text, color string, err error) {
|
||||
if len(params) < 2 {
|
||||
err = errors.New("You need to provide user and repo")
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue