-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtravis.go
133 lines (115 loc) · 3.42 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"fmt"
"net/http"
"time"
travis "github.com/Ableton/go-travis"
"github.com/gizak/termui"
"github.com/google/go-github/github"
"github.com/sirupsen/logrus"
)
func doTravisCI() ([]*termui.Table, error) {
// Check that the Travis CI API token is not empty.
if len(travisToken) <= 0 {
logrus.Warn("Travis CI API token cannot be empty")
logrus.Info("skipping Travis CI data")
return nil, nil
}
// Check that the Travis owners is not empty.
if len(travisOwners) <= 0 {
logrus.Warn("Travis CI owners cannot be empty")
logrus.Info("skipping Travis CI data")
return nil, nil
}
tables := []*termui.Table{}
// Iterate over the travisOwners if it was passed.
for _, travisOwner := range travisOwners {
// Initialize the table.
table := termui.NewTable()
rows := [][]string{
{"repo", "branch", "state", "finished at"},
}
redrows := []int{}
otherrows := []int{}
// Get the owners repos from GitHub.
ghClient := github.NewClient(nil)
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 100},
Type: "sources",
}
var repos []*github.Repository
for {
reposResp, resp, err := ghClient.Repositories.List(context.Background(), travisOwner, opt)
if err != nil {
return nil, fmt.Errorf("listing repos for %q failed: %v", travisOwner, err)
}
repos = append(repos, reposResp...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
// Initialize the travis client.
travisClient := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, travisToken)
// Iterate over the repositories and get the master branch build status.
for _, repo := range repos {
if repo.GetFork() || repo.GetArchived() {
// Continue early if its a fork or archived because we don't care.
continue
}
// Get the master branch
branch, resp, err := travisClient.Branches.GetFromSlug(repo.GetFullName(), "master")
if err != nil {
// This will fail on forks or non travis building repos with a 404
// so we might as well error silently if we get a 404.
if resp.StatusCode == http.StatusNotFound {
continue
}
return nil, fmt.Errorf("getting master branch for travis repo %q failed: %v", repo.GetFullName(), err)
}
if showAllBuilds || branch.State != "passed" {
rows = append(rows, []string{
repo.GetName(),
"master",
branch.State,
printTime(branch.FinishedAt),
})
if branch.State == "failed" {
redrows = append(redrows, len(rows)-1)
} else if branch.State != "passed" {
otherrows = append(otherrows, len(rows)-1)
}
}
}
if len(rows) <= 1 {
// return early if we have no data
continue
}
// Set the rows.
table.Rows = rows
// Set the default colors and settings.
table.FgColor = termui.ColorWhite
table.BgColor = termui.ColorDefault
table.TextAlign = termui.AlignLeft
table.Border = true
table.Separator = true
table.Block.BorderLabel = "Travis CI builds for " + travisOwner
table.Analysis()
table.SetSize()
// Set the color to red for the red rows
for _, br := range redrows {
table.FgColors[br] = termui.ColorRed
}
// Set the color to yellow for the other rows
for _, br := range otherrows {
table.FgColors[br] = termui.ColorYellow
}
tables = append(tables, table)
}
return tables, nil
}
func printTime(s string) string {
t, _ := time.Parse("2006-01-02T15:04:05Z", s)
return t.Local().Format("Mon, Jan 02 15:04 MST")
}