This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
220 lines (189 loc) · 5.56 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
// Copyright (c) 2020, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
"time"
"github.com/shurcooL/githubv4"
"golang.org/x/mod/modfile"
"golang.org/x/oauth2"
)
func main() {
if err := mainErr(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var (
flagCount = flag.Int("count", 100, "number of modules to output")
flagVerbose = flag.Bool("v", false, "print verbose log output")
)
func vlogf(format string, args ...interface{}) {
if *flagVerbose {
log.Printf(format, args...)
}
}
type repository struct {
URL githubv4.URI
StargazerCount githubv4.Int
ForkCount githubv4.Int
DefaultBranchRef struct {
Target struct {
Commit struct {
OID githubv4.String
CommittedDate githubv4.DateTime
} `graphql:"... on Commit"`
}
}
GoModObj struct {
Blob struct {
Text githubv4.String
} `graphql:"... on Blob"`
} `graphql:"object(expression: \"HEAD:go.mod\")"`
}
type repoSearch struct {
Search struct {
RepositoryCount int
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
Nodes []struct {
Repository repository `graphql:"... on Repository"`
}
} `graphql:"search(first: 100, after: $repocursor, type: REPOSITORY, query: $querystring)"`
RateLimit struct {
Cost githubv4.Int
Limit githubv4.Int
Remaining githubv4.Int
ResetAt githubv4.DateTime
}
}
type moduleStats struct {
modulePath string
sourceURL string // e.g. github https URL
version string // e.g. commit ID
score int
}
func mainErr() error {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: corpus [flags]\n\n")
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) > 0 {
flag.Usage() // we don't take any args just yet
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
ctx := context.Background() // TODO: ^C handling, global timeout
oneYearAgo := time.Now().UTC().Add(-time.Hour * 24 * 364) // 364 days
queryString := fmt.Sprintf(`archived:false is:public pushed:>=%s language:go sort:stars`,
oneYearAgo.Format("2006-01-02"))
origQueryString := queryString
computedModulePaths := make(map[string]struct{})
var cursor *githubv4.String
var lastStarCount int
var modules []moduleStats
moreLoop:
for page := 1; ; page++ {
if cursor == nil {
vlogf("querying first page of results for %q", queryString)
} else {
vlogf("%d/%d done; querying page %d with cursor %s", len(modules), *flagCount, page, *cursor)
}
// GraphQL queries can take a few seconds.
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
var query repoSearch
variables := map[string]interface{}{
"repocursor": cursor,
"querystring": githubv4.String(queryString),
}
// When calling GraphQL APIs, there's a chance of hitting rate limits
// or in some cases secondary rate limits. The score of the query above is
// ~101 and here we sleep for 1 second in between the API calls to honor
// the rate limits.
if page > 1 {
time.Sleep(1 * time.Second)
}
if err := client.Query(ctx, &query, variables); err != nil {
return err
}
cancel() // we are in a loop
for _, node := range query.Search.Nodes {
repo := node.Repository
module := moduleStats{
modulePath: modfile.ModulePath([]byte(repo.GoModObj.Blob.Text)),
sourceURL: repo.URL.String(),
}
lastStarCount = int(repo.StargazerCount)
if module.modulePath == "" {
vlogf("no module found in %s; skipping", module.sourceURL)
continue
}
// Skip if module was already computed to avoid duplicated entries
if _, alreadyComputed := computedModulePaths[module.modulePath]; alreadyComputed {
vlogf("module already computed %s; skipping", module.modulePath)
continue
}
computedModulePaths[module.modulePath] = struct{}{}
lastCommit := repo.DefaultBranchRef.Target.Commit
if lastCommit.CommittedDate.Before(oneYearAgo) {
// Unfortunately, github's "pushed" filter is too broad,
// as it includes non-default branches.
vlogf("no recent pushes to %s; skipping", module.sourceURL)
continue
}
module.version = string(lastCommit.OID)
// For now, the score is just the sum of stars and forks.
module.score = int(repo.StargazerCount + repo.ForkCount)
modules = append(modules, module)
if len(modules) >= *flagCount {
break moreLoop
}
}
if !query.Search.PageInfo.HasNextPage {
// GitHub's search is capped at 1k results (sigh) so
// just start over with the star count of the last result.
log.Printf("out of results at %d pages and %d modules; restarting at %d stars", page, len(modules), lastStarCount)
cursor = nil
queryString = fmt.Sprintf("%s stars:<%d", origQueryString, lastStarCount)
} else {
cursor = &query.Search.PageInfo.EndCursor
}
}
// TODO: Since we sort after fetching, and we only sort the query by
// stars while our score also counts forks, we might skew the results
// towards the cutoff. Perhaps query for an extra 20%, sort, then
// discard the tail end.
sort.Slice(modules, func(i, j int) bool {
return modules[i].score > modules[j].score
})
w := csv.NewWriter(os.Stdout)
w.Comma = '\t'
w.Write([]string{"module", "version", "source", "score"})
for _, m := range modules {
w.Write([]string{
m.modulePath,
m.version,
m.sourceURL,
strconv.Itoa(m.score),
})
}
w.Flush()
if err := w.Error(); err != nil {
return err
}
return nil
}