This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
forked from spencerkimball/repo-digest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
471 lines (434 loc) · 12.9 KB
/
query.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball ([email protected])
package main
import (
"fmt"
"log"
"path"
"regexp"
"sort"
"strconv"
"time"
)
// TODO(spencer): combine this code with the code in stargazers
// for a single utility.
const (
// tinyPR threshold of additions and deletions.
tinyPR = 20
// smallPR threshold of additions and deletions.
smallPR = 100
// mediumPR threshold of additions and deletions.
mediumPR = 500
// largePR threshold of additions and deletions.
largePR = 1000
)
var ignoreRegexp = []*regexp.Regexp{
regexp.MustCompile(`.*\.pb\.(go|cc|h)`),
regexp.MustCompile(`.*\.css`),
}
func skipFile(f string) bool {
for _, ire := range ignoreRegexp {
if ire.MatchString(f) {
return true
}
}
return false
}
type User struct {
Login string `json:"login"`
ID int `json:"id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HtmlURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
Name string `json:"name"`
Company string `json:"company"`
Blog string `json:"blog"`
Location string `json:"location"`
Email string `json:"email"`
Hireable bool `json:"hireable"`
Bio string `json:"bio"`
PublicRepos int `json:"public_repos"`
PublicGists int `json:"public_gists"`
Followers int `json:"followers"`
Following int `json:"following"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
//GistsURL string `json:"gists_url"`
//OrganizationsURL string `json:"organizations_url"`
//ReposURL string `json:"repos_url"`
//EventsURL string `json:"events_url"`
//ReceivedEventsURL string `json:"received_events_url"`
}
type File struct {
SHA string `json:"sha"`
Filename string `json:"filename"`
Status string `json:"status"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
Changes int `json:"changes"`
BlobURL string `json:"blob_url"`
RawURL string `json:"raw_url"`
ContentsURL string `json:"contents_url"`
Patch string `json:"patch"`
}
// Subdirectory holds name of a subdirectory and the count of changes
// to files it contains.
type Subdirectory struct {
Name string
Files []*File
}
// TotalChanges returns the total of additions and deletions made to
// files within the subdirectory.
func (sd *Subdirectory) TotalChanges() int {
total := 0
for _, f := range sd.Files {
total += f.Changes
}
return total
}
func (sd *Subdirectory) TotalChangesStr() string {
return format(sd.TotalChanges())
}
type Subdirectories []*Subdirectory
func (slice Subdirectories) Len() int {
return len(slice)
}
func (slice Subdirectories) Less(i, j int) bool {
return slice[i].TotalChanges() > slice[j].TotalChanges()
}
func (slice Subdirectories) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type PullRequest struct {
Repo string // not fetched from API
URL string `json:"url"`
ID int `json:"id"`
HtmlURL string `json:"html_url"`
DiffURL string `json:"diff_url"`
PatchURL string `json:"patch_url"`
IssueURL string `json:"issue_url"`
Number int `json:"number"`
State string `json:"state"`
Locked bool `json:"locked"`
Title string `json:"title"`
User User `json:"user"`
Body string `json:"body"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ClosedAt string `json:"closed_at"`
MergedAt string `json:"merged_at"`
MergeCommitSHA string `json:"merge_commit_sha"`
Assignee User `json:"assignee"`
CommitsURL string `json:"commits_url"`
Review_commentsURL string `json:"review_comments_url"`
Review_commentURL string `json:"review_comment_url"`
CommentsURL string `json:"comments_url"`
StatusesURL string `json:"statuses_url"`
Merged bool `json:"merged"`
Mergeable bool `json:"mergeable"`
MergeableState string `json:"mergeable_state"`
MergedBy User `json:"merged_by"`
Comments int `json:"comments"`
ReviewComments int `json:"review_comments"`
Commits int `json:"commits"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
ChangedFiles int `json:"changed_files"`
CommitMessages []struct {
Commit struct {
Message string `json:"message"`
URL string `json:"url"`
} `json:"commit"`
}
Files []*File `json:"-"`
}
// TotalChanges returns total of additions and deletions.
func (pr *PullRequest) TotalChanges() int {
total := 0
for _, f := range pr.Files {
total += f.Changes
}
return total
}
func (pr *PullRequest) AdditionsStr() string {
return format(pr.Additions)
}
func (pr *PullRequest) DeletionsStr() string {
return format(pr.Deletions)
}
func (pr *PullRequest) CommentsStr() string {
return format(pr.Comments)
}
// Subdirectories returns a sorted slice of subdirectories which include
// changed files, sorted by number of changes. Only the subdirectories
// which comprise <=80% of the total changes are returned.
func (pr *PullRequest) Subdirectories() []*Subdirectory {
subdirs := map[string]*Subdirectory{}
sds := []*Subdirectory{}
for _, f := range pr.Files {
dir := path.Dir(f.Filename)
if len(dir) == 0 {
dir = "/"
}
if _, ok := subdirs[dir]; !ok {
sd := &Subdirectory{Name: dir}
sds = append(sds, sd)
subdirs[dir] = sd
}
subdirs[dir].Files = append(subdirs[dir].Files, f)
}
sort.Sort(Subdirectories(sds))
total := pr.TotalChanges()
count := 0
for i, sd := range sds {
count += sd.TotalChanges()
if float64(count)/float64(total) > 0.80 {
// Truncate the sds array to ignore uninteresting subdirectories.
sds = sds[:i+1]
break
}
}
return sds
}
// Class returns one of "tiny", "small", "medium" or "large" depending
// on the total number of changes in the pull request.
func (pr *PullRequest) Class() string {
if tc := pr.TotalChanges(); tc < tinyPR {
return "●"
} else if tc < smallPR {
return "●●"
} else if tc < mediumPR {
return "●●●"
} else if tc < largePR {
return "●●●●"
}
return "●●●●●"
}
// CreatedAtStr returns created at timestap in human-readable format
// according to server-local time.
func (pr *PullRequest) CreatedAtStr() string {
t, err := time.Parse(time.RFC3339, pr.CreatedAt)
if err != nil {
return pr.CreatedAt
}
return t.Local().Format("Mon Jan _2 15:04:05")
}
// ClosedAtStr returns closed at timestap in human-readable format
// according to server-local time.
func (pr *PullRequest) ClosedAtStr() string {
t, err := time.Parse(time.RFC3339, pr.ClosedAt)
if err != nil {
return pr.ClosedAt
}
return t.Local().Format("Mon Jan _2 15:04:05")
}
// Queries pull requests for the repository. Returns a slice each for
// open and closed pull requests.
func Query(c *Config) (open, closed []*PullRequest, err error) {
for _, repo := range c.Repos {
var os []*PullRequest
var cs []*PullRequest
os, cs, err = QueryPullRequests(c, repo)
if err != nil {
return nil, nil, err
}
open = append(open, os...)
closed = append(closed, cs...)
}
if err = QueryDetailedPullRequests(c, open); err != nil {
return nil, nil, err
}
if err = QueryDetailedPullRequests(c, closed); err != nil {
return nil, nil, err
}
return open, closed, nil
}
// QueryPullRequests queries all pull requests from the repo or a
// day's worth, whichever is greater.
func QueryPullRequests(c *Config, repo string) ([]*PullRequest, []*PullRequest, error) {
log.Printf("querying pull requests from %s opened or closed after %s\n", repo, c.FetchSince.Format(time.RFC3339))
url := fmt.Sprintf("%srepos/%s/pulls?state=all&sort=updated&direction=desc", c.Host, repo)
open, closed := []*PullRequest{}, []*PullRequest{}
total := 0
var err error
var done bool
fmt.Println("*** 0 open 0 closed, 0 total pull requests")
for len(url) > 0 && !done {
fetched := []*PullRequest{}
url, err = fetchURL(c, url, &fetched)
if err != nil {
return nil, nil, err
}
total += len(fetched)
for _, pr := range fetched {
// Break out of loop if updated timestamp is <= FetchSince.
t, err := time.Parse(time.RFC3339, pr.UpdatedAt)
if err != nil {
return nil, nil, err
}
if !c.FetchSince.Before(t) {
done = true
break
}
var date string
switch pr.State {
case "open":
date = pr.CreatedAt
case "closed":
// Ignore unmerged PRs.
if pr.MergedAt == "" {
continue
}
date = pr.ClosedAt
default:
continue
}
t, err = time.Parse(time.RFC3339, date)
if err != nil {
return nil, nil, err
}
if pr.State == "open" {
if c.FetchSince.Before(t) {
open = append(open, pr)
}
} else {
if c.FetchSince.Before(t) {
closed = append(closed, pr)
}
}
fmt.Printf("\r*** %s open %s closed %s total pull requests\n", format(len(open)), format(len(closed)), format(total))
}
}
fmt.Printf("\n")
for i := range open {
open[i].Repo = repo
}
for i := range closed {
closed[i].Repo = repo
}
return open, closed, nil
}
// QueryDetailedPullRequests queries detailed info on each pull request
// in the provided slice.
func QueryDetailedPullRequests(c *Config, prs []*PullRequest) error {
log.Printf("querying detailed info for each of %s pull requests...\n", format(len(prs)))
fmt.Println("*** detailed info for 0 pull requests")
for i, pr := range prs {
// Fetch detailed pull request info.
if _, err := fetchURL(c, pr.URL, pr); err != nil {
return err
}
// Fetch commit messages.
if _, err := fetchURL(c, pr.URL+"/commits", &pr.CommitMessages); err != nil {
return err
}
// Fetch files changed by pull request.
if _, err := fetchURL(c, pr.URL+"/files", &pr.Files); err != nil {
return err
}
// Remove files we're supposed to ignore.
newFiles := []*File{}
for _, f := range pr.Files {
if !skipFile(f.Filename) {
newFiles = append(newFiles, f)
}
}
pr.Files = newFiles
fmt.Printf("\r*** detailed info for %s pull requests\n", format(i+1))
}
fmt.Printf("\n")
return nil
}
func CountMonthly(c *Config) ([]int, error) {
var counts []int
for t := c.Now; !t.Before(c.FetchSince); {
counts = append(counts, 0)
t = t.AddDate(0, -1, 0)
}
for _, repo := range c.Repos {
if err := CountMonthlyPullRequests(c, repo, counts); err != nil {
return nil, err
}
}
return counts, nil
}
// CountMonthlyPullRequests queries all pull requests by created data
// and adds counts to the specified counts slice by month.
func CountMonthlyPullRequests(c *Config, repo string, counts []int) error {
log.Printf("counting monthly pull requests from %s after %s", repo, c.FetchSince.Format(time.RFC3339))
url := fmt.Sprintf("%srepos/%s/pulls?state=all&sort=created&direction=desc", c.Host, repo)
var idx int
var monthTotal int
t := c.Now
nextT := t.AddDate(0, -1, 0)
fillCounts := func(prT time.Time) {
for prT.Before(nextT) {
counts[idx] += monthTotal
idx++
monthTotal = 0
t = nextT
nextT = t.AddDate(0, -1, 0)
}
}
for done := false; len(url) > 0 && !done; {
fetched := []*PullRequest{}
var err error
url, err = fetchURL(c, url, &fetched)
if err != nil {
return err
}
for _, pr := range fetched {
prT, err := time.Parse(time.RFC3339, pr.CreatedAt)
if err != nil {
return err
}
// Break out of loop if updated timestamp is <= FetchSince.
if !c.FetchSince.Before(prT) {
done = true
break
}
fillCounts(prT)
monthTotal++
}
}
fillCounts(c.FetchSince)
counts[idx] += monthTotal
return nil
}
func format(n int) string {
in := strconv.FormatInt(int64(n), 10)
out := make([]byte, len(in)+(len(in)-2+int(in[0]/'0'))/3)
if in[0] == '-' {
in, out[0] = in[1:], '-'
}
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 {
out[j] = in[i]
if i == 0 {
return string(out)
}
if k++; k == 3 {
j, k = j-1, 0
out[j] = ','
}
}
}