-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_travis.go
87 lines (71 loc) · 2.1 KB
/
service_travis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
const travisCacheDuration = 5 * time.Minute
func init() {
registerServiceHandler("travis", travisServiceHandler{})
}
type travisServiceHandler struct{}
func (travisServiceHandler) GetDocumentation() serviceHandlerDocumentationList {
return serviceHandlerDocumentationList{{
ServiceName: "Travis-CI",
DemoPath: "/travis/Luzifer/password",
Arguments: []string{"<user>", "<repo>", "[branch]"},
}}
}
func (travisServiceHandler) IsEnabled() bool { return true }
func (travisServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
if len(params) < 2 { //nolint:gomnd
err = errors.New("you need to provide user and repo")
return title, text, color, err
}
if len(params) < 3 { //nolint:gomnd
params = append(params, "master")
}
path := strings.Join([]string{"repos", params[0], params[1], "branches", params[2]}, "/")
var state string
state, err = cacheStore.Get("travis", path)
if err != nil {
var resp *http.Response
req, _ := http.NewRequestWithContext(ctx, "GET", "https://api.travis-ci.org/"+path, nil)
resp, err = http.DefaultClient.Do(req)
if err != nil {
return title, text, color, errors.Wrap(err, "executing request")
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.WithError(err).Error("closing request body (leaked fd)")
}
}()
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, text, color, errors.Wrap(err, "decoding JSON response")
}
state = r.Branch.State
logErr(cacheStore.Set("travis", path, state, travisCacheDuration), "writing Travis status to cache")
}
title = "travis"
text = state
if text == "" {
text = "unknown"
}
color = map[string]string{
"unknown": "9f9f9f",
"passed": "4c1",
"failed": "e05d44",
"canceled": "9f9f9f",
}[text]
return title, text, color, nil
}