forked from tecos-nitt/Tecos-Hacktoberfest-Leaderboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
148 lines (138 loc) · 3.53 KB
/
collector.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
package main
import (
"bufio"
"context"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/google/go-github/v32/github"
"github.com/gregjones/httpcache"
"github.com/hashicorp/go-retryablehttp"
"golang.org/x/oauth2"
)
var reUsername = regexp.MustCompile(`^\* .*@([a-zA-Z0-9][\-a-zA-Z0-9]*[a-zA-Z0-9]).*$`)
func searchQuery(usernames []string, eventDate string) string {
b := strings.Builder{}
for _, u := range usernames {
b.WriteString("author:")
b.WriteString(u)
b.WriteString(" ")
}
b.WriteString("created:")
b.WriteString(eventDate)
return b.String()
}
type StatsCollector struct {
ParticipantFile string
EventDate string
GitHub *github.Client
}
func NewStatsCollector(eventDate, playerFile string) *StatsCollector {
var tksrc oauth2.TokenSource
if GH_TOKEN != "" {
tksrc = oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: GH_TOKEN,
})
}
cl := oauth2.NewClient(context.Background(), tksrc)
cachedTransport := httpcache.NewMemoryCacheTransport()
cachedTransport.Transport = cl.Transport
retryCl := retryablehttp.NewClient()
retryCl.Logger = nil
retryCl.HTTPClient = cachedTransport.Client()
gh := github.NewClient(retryCl.StandardClient())
return &StatsCollector{
ParticipantFile: playerFile,
EventDate: eventDate,
GitHub: gh,
}
}
func (sc *StatsCollector) PlayerNames(ctx context.Context) ([]string, error) {
req, err := retryablehttp.NewRequest(http.MethodGet, sc.ParticipantFile, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res, err := retryablehttp.NewClient().Do(req)
if err != nil {
return nil, err
}
names := []string{}
r := bufio.NewScanner(res.Body)
for r.Scan() {
line := strings.TrimSpace(r.Text())
m := reUsername.FindStringSubmatch(line)
if len(m) == 0 || m[1] == "username" {
continue
}
names = append(names, m[1])
}
return names, nil
}
func (sc *StatsCollector) Players(ctx context.Context) (Players, error) {
usernames, err := sc.PlayerNames(ctx)
if err != nil {
return nil, err
}
return sc.queryUserData(ctx, usernames...)
}
func (sc *StatsCollector) queryUserData(ctx context.Context, usernames ...string) (Players, error) {
players := Players{}
for _, u := range usernames {
user, _, err := sc.GitHub.Users.Get(ctx, u)
if err != nil {
fmt.Printf("[WARN] Cannot fetch user %s: %s\n", u, err)
continue
}
players[user.GetLogin()] = NewPlayer(user)
}
query := searchQuery(usernames, sc.EventDate)
fmt.Println("Search query:", query)
opt := github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
Page: 0,
},
}
repos := map[string]*Repo{}
for {
res, r, err := sc.GitHub.Search.Issues(ctx, query, &opt)
if err != nil {
return players, err
}
opt.ListOptions.Page = r.NextPage
for _, issue := range res.Issues {
p := players[issue.User.GetLogin()]
if p == nil { // Skip if player does not exist
continue
}
repo, ok := repos[issue.GetRepositoryURL()]
if !ok {
var err error
repo, err = NewRepoFromURL(ctx, sc.GitHub, issue.GetRepositoryURL())
if err != nil {
return players, err
}
repos[issue.GetRepositoryURL()] = repo
}
if issue.IsPullRequest() {
pullreq, _, err := sc.GitHub.PullRequests.Get(ctx, repo.Owner, repo.Name, issue.GetNumber())
if err != nil {
return nil, err
}
pr, err := NewPullRequest(pullreq, issue, repo)
if err != nil {
return players, err
}
p.AddContrib(pr)
} else {
p.AddIssue(NewIssue(issue, repo))
}
}
if r.NextPage == 0 {
break
}
}
return players, nil
}