-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.go
354 lines (303 loc) · 8.95 KB
/
leaderboard.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
349
350
351
352
353
354
package main
import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cli/go-gh/pkg/auth"
"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/jedib0t/go-pretty/progress"
"github.com/jedib0t/go-pretty/table"
"github.com/google/go-github/v60/github"
)
var (
amountOfRequests atomic.Uint64
ratelimitRemaining atomic.Uint64
ratelimitReset atomic.Pointer[github.Timestamp]
)
func fetchRepositories(client *github.Client, options *Options, clientNumber int, totalClients int, repos chan *github.Repository) {
page := clientNumber
for i := 0; true; i++ {
opts := github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{
Page: page,
},
}
fetchedRepos, response, err := client.Repositories.ListByOrg(context.Background(), options.Organization, &opts)
if err != nil {
panic(err)
}
for _, fetchedRepo := range fetchedRepos {
repos <- fetchedRepo
}
page = clientNumber + i*totalClients
if page > response.LastPage {
return
}
}
}
func processPullRequest(client *github.Client, options *Options, repo *github.Repository, pr *github.PullRequest, stats chan *Stats) {
stats <- &Stats{
Name: pr.GetUser().GetLogin(),
PullRequests: 1,
}
slog.Debug("Fetching reviews", "repository", repo.GetName(), "pullRequest", "#"+strconv.Itoa(pr.GetNumber()))
reviews, response, err := client.PullRequests.ListReviews(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber(), &github.ListOptions{
PerPage: 100,
})
if err != nil {
panic(err)
}
ratelimitRemaining.Store(uint64(response.Rate.Remaining))
ratelimitReset.Store(&response.Rate.Reset)
if response.NextPage != 0 {
log.Fatalf("Found to many reviews in pull request %s/%s#%d to handle\n", repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber())
}
for _, review := range reviews {
if review.GetSubmittedAt().After(options.Since) {
body := review.GetBody()
lines := len(strings.Split(body, "\n"))
stats <- &Stats{
Name: review.GetUser().GetLogin(),
Reviews: 1,
CommentLinesWritten: lines,
}
}
}
slog.Debug("Fetching comments", "repository", repo.GetName(), "pullRequest", "#"+strconv.Itoa(pr.GetNumber()))
page := 0
for {
comments, response, err := client.PullRequests.ListComments(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber(), &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
Page: page,
},
})
if err != nil {
panic(err)
}
ratelimitRemaining.Store(uint64(response.Rate.Remaining))
ratelimitReset.Store(&response.Rate.Reset)
for _, comment := range comments {
if comment.GetCreatedAt().After(options.Since) {
body := comment.GetBody()
lines := len(strings.Split(body, "\n"))
stats <- &Stats{
Name: comment.GetUser().GetLogin(),
Comments: 1,
CommentLinesWritten: lines,
}
}
}
if response.NextPage == 0 {
return
}
page = response.NextPage
}
}
func processRepository(client *github.Client, pw progress.Writer, options *Options, repo *github.Repository, stats chan *Stats) {
slog.Debug("Fetch pull requests", "repository", repo.GetName())
page := 0
var tracker progress.Tracker
for {
pullRequests, response, err := client.PullRequests.List(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), &github.PullRequestListOptions{
State: "all",
Sort: "updated",
Direction: "desc",
ListOptions: github.ListOptions{
Page: page,
},
})
if err != nil {
panic(err)
}
ratelimitRemaining.Store(uint64(response.Rate.Remaining))
ratelimitReset.Store(&response.Rate.Reset)
if page == 0 {
tracker = progress.Tracker{Message: repo.GetName(), Total: int64(response.LastPage)}
pw.AppendTracker(&tracker)
}
tracker.Increment(1)
var wg sync.WaitGroup
for _, pullRequest := range pullRequests {
if pullRequest.GetUpdatedAt().After(options.Since) {
wg.Add(1)
go func(pr *github.PullRequest) {
defer wg.Done()
processPullRequest(client, options, repo, pr, stats)
}(pullRequest)
}
}
wg.Wait()
if response.NextPage == 0 {
tracker.Increment(1)
tracker.MarkAsDone()
return
}
page = response.NextPage
}
}
type Stats struct {
Name string
PullRequests int
Reviews int
Comments int
CommentLinesWritten int
}
func processRepositories(client *github.Client, pw progress.Writer, options *Options, repos chan *github.Repository, stats chan *Stats) {
fmt.Printf("Processing data since %v matching repository name pattern %s\n", options.Since, options.NamePattern)
var wg sync.WaitGroup
for repo := range repos {
matched, err := regexp.MatchString(options.NamePattern, repo.GetName())
if err != nil {
panic(err)
}
if matched {
wg.Add(1)
go func(repo *github.Repository) {
defer wg.Done()
processRepository(client, pw, options, repo, stats)
}(repo)
}
}
wg.Wait()
close(stats)
}
type CountingRoundTripper struct {
Proxied http.RoundTripper
}
func (crt CountingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error) {
res, e = crt.Proxied.RoundTrip(req)
amountOfRequests.Add(1)
return res, e
}
func createClient() *github.Client {
host, _ := auth.DefaultHost()
authToken, _ := auth.TokenForHost(host)
if authToken == "" {
log.Fatalf("authentication token not found for host %s", host)
}
roundTrip := CountingRoundTripper{http.DefaultTransport}
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(roundTrip)
if err != nil {
panic(err)
}
return github.NewClient(rateLimiter).WithAuthToken(authToken)
}
func accumulateStatsPerUser(stats chan *Stats) map[string]*Stats {
acc := make(map[string]*Stats)
for s := range stats {
userStats := acc[s.Name]
if userStats == nil {
userStats = &Stats{
Name: s.Name,
}
acc[userStats.Name] = userStats
}
userStats.PullRequests += s.PullRequests
userStats.Reviews += s.Reviews
userStats.Comments += s.Comments
userStats.CommentLinesWritten += s.CommentLinesWritten
}
return acc
}
func fetchAllRepositories(client *github.Client, options *Options, repos chan *github.Repository, totalClients int) {
wg := sync.WaitGroup{}
for i := 0; i < totalClients; i++ {
slog.Debug("Spawned repository fetch client", "client", i)
wg.Add(1)
go func(i int) {
defer wg.Done()
fetchRepositories(client, options, i, totalClients, repos)
}(i)
}
wg.Wait()
close(repos)
}
func initializedLogger() {
input := os.Getenv("LOG_LEVEL")
var logLevel slog.Leveler
switch input {
case "DEBUG":
logLevel = slog.LevelDebug
case "WARN":
logLevel = slog.LevelWarn
case "ERROR":
logLevel = slog.LevelError
default:
logLevel = slog.LevelInfo
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}))
slog.SetDefault(logger)
}
type Options struct {
Since time.Time
Organization string
NamePattern string
}
func parseCliArgs() *Options {
oneWeek := time.Duration(24*7) * time.Hour
lastWeek := time.Now().Add(-oneWeek)
sinceDefault := fmt.Sprintf("%d-%02d-%02d", lastWeek.Year(), lastWeek.Month(), lastWeek.Day())
var organization string
var namePattern string
var sinceText string
flag.StringVar(&organization, "org", "", "Github organization to scan")
flag.StringVar(&namePattern, "name", "*", "Regex pattern to match the repository name")
flag.StringVar(&sinceText, "since", sinceDefault, "time span to look into")
flag.Parse()
since, err := time.Parse("2006-01-02", sinceText)
if err != nil {
panic(err)
}
if organization == "" {
fmt.Println("Please provide an Github organization name with --org")
os.Exit(1)
}
return &Options{
Since: since,
Organization: organization,
NamePattern: namePattern,
}
}
func showResults(statsPerUser map[string]*Stats) {
t := table.NewWriter()
t.SetTitle("Code Review Leaderboard")
t.AppendHeader(table.Row{"User", "Pull Requests", "Reviews", "Comments", "#Comment Lines"})
for _, stats := range statsPerUser {
t.AppendRow(table.Row{stats.Name, stats.PullRequests, stats.Reviews, stats.Comments, stats.CommentLinesWritten})
}
fmt.Println()
fmt.Println(t.Render())
}
func main() {
pw := progress.NewWriter()
pw.SetSortBy(progress.SortByPercentDsc)
pw.SetStyle(progress.StyleDefault)
pw.Style().Colors = progress.StyleColorsDefault
go pw.Render()
initializedLogger()
options := parseCliArgs()
client := createClient()
repos := make(chan *github.Repository, 128)
go fetchAllRepositories(client, options, repos, 5)
stats := make(chan *Stats, 128)
go processRepositories(client, pw, options, repos, stats)
accumulated := accumulateStatsPerUser(stats)
// wait until progress bar rendering is done
for pw.LengthActive() != 0 {
}
fmt.Printf("%d requests sent to Github\n", amountOfRequests.Load())
fmt.Printf("Rate Limit: remaining=%d reset=%v\n", ratelimitRemaining.Load(), ratelimitReset.Load())
showResults(accumulated)
}