-
Notifications
You must be signed in to change notification settings - Fork 3
/
prs.go
76 lines (64 loc) · 2.09 KB
/
prs.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
package main
import (
"context"
"time"
"github.com/shurcooL/githubv4"
)
func getPrFromRepo(client Client, org, repo string) ([]PullRequest, error) {
var repoQuery struct {
Repository struct {
PullRequests struct {
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
Edges []PullRequestEdge
} `graphql:"pullRequests(first: 100, states: $states, after: $cursor)"`
} `graphql:"repository(name: $repo, owner: $org)"`
}
states := []githubv4.PullRequestState{
githubv4.PullRequestStateOpen,
}
var PRS []PullRequest
variables := map[string]interface{}{
"org": githubv4.String(org),
"cursor": (*githubv4.String)(nil), // Null after argument to get first page.
"states": states,
"repo": githubv4.String(repo),
}
for {
err := client.Query(context.Background(), &repoQuery, variables)
if err != nil {
return PRS, err
}
// The three dots ... is called the ellipsis or "unpacking" operator.
// It allows a slice to be expanded in place and passed as individual arguments to a variadic function like append().
// The verbose equivalent would look like
// prsToAdd := extractPRDataFromEdges(repoQuery.Repository.PullRequests.Edges)
// PRS = append(PRS, prsToAdd[0], prsToAdd[1], ..., prsToAdd[N])
PRS = append(PRS, extractPRDataFromEdges(repoQuery.Repository.PullRequests.Edges)...)
if !repoQuery.Repository.PullRequests.PageInfo.HasNextPage {
break
} else {
variables["cursor"] = githubv4.NewString(repoQuery.Repository.PullRequests.PageInfo.EndCursor)
}
// Sleep for at least a second. https://docs.github.com/en/rest/guides/best-practices-for-integrators
time.Sleep(2 * time.Second)
}
return PRS, nil
}
func extractPRDataFromEdges(edges []PullRequestEdge) []PullRequest {
var PRS []PullRequest
for _, edge := range edges {
if !bool(edge.Node.IsDraft) {
var pr PullRequest
pr.URL = edge.Node.URL.String()
pr.CreatedAt = edge.Node.CreatedAt
pr.Owner = edge.Node.Author.Login
pr.Mergeable = edge.Node.Mergeable
pr.ReviewDecision = edge.Node.ReviewDecision
PRS = append(PRS, pr)
}
}
return PRS
}