mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
84
travisci/client.go
Normal file
84
travisci/client.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package travisci
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
const APIEnvToken = "WTF_TRAVIS_API_TOKEN"
|
||||
|
||||
func BuildsFor() (*Builds, error) {
|
||||
builds := &Builds{}
|
||||
|
||||
resp, err := travisRequest("builds")
|
||||
if err != nil {
|
||||
return builds, err
|
||||
}
|
||||
|
||||
parseJson(&builds, resp.Body)
|
||||
|
||||
return builds, nil
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
var (
|
||||
travisAPIURL = &url.URL{Scheme: "https", Host: "api.travis-ci.org", Path: "/"}
|
||||
)
|
||||
|
||||
func travisRequest(path string) (*http.Response, error) {
|
||||
params := url.Values{}
|
||||
params.Add("limit", "10")
|
||||
|
||||
url := travisAPIURL.ResolveReference(&url.URL{Path: path, RawQuery: params.Encode()})
|
||||
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Travis-API-Version", "3")
|
||||
|
||||
bearer := fmt.Sprintf("token %s", apiToken())
|
||||
req.Header.Add("Authorization", bearer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := &http.Client{}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return nil, fmt.Errorf(resp.Status)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func apiToken() string {
|
||||
return os.Getenv(APIEnvToken)
|
||||
}
|
||||
|
||||
func parseJson(obj interface{}, text io.Reader) {
|
||||
jsonStream, err := ioutil.ReadAll(text)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
|
||||
|
||||
for {
|
||||
if err := decoder.Decode(obj); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
25
travisci/travis.go
Normal file
25
travisci/travis.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package travisci
|
||||
|
||||
type Builds struct {
|
||||
Builds []Build `json:"builds"`
|
||||
}
|
||||
|
||||
type Build struct {
|
||||
CreatedBy Owner `json:"created_by"`
|
||||
Branch Branch `json:"branch"`
|
||||
Number string `json:"number"`
|
||||
Repository Repository `json:"repository"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type Owner struct {
|
||||
Login string `json:"login"`
|
||||
}
|
||||
|
||||
type Branch struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
82
travisci/widget.go
Normal file
82
travisci/widget.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package travisci
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" Travis CI", "travisci", false),
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
builds, err := BuildsFor()
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
|
||||
widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.Name))
|
||||
|
||||
var content string
|
||||
if err != nil {
|
||||
widget.View.SetWrap(true)
|
||||
content = err.Error()
|
||||
} else {
|
||||
widget.View.SetWrap(false)
|
||||
content = widget.contentFrom(builds)
|
||||
}
|
||||
|
||||
widget.View.SetText(content)
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) contentFrom(builds *Builds) string {
|
||||
var str string
|
||||
for _, build := range builds.Builds {
|
||||
str = str + fmt.Sprintf(
|
||||
"[%s] %s-%s (%s) [white]%s\n",
|
||||
buildColor(&build),
|
||||
build.Repository.Name,
|
||||
build.Number,
|
||||
build.Branch.Name,
|
||||
build.CreatedBy.Login,
|
||||
)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func buildColor(build *Build) string {
|
||||
switch build.State {
|
||||
case "broken":
|
||||
return "red"
|
||||
case "failed":
|
||||
return "red"
|
||||
case "failing":
|
||||
return "red"
|
||||
case "pending":
|
||||
return "yellow"
|
||||
case "started":
|
||||
return "yellow"
|
||||
case "fixed":
|
||||
return "green"
|
||||
case "passed":
|
||||
return "green"
|
||||
default:
|
||||
return "white"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user