-
Notifications
You must be signed in to change notification settings - Fork 204
/
prapi.go
267 lines (235 loc) Β· 5.59 KB
/
prapi.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package data
import (
"fmt"
"net/url"
"time"
"github.com/charmbracelet/log"
gh "github.com/cli/go-gh/v2/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
"github.com/shurcooL/githubv4"
)
type PullRequestData struct {
Number int
Title string
Body string
Author struct {
Login string
}
UpdatedAt time.Time
Url string
State string
Mergeable string
ReviewDecision string
Additions int
Deletions int
HeadRefName string
BaseRefName string
HeadRepository struct {
Name string
}
HeadRef struct {
Name string
}
Repository Repository
Assignees Assignees `graphql:"assignees(first: 3)"`
Comments Comments `graphql:"comments(last: 5, orderBy: { field: UPDATED_AT, direction: DESC })"`
LatestReviews Reviews `graphql:"latestReviews(last: 3)"`
ReviewThreads ReviewThreads `graphql:"reviewThreads(last: 20)"`
IsDraft bool
Commits Commits `graphql:"commits(last: 1)"`
Labels PRLabels `graphql:"labels(first: 3)"`
}
type CheckRun struct {
Name graphql.String
Status graphql.String
Conclusion graphql.String
CheckSuite struct {
Creator struct {
Login graphql.String
}
WorkflowRun struct {
Workflow struct {
Name graphql.String
}
}
}
}
type StatusContext struct {
Context graphql.String
State graphql.String
Creator struct {
Login graphql.String
}
}
type Commits struct {
Nodes []struct {
Commit struct {
Deployments struct {
Nodes []struct {
Task graphql.String
Description graphql.String
}
} `graphql:"deployments(last: 10)"`
StatusCheckRollup struct {
Contexts struct {
TotalCount graphql.Int
Nodes []struct {
Typename graphql.String `graphql:"__typename"`
CheckRun CheckRun `graphql:"... on CheckRun"`
StatusContext StatusContext `graphql:"... on StatusContext"`
}
} `graphql:"contexts(last: 20)"`
}
}
}
}
type Comment struct {
Author struct {
Login string
}
Body string
UpdatedAt time.Time
}
type ReviewComment struct {
Author struct {
Login string
}
Body string
UpdatedAt time.Time
StartLine int
Line int
}
type ReviewComments struct {
Nodes []ReviewComment
TotalCount int
}
type Comments struct {
Nodes []Comment
TotalCount int
}
type Review struct {
Author struct {
Login string
}
Body string
State string
UpdatedAt time.Time
}
type Reviews struct {
Nodes []Review
}
type ReviewThreads struct {
Nodes []struct {
Id string
IsOutdated bool
OriginalLine int
StartLine int
Line int
Path string
Comments ReviewComments `graphql:"comments(first: 10)"`
}
}
type PRLabel struct {
Color string
Name string
}
type PRLabels struct {
Nodes []Label
}
type PageInfo struct {
HasNextPage bool
StartCursor string
EndCursor string
}
func (data PullRequestData) GetTitle() string {
return data.Title
}
func (data PullRequestData) GetRepoNameWithOwner() string {
return data.Repository.NameWithOwner
}
func (data PullRequestData) GetNumber() int {
return data.Number
}
func (data PullRequestData) GetUrl() string {
return data.Url
}
func (data PullRequestData) GetUpdatedAt() time.Time {
return data.UpdatedAt
}
func makePullRequestsQuery(query string) string {
return fmt.Sprintf("is:pr %s sort:updated", query)
}
type PullRequestsResponse struct {
Prs []PullRequestData
TotalCount int
PageInfo PageInfo
}
func FetchPullRequests(query string, limit int, pageInfo *PageInfo) (PullRequestsResponse, error) {
var err error
client, err := gh.DefaultGraphQLClient()
if err != nil {
return PullRequestsResponse{}, err
}
var queryResult struct {
Search struct {
Nodes []struct {
PullRequest PullRequestData `graphql:"... on PullRequest"`
}
IssueCount int
PageInfo PageInfo
} `graphql:"search(type: ISSUE, first: $limit, after: $endCursor, query: $query)"`
}
var endCursor *string
if pageInfo != nil {
endCursor = &pageInfo.EndCursor
}
variables := map[string]interface{}{
"query": graphql.String(makePullRequestsQuery(query)),
"limit": graphql.Int(limit),
"endCursor": (*graphql.String)(endCursor),
}
log.Debug("Fetching PRs", "query", query, "limit", limit, "endCursor", endCursor)
err = client.Query("SearchPullRequests", &queryResult, variables)
if err != nil {
return PullRequestsResponse{}, err
}
log.Debug("Successfully fetched PRs", "query", query, "count", queryResult.Search.IssueCount)
prs := make([]PullRequestData, 0, len(queryResult.Search.Nodes))
for _, node := range queryResult.Search.Nodes {
if node.PullRequest.Repository.IsArchived {
continue
}
prs = append(prs, node.PullRequest)
}
return PullRequestsResponse{
Prs: prs,
TotalCount: queryResult.Search.IssueCount,
PageInfo: queryResult.Search.PageInfo,
}, nil
}
func FetchPullRequest(prUrl string) (PullRequestData, error) {
var err error
client, err := gh.DefaultGraphQLClient()
if err != nil {
return PullRequestData{}, err
}
var queryResult struct {
Resource struct {
PullRequest PullRequestData `graphql:"... on PullRequest"`
} `graphql:"resource(url: $url)"`
}
parsedUrl, err := url.Parse(prUrl)
if err != nil {
return PullRequestData{}, err
}
variables := map[string]interface{}{
"url": githubv4.URI{URL: parsedUrl},
}
log.Debug("Fetching PR", "url", prUrl)
err = client.Query("FetchPullRequest", &queryResult, variables)
if err != nil {
return PullRequestData{}, err
}
log.Debug("Successfully fetched PR", "url", prUrl, "data", queryResult.Resource.PullRequest)
return queryResult.Resource.PullRequest, nil
}