-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
stats.go
252 lines (228 loc) · 5.38 KB
/
stats.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
package orgstats
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/google/go-github/v39/github"
)
// Stat represents an user adds, rms and commits count
type Stat struct {
Additions, Deletions, Commits, Reviews int
}
// Stats contains the user->Stat mapping
type Stats struct {
data map[string]Stat
since time.Time
}
func (s Stats) Logins() []string {
logins := make([]string, 0, len(s.data))
for login := range s.data {
logins = append(logins, login)
}
return logins
}
func (s Stats) For(login string) Stat {
return s.data[login]
}
// NewStats return a new Stats map
func NewStats(since time.Time) Stats {
return Stats{
data: make(map[string]Stat),
since: since,
}
}
// Gather a given organization's stats
func Gather(
ctx context.Context,
client *github.Client,
org string,
userBlacklist, repoBlacklist []string,
since time.Time,
includeReviewStats bool,
) (Stats, error) {
allStats := NewStats(since)
if err := gatherLineStats(
ctx,
client,
org,
userBlacklist,
repoBlacklist,
&allStats,
); err != nil {
return Stats{}, err
}
log.Println("total authors stats:", len(allStats.data))
if !includeReviewStats {
return allStats, nil
}
for user := range allStats.data {
if err := gatherReviewStats(
ctx,
client,
org,
user,
userBlacklist,
repoBlacklist,
&allStats,
since,
); err != nil {
return Stats{}, err
}
}
return allStats, nil
}
func gatherReviewStats(
ctx context.Context,
client *github.Client,
org, user string,
userBlacklist, repoBlacklist []string,
allStats *Stats,
since time.Time,
) error {
ts := since.Format("2006-01-02")
// review:approved, review:changes_requested
reviewed, err := search(ctx, client, fmt.Sprintf("user:%s is:pr reviewed-by:%s created:>%s", org, user, ts))
if err != nil {
return err
}
allStats.addReviewStats(user, reviewed)
return nil
}
func search(
ctx context.Context,
client *github.Client,
query string,
) (int, error) {
log.Printf("searching '%s'", query)
result, _, err := client.Search.Issues(ctx, query, &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 1,
},
})
if rateErr, ok := err.(*github.RateLimitError); ok {
handleRateLimit(rateErr)
return search(ctx, client, query)
}
if _, ok := err.(*github.AcceptedError); ok {
return search(ctx, client, query)
}
if err != nil {
return 0, fmt.Errorf("failed to search: %s: %w", query, err)
}
return *result.Total, nil
}
func gatherLineStats(
ctx context.Context,
client *github.Client,
org string,
userBlacklist, repoBlacklist []string,
allStats *Stats,
) error {
allRepos, err := repos(ctx, client, org)
if err != nil {
return err
}
for _, repo := range allRepos {
if isBlacklisted(repoBlacklist, repo.GetName()) {
log.Println("ignoring blacklisted repo:", repo.GetName())
continue
}
stats, serr := getStats(ctx, client, org, *repo.Name)
if serr != nil {
return serr
}
for _, cs := range stats {
if isBlacklisted(userBlacklist, cs.Author.GetLogin()) {
log.Println("ignoring blacklisted author:", cs.Author.GetLogin())
continue
}
log.Println("recording stats for author", cs.Author.GetLogin(), "on repo", repo.GetName())
allStats.add(cs)
}
}
return err
}
func isBlacklisted(blacklist []string, s string) bool {
for _, b := range blacklist {
if strings.EqualFold(s, b) {
return true
}
}
return false
}
func (s *Stats) addReviewStats(user string, reviewed int) {
stat := s.data[user]
stat.Reviews += reviewed
s.data[user] = stat
}
func (s *Stats) add(cs *github.ContributorStats) {
if cs.GetAuthor() == nil {
return
}
stat := s.data[cs.GetAuthor().GetLogin()]
var adds int
var rms int
var commits int
for _, week := range cs.Weeks {
if !s.since.IsZero() && week.Week.Time.UTC().Before(s.since) {
continue
}
adds += *week.Additions
rms += *week.Deletions
commits += *week.Commits
}
stat.Additions += adds
stat.Deletions += rms
stat.Commits += commits
if stat.Additions+stat.Deletions+stat.Commits == 0 && !s.since.IsZero() {
// ignore users with no activity when running with a since time
return
}
s.data[cs.GetAuthor().GetLogin()] = stat
}
func repos(ctx context.Context, client *github.Client, org string) ([]*github.Repository, error) {
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, org, opt)
if rateErr, ok := err.(*github.RateLimitError); ok {
handleRateLimit(rateErr)
continue
}
if err != nil {
return allRepos, err
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
log.Println("got", len(allRepos), "repositories")
return allRepos, nil
}
func getStats(ctx context.Context, client *github.Client, org, repo string) ([]*github.ContributorStats, error) {
stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
if err != nil {
if rateErr, ok := err.(*github.RateLimitError); ok {
handleRateLimit(rateErr)
return getStats(ctx, client, org, repo)
}
if _, ok := err.(*github.AcceptedError); ok {
return getStats(ctx, client, org, repo)
}
}
return stats, err
}
func handleRateLimit(err *github.RateLimitError) {
s := err.Rate.Reset.UTC().Sub(time.Now().UTC())
if s < 0 {
s = 5 * time.Second
}
log.Printf("hit rate limit, waiting %v", s)
time.Sleep(s)
}