-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis-dump.go
202 lines (183 loc) · 5.27 KB
/
travis-dump.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
/* Configuration */
const APIKey = "travis_api_key"
const RepoId = "repo_id"
type TravisUser struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
ID int `json:"id"`
Login string `json:"login"`
}
type TravisBuild struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
Permissions struct {
Read bool `json:"read"`
Cancel bool `json:"cancel"`
Restart bool `json:"restart"`
} `json:"@permissions"`
ID int `json:"id"`
Number string `json:"number"`
State string `json:"state"`
Duration int `json:"duration"`
EventType string `json:"event_type"`
PreviousState string `json:"previous_state"`
PullRequestTitle string `json:"pull_request_title"`
PullRequestNumber interface{} `json:"pull_request_number"`
StartedAt time.Time `json:"started_at"`
FinishedAt time.Time `json:"finished_at"`
Repository struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"repository"`
Branch struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
Name string `json:"name"`
} `json:"branch"`
Tag interface{} `json:"tag"`
Commit struct {
Type string `json:"@type"`
Representation string `json:"@representation"`
ID int `json:"id"`
Sha string `json:"sha"`
Ref string `json:"ref"`
Message string `json:"message"`
CompareURL string `json:"compare_url"`
CommittedAt time.Time `json:"committed_at"`
} `json:"commit"`
Jobs []struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
ID int `json:"id"`
} `json:"jobs"`
Stages []interface{} `json:"stages"`
CreatedBy TravisUser `json:"created_by"`
}
type TravisBuildResponse struct {
Type string `json:"@type"`
Href string `json:"@href"`
Representation string `json:"@representation"`
Pagination struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
Count int `json:"count"`
IsFirst bool `json:"is_first"`
IsLast bool `json:"is_last"`
Next struct {
Href string `json:"@href"`
Offset int `json:"offset"`
Limit int `json:"limit"`
} `json:"next"`
Prev interface{} `json:"prev"`
First struct {
Href string `json:"@href"`
Offset int `json:"offset"`
Limit int `json:"limit"`
} `json:"first"`
Last struct {
Href string `json:"@href"`
Offset int `json:"offset"`
Limit int `json:"limit"`
} `json:"last"`
} `json:"@pagination"`
Builds []TravisBuild `json:"builds"`
}
func main() {
writer := createWriter()
processAllBuilds(RepoId, 7200, writer)
writer.Flush()
}
func processAllBuilds(repo string, nextOffset int, writer *csv.Writer) {
limit := 100
fetched := 0
initOffset := nextOffset
for {
builds := getBuilds(repo, limit, nextOffset)
fetched += len(builds.Builds)
fmt.Println("Progress: ", fetched, " of: ", builds.Pagination.Count-initOffset)
writeToCSV(builds.Builds, writer)
if builds.Pagination.Next.Offset == 0 {
break // Reached the end
}
nextOffset = builds.Pagination.Next.Offset
}
}
func createWriter() *csv.Writer {
file, _ := os.Create("travis-builds.csv")
writer := csv.NewWriter(file)
// Headers
headers := []string{
"ID",
"Number",
"State",
"EventType",
"RepositoryName",
"BranchName",
"PullRequestTitle",
"StartedAt",
"FinishedAt",
"Duration",
"CreatedByID",
"CreatedByLogin"}
writer.Write(headers)
return writer
}
func writeToCSV(builds []TravisBuild, writer *csv.Writer) {
// Content
for _, build := range builds {
line := []string{
strconv.FormatInt(int64(build.ID), 10),
build.Number,
build.State,
build.EventType,
build.Repository.Name,
build.Branch.Name,
build.PullRequestTitle,
build.StartedAt.Format(time.RFC3339),
build.FinishedAt.Format(time.RFC3339),
strconv.FormatInt(int64(build.Duration), 10),
strconv.FormatInt(int64(build.CreatedBy.ID), 10),
build.CreatedBy.Login}
writer.Write(line)
}
}
func getBuilds(repo string, limit int, offset int) TravisBuildResponse {
var buildResponse TravisBuildResponse
url := fmt.Sprintf("https://api.travis-ci.com/repo/%s/builds?limit=%d&offset=%d&sort_by=started_at:desc", repo, limit, offset)
token := fmt.Sprintf("token %s", APIKey)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Add("Travis-API-Version", "3")
req.Header.Add("Authorization", token)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&buildResponse); err != nil {
log.Println(err)
}
return buildResponse
}