-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
348 lines (306 loc) · 8.28 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
type RateLimit struct {
Limit int
Remaining int
Cost int
ResetAt time.Time
}
type Repository struct {
URL string
SshUrl string
Name string
IsEmpty bool
PrimaryLanguage struct {
Name string
}
Owner struct {
URL string
}
}
type RepoResult struct {
URL string `json:"url"`
SshUrl string `json:"ssh_url"`
PrimaryLanguage string `json:"language"`
}
type UserResult struct {
URL string `json:"user"`
Repos *[]RepoResult `json:"repos"`
}
type RateLimitQuery struct {
RateLimit RateLimit `graphql:"rateLimit"`
}
type ReposQuery struct {
RateLimit RateLimit `graphql:"rateLimit"`
Search struct {
RepositoryCount int
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
Edges []struct {
Node struct {
Repo Repository `graphql:"... on Repository"`
}
}
} `graphql:"search(query: $query, type: REPOSITORY, first: 100, after: $after)"`
}
var (
githubCreateDate = time.Date(2008, 2, 8, 0, 0, 0, 0, time.UTC)
httpClient *http.Client
githubV4Client *githubv4.Client
requestDelay int
adjustDelay bool
rateLimit *RateLimit
outputFile string
reposToGet int
reposRetrieved int
delayMutex = &sync.Mutex{}
silent bool
results = make([]UserResult, 0)
)
func adjustDelayTime(rateLimit RateLimit) {
remainingRepos := reposToGet - reposRetrieved
remainingRequests := remainingRepos + remainingRepos/100 + 1
if remainingRequests < rateLimit.Remaining {
requestDelay = 0
} else {
if rateLimit.Remaining == 0 {
handleGraphQLAPIError(nil)
}
untilNextReset := rateLimit.ResetAt.Sub(time.Now()).Milliseconds()
if untilNextReset < 0 {
untilNextReset = time.Hour.Milliseconds()
}
requestDelay = int(untilNextReset)/rateLimit.Remaining + 1
}
}
func doQuery(result *ReposQuery, variables *map[string]interface{}) {
errHandle:
start := time.Now()
err := githubV4Client.Query(context.Background(), result, *variables)
duration := time.Since(start).Milliseconds() - int64(time.Millisecond)
delayMutex.Lock()
rateLimit = &result.RateLimit
if err != nil {
handleGraphQLAPIError(err)
delayMutex.Unlock()
goto errHandle
}
if adjustDelay {
adjustDelayTime(*rateLimit)
}
sleep := int64(requestDelay*rateLimit.Cost)*int64(time.Millisecond) - duration
delayMutex.Unlock()
time.Sleep(time.Duration(sleep))
}
func addRepo(user *UserResult, repo *Repository) {
*user.Repos = append(*user.Repos, RepoResult{
URL: repo.URL,
SshUrl: repo.SshUrl,
PrimaryLanguage: repo.PrimaryLanguage.Name,
})
}
func addUser(user UserResult) {
results = append(results, user)
}
func getRepos(query string, startingDate time.Time, endingDate time.Time, userRes *UserResult) {
var reposQuery ReposQuery
querySplit := strings.Split(query, "created:")
query = strings.Trim(querySplit[0], " ") + " created:" +
startingDate.Format(time.RFC3339) + ".." + endingDate.Format(time.RFC3339)
variables := map[string]interface{}{
"query": githubv4.String(query),
"after": (*githubv4.String)(nil),
}
doQuery(&reposQuery, &variables)
maxRepos := reposQuery.Search.RepositoryCount
if userRes == nil {
reposToGet = maxRepos
if maxRepos > 0 && len(reposQuery.Search.Edges) > 0 {
repos := make([]RepoResult, 0)
userRes = &UserResult{
URL: reposQuery.Search.Edges[0].Node.Repo.Owner.URL,
Repos: &repos,
}
defer addUser(*userRes)
} else {
return
}
}
if maxRepos >= 1000 {
dateDif := endingDate.Sub(startingDate) / 2
getRepos(query, startingDate, startingDate.Add(dateDif), userRes)
getRepos(query, startingDate.Add(dateDif), endingDate, userRes)
return
}
reposCnt := 0
for _, nodeStruct := range reposQuery.Search.Edges {
if nodeStruct.Node.Repo.IsEmpty {
continue
}
addRepo(userRes, &nodeStruct.Node.Repo)
reposCnt++
}
variables = map[string]interface{}{
"query": githubv4.String(query),
"after": githubv4.NewString(reposQuery.Search.PageInfo.EndCursor),
}
for reposCnt < maxRepos {
doQuery(&reposQuery, &variables)
if len(reposQuery.Search.Edges) == 0 {
break
}
for _, nodeStruct := range reposQuery.Search.Edges {
if nodeStruct.Node.Repo.IsEmpty {
continue
}
addRepo(userRes, &nodeStruct.Node.Repo)
reposCnt++
}
variables["after"] = githubv4.NewString(reposQuery.Search.PageInfo.EndCursor)
}
reposRetrieved += reposCnt
}
func handleGraphQLAPIError(err error) {
if err == nil || strings.Contains(err.Error(), "limit exceeded") {
untilNextReset := rateLimit.ResetAt.Sub(time.Now())
if untilNextReset < time.Minute {
rateLimit.ResetAt = time.Now().Add(untilNextReset).Add(time.Hour)
time.Sleep(untilNextReset + 3*time.Second)
return
} else {
writeOutput(outputFile, silent)
fmt.Println("\n" + err.Error())
fmt.Println("Next reset at " + rateLimit.ResetAt.Format(time.RFC1123))
os.Exit(0)
}
}
writeOutput(outputFile, silent)
fmt.Println("\n" + err.Error())
os.Exit(0)
}
func writeOutput(fileName string, silent bool) {
if len(results) == 0 {
return
}
output, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
fmt.Println("Couldn't create output file")
}
defer output.Close()
data, _ := json.MarshalIndent(results, "", " ")
err = ioutil.WriteFile(fileName, data, 0755)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !silent {
fmt.Println(string(data))
}
}
func main() {
token := flag.String("token-string", "", "Github token")
tokenFile := flag.String("token-file", "", "File to read Github token from")
usersFile := flag.String("usernames", "", "File to read usernames from")
flag.StringVar(&outputFile, "o", "", "Output file name")
flag.BoolVar(&silent, "silent", false, "Don't print output to stdout")
flag.IntVar(&requestDelay, "delay", 0, "Time delay after every GraphQL request [ms]")
flag.BoolVar(&adjustDelay, "adjust-delay", false, "Automatically adjust time delay between requests")
flag.Parse()
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
<-signalChannel
fmt.Println("\nProgram interrupted, exiting...")
os.Exit(0)
}()
if (*token == "" && *tokenFile == "") || outputFile == "" {
fmt.Println("Token and output file must be specified!")
os.Exit(1)
}
if *usersFile == "" {
fmt.Println("Usernames must be specified!")
os.Exit(1)
}
githubToken := ""
if *tokenFile != "" {
file, err := os.Open(*tokenFile)
if err != nil {
fmt.Println("Couldn't open file to read token!")
os.Exit(1)
}
defer file.Close()
tokenData, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("Couldn't read from token file!")
os.Exit(1)
}
githubToken = string(tokenData)
} else {
githubToken = *token
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
httpClient = oauth2.NewClient(context.Background(), src)
githubV4Client = githubv4.NewClient(httpClient)
file, err := os.Open(*usersFile)
if err != nil {
fmt.Println("Couldn't open file to read users!")
os.Exit(1)
}
defer file.Close()
userNames := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
userName := strings.TrimSpace(scanner.Text())
userNames = append(userNames, userName)
}
if adjustDelay {
var RLQuery RateLimitQuery
err = githubV4Client.Query(context.Background(), &RLQuery, nil)
if err != nil {
fmt.Println(err)
fmt.Println("Couldn't get initial rate limit!")
os.Exit(1)
}
rateLimit = &RLQuery.RateLimit
if len(userNames) < rateLimit.Remaining {
requestDelay = 0
} else {
if rateLimit.Remaining == 0 {
handleGraphQLAPIError(nil)
}
untilNextReset := rateLimit.ResetAt.Sub(time.Now()).Milliseconds()
if untilNextReset < 0 {
untilNextReset = time.Hour.Milliseconds()
}
requestDelay = int(untilNextReset)/rateLimit.Remaining + 1
}
}
fmt.Println("In progress...")
for _, userName := range userNames {
getRepos("user:"+userName, githubCreateDate, time.Now().UTC(), nil)
}
writeOutput(outputFile, silent)
fmt.Println("Done! " + strconv.Itoa(reposRetrieved) + " repositories found.")
}