This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
/
travis_ci.go
102 lines (86 loc) · 1.92 KB
/
travis_ci.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
package platform
import (
"context"
"strconv"
"github.com/shuheiktgw/go-travis"
)
const noToken = "none"
type TravisCI struct {
client *travis.Client
}
func NewTravisCI(token string) *TravisCI {
if token == noToken {
token = ""
}
return &TravisCI{
client: travis.NewClient(travis.ApiOrgUrl, token),
}
}
func (tc TravisCI) Builds(repository string, owner string, limit int64) ([][]string, error) {
include := []string{
"build.repository",
"build.state",
"build.duration",
"build.started_at",
"build.finished_at",
}
var builds []*travis.Build
var err error
if repository == "" || owner == "" {
builds, _, err = tc.client.Builds.List(
context.Background(),
&travis.BuildsOption{
Include: include,
Limit: int(limit),
},
)
if err != nil {
return nil, err
}
} else {
builds, _, err = tc.client.Builds.ListByRepoSlug(
context.Background(),
createRepoName(repository, owner),
&travis.BuildsByRepoOption{
Include: include,
Limit: int(limit),
SortBy: "build.finished_at",
},
)
if err != nil {
return nil, err
}
}
table := formatBuilds(builds, limit)
return table, nil
}
func formatBuilds(builds []*travis.Build, limit int64) [][]string {
table := make([][]string, limit+1)
table[0] = []string{
"Repository",
"State",
"Duration",
"Finished At",
}
if len(builds) > 0 {
for k, v := range builds {
table[k+1] = append(table[k+1], *v.Repository.Name)
table[k+1] = append(table[k+1], *v.State)
// Can be nil if
if v.Duration != nil {
table[k+1] = append(table[k+1], strconv.FormatUint(uint64(*v.Duration), 10))
} else {
table[k+1] = append(table[k+1], "Running")
}
if v.FinishedAt != nil {
table[k+1] = append(table[k+1], *v.FinishedAt)
} else {
table[k+1] = append(table[k+1], "Running")
}
}
}
return table
}
func createRepoName(repository string, owner string) string {
return owner + "/" + repository
}