2016-06-28 19:04:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2016-06-29 13:13:26 +00:00
|
|
|
"time"
|
2016-06-28 22:51:51 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2016-06-28 19:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-06-28 21:56:22 +00:00
|
|
|
registerServiceHandler("travis", travisServiceHandler{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type travisServiceHandler struct{}
|
|
|
|
|
2016-06-29 11:09:08 +00:00
|
|
|
func (t travisServiceHandler) GetDocumentation() serviceHandlerDocumentationList {
|
|
|
|
return serviceHandlerDocumentationList{{
|
2016-06-28 21:56:22 +00:00
|
|
|
ServiceName: "Travis-CI",
|
|
|
|
DemoPath: "/travis/Luzifer/password",
|
|
|
|
Arguments: []string{"<user>", "<repo>", "[branch]"},
|
2016-06-29 11:09:08 +00:00
|
|
|
}}
|
2016-06-28 21:56:22 +00:00
|
|
|
}
|
2016-06-28 19:04:13 +00:00
|
|
|
|
2021-03-11 10:16:15 +00:00
|
|
|
func (travisServiceHandler) IsEnabled() bool { return true }
|
|
|
|
|
2016-06-28 22:51:51 +00:00
|
|
|
func (t travisServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
2016-06-28 21:56:22 +00:00
|
|
|
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]}, "/")
|
|
|
|
|
2016-06-29 13:13:26 +00:00
|
|
|
var state string
|
|
|
|
state, err = cacheStore.Get("travis", path)
|
|
|
|
|
2016-06-28 21:56:22 +00:00
|
|
|
if err != nil {
|
2016-06-29 13:13:26 +00:00
|
|
|
var resp *http.Response
|
2018-06-01 20:42:37 +00:00
|
|
|
req, _ := http.NewRequest("GET", "https://api.travis-ci.org/"+path, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req.WithContext(ctx))
|
2016-06-29 13:13:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-06-28 21:56:22 +00:00
|
|
|
|
2016-06-29 13:13:26 +00:00
|
|
|
r := struct {
|
|
|
|
File string `json:"file"`
|
|
|
|
Branch struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
} `json:"branch"`
|
|
|
|
}{}
|
2016-06-28 21:56:22 +00:00
|
|
|
|
2016-06-29 13:13:26 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = r.Branch.State
|
|
|
|
cacheStore.Set("travis", path, state, 5*time.Minute)
|
2016-06-28 21:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "travis"
|
2016-06-29 13:13:26 +00:00
|
|
|
text = state
|
2016-06-28 21:56:22 +00:00
|
|
|
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
|
|
|
}
|