1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/travisci/widget.go
Anand Sudhir Prayaga d0102b643a add travis ci module
close #245
2018-07-19 15:12:42 +02:00

83 lines
1.4 KiB
Go

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"
}
}