-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
149 lines (132 loc) · 3.43 KB
/
github.go
File metadata and controls
149 lines (132 loc) · 3.43 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
var githubClient = &http.Client{Timeout: 10 * time.Second}
const githubGraphQL = "https://api.github.com/graphql"
const prQuery = `
query GetUserPRs($username: String!, $first: Int!, $after: String) {
user(login: $username) {
pullRequests(first: $first, after: $after, orderBy: {field: CREATED_AT, direction: DESC}) {
pageInfo { hasNextPage endCursor }
nodes {
id number title state
createdAt mergedAt closedAt isDraft
repository {
name
owner { login }
stargazerCount
}
url
timelineItems(itemTypes: [CLOSED_EVENT], last: 1) {
nodes {
... on ClosedEvent {
closer { __typename }
}
}
}
}
}
}
}`
type rawPR struct {
ID string `json:"id"`
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
CreatedAt string `json:"createdAt"`
MergedAt *string `json:"mergedAt"`
ClosedAt *string `json:"closedAt"`
IsDraft bool `json:"isDraft"`
Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
StargazerCount int `json:"stargazerCount"`
} `json:"repository"`
URL string `json:"url"`
TimelineItems struct {
Nodes []struct {
Closer *struct {
Typename string `json:"__typename"`
} `json:"closer"`
} `json:"nodes"`
} `json:"timelineItems"`
}
type gqlResponse struct {
Data struct {
User *struct {
PullRequests struct {
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
EndCursor *string `json:"endCursor"`
} `json:"pageInfo"`
Nodes []rawPR `json:"nodes"`
} `json:"pullRequests"`
} `json:"user"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
func fetchAllPRs(token, username string) ([]rawPR, error) {
var all []rawPR
var cursor *string
for {
page, hasNext, next, err := fetchPage(token, username, cursor)
if err != nil {
return nil, err
}
all = append(all, page...)
if !hasNext {
break
}
cursor = next
}
return all, nil
}
func firstN(b []byte, n int) string {
if len(b) <= n {
return string(b)
}
return string(b[:n]) + "..."
}
func fetchPage(token, username string, after *string) ([]rawPR, bool, *string, error) {
body := map[string]any{
"query": prQuery,
"variables": map[string]any{
"username": username,
"first": 100,
"after": after,
},
}
b, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", githubGraphQL, bytes.NewReader(b))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "pullscape")
resp, err := githubClient.Do(req)
if err != nil {
return nil, false, nil, err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var result gqlResponse
if err := json.Unmarshal(data, &result); err != nil {
return nil, false, nil, fmt.Errorf("github returned HTTP %d with non-JSON body: %s", resp.StatusCode, firstN(data, 120))
}
if len(result.Errors) > 0 {
return nil, false, nil, fmt.Errorf("github: %s", result.Errors[0].Message)
}
if result.Data.User == nil {
return nil, false, nil, fmt.Errorf("user %q not found", username)
}
prs := result.Data.User.PullRequests
return prs.Nodes, prs.PageInfo.HasNextPage, prs.PageInfo.EndCursor, nil
}