-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.go
221 lines (194 loc) · 4.76 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/url"
"os"
"sync"
"time"
"github.com/json-iterator/go"
"github.com/lc/secretz/lib"
)
var repos []string
// TravisCI is the struct that holds repo slugs.
type TravisCI struct {
Repositories []struct {
Slug string `json:"slug"`
Active bool `json:"active"`
} `json:"repositories"`
}
// Builds is the struct that build information from TravisCI
type Builds struct {
Type string `json:"@type"`
Pagination struct {
IsFirst bool `json:"is_first"`
IsLast bool `json:"is_last"`
} `json:"@pagination"`
Builds []struct {
Jobs []struct {
JobId int `json:"id"`
} `json:"jobs"`
} `json:"builds"`
}
var (
setkey string
org string
timeout int
concurrency int
delay int
// GetMembers is a flag with the options: list or scan
GetMembers string
targets []string
)
func init() {
flag.Usage = lib.Usage
flag.StringVar(&setkey, "setkey", "", "Set API Key for api.travis-ci.org")
flag.StringVar(&org, "t", "", "Target organization")
flag.IntVar(&timeout, "timeout", 30, "Timeout for the tool in seconds")
flag.IntVar(&concurrency, "c", 5, "Number of concurrent fetchers")
flag.StringVar(&GetMembers, "members", "", "Get GitHub Org Members")
flag.IntVar(&delay, "delay", 600, "delay between requests + random delay/2 jitter")
}
// Job struct holds a JobID and the Organization name.
type Job struct {
ID int
Org string
}
func main() {
flag.Parse()
lib.Secretz.Timeout = time.Duration(timeout) * time.Second
if setkey != "" {
lib.SetAPIKey(setkey)
}
if len(org) < 1 || setkey != "" {
log.Fatalf("Usage: %s -t <organization>\n", os.Args[0])
}
if GetMembers == "" {
targets = append(targets, org)
} else {
switch GetMembers {
case "list":
GHMem := lib.OrgMembers(org)
for _, Member := range *GHMem {
fmt.Println(Member.Login)
}
os.Exit(0)
case "scan":
GHMem := lib.OrgMembers(org)
for _, Member := range *GHMem {
targets = append(targets, Member.Login)
}
break
default:
log.Fatalf("Invalid option specified in -members flag!\n")
}
}
for _, org := range targets {
lib.CreateOrg(org)
log.Printf("Fetching repos for %s\n", org)
ParseResponse(org)
log.Printf("Fetching builds for %s's repos\n", org)
jobChan := make(chan *Job)
finishedChan := make(chan string)
var wg, wg2 sync.WaitGroup
wg2.Add(1)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
SaveLogs(jobChan, finishedChan)
}()
}
go func() {
defer wg2.Done()
for str := range finishedChan {
log.Printf("%s\n", str)
}
}()
go func() {
for _, slug := range repos {
builds := GetBuilds(slug)
for _, job := range builds {
jobChan <- &Job{ID: job, Org: org}
}
}
close(jobChan)
}()
wg.Wait()
close(finishedChan)
wg2.Wait()
}
}
// ParseResponse gets the JSON response from Travis and parses it for repo slugs
func ParseResponse(org string) {
for {
api := fmt.Sprintf("https://api.travis-ci.org/owner/%s/repos?limit=100&offset=%d", org, len(repos))
res := lib.QueryApi(api)
ciResp := new(TravisCI)
err := jsoniter.Unmarshal(res, ciResp)
if err != nil {
log.Fatalf("Could not decode json: %s\n", err)
}
if len(ciResp.Repositories) == 0 {
break
}
for _, repo := range ciResp.Repositories {
repos = append(repos, repo.Slug)
}
}
}
// GetBuilds gets all JobId's from builds of a repo.
func GetBuilds(slug string) []int {
builds := new(Builds)
jobs := []int{}
offset := 0
for {
log.Printf("Fetching builds %s [offset: %d]\n", slug, offset)
api := fmt.Sprintf("https://api.travis-ci.org/repo/%s/builds?limit=100&offset=%d", url.QueryEscape(slug), offset)
res := lib.QueryApi(api)
// delay + jitter
if delay > 0 {
time.Sleep((time.Duration(delay + rand.Intn(delay/2))) * time.Millisecond)
}
err := jsoniter.Unmarshal(res, builds)
if err != nil {
log.Fatalf("Could not decode json: %s\n", err)
}
if builds.Type == "error" {
break
}
loop := len(builds.Builds)
i := 0
for i < loop {
for _, z := range builds.Builds[i].Jobs {
jobs = append(jobs, z.JobId)
}
i++
}
if builds.Pagination.IsLast {
break
}
offset += 100
}
return jobs
}
// SaveLogs saves build logs for given job ids
func SaveLogs(jobChan chan *Job, resultChan chan string) {
for job := range jobChan {
api := fmt.Sprintf("https://api.travis-ci.org/job/%d/log.txt", job.ID)
// delay + jitter
if delay > 0 {
time.Sleep((time.Duration(delay + rand.Intn(delay/2))) * time.Millisecond)
}
res := lib.QueryApi(api)
file := fmt.Sprintf("output/%s/%d.txt", job.Org, job.ID)
err := ioutil.WriteFile(file, res, 0644)
if err != nil {
log.Fatalf("Error: %v", err)
}
resultChan <- fmt.Sprintf("Wrote log %d to %s", job.ID, file)
}
}