/
pull_requests.go
55 lines (43 loc) · 1.23 KB
/
pull_requests.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
package github
import (
"context"
"time"
"github.com/jmoiron/sqlx"
"github.com/shurcooL/graphql"
)
func pullRequests(db *sqlx.DB) (limits []rateLimit) {
pullRequests := map[int]time.Time{}
var query struct {
RateLimit rateLimit
Repository struct {
PullRequests struct {
Nodes []struct {
Author struct {
User struct{ DatabaseID int } `graphql:"... on User"`
}
MergedAt time.Time
}
PageInfo pageInfo
} `graphql:"pullRequests(after: $cursor first: 100 states: MERGED)"`
} `graphql:"repository(name: \"code-golf\" owner: \"code-golf\")"`
}
variables := map[string]any{"cursor": (*graphql.String)(nil)}
for {
if err := client.Query(context.Background(), &query, variables); err != nil {
panic(err)
}
limits = append(limits, query.RateLimit)
for _, node := range query.Repository.PullRequests.Nodes {
mergedAt, ok := pullRequests[node.Author.User.DatabaseID]
if !ok || node.MergedAt.Before(mergedAt) {
pullRequests[node.Author.User.DatabaseID] = node.MergedAt
}
}
if !query.Repository.PullRequests.PageInfo.HasNextPage {
break
}
variables["cursor"] = &query.Repository.PullRequests.PageInfo.EndCursor
}
awardCheevos(db, pullRequests, "patches-welcome")
return
}