@@ -2,7 +2,6 @@ package api
22
33import (
44 "fmt"
5- "time"
65)
76
87type PullRequestsPayload struct {
@@ -18,34 +17,28 @@ type PullRequest struct {
1817 URL string
1918 HeadRefName string
2019
21- IsCrossRepository bool
2220 HeadRepositoryOwner struct {
2321 Login string
2422 }
25-
26- Reviews struct {
27- Nodes []struct {
28- State string
29- Author struct {
30- Login string
31- }
23+ HeadRepository struct {
24+ Name string
25+ DefaultBranchRef struct {
26+ Name string
3227 }
3328 }
29+ IsCrossRepository bool
30+ MaintainerCanModify bool
31+
32+ ReviewDecision string
3433
3534 Commits struct {
3635 Nodes []struct {
3736 Commit struct {
38- Status struct {
39- Contexts []struct {
40- State string
41- }
42- }
43- CheckSuites struct {
44- Nodes []struct {
45- CheckRuns struct {
46- Nodes []struct {
47- Conclusion string
48- }
37+ StatusCheckRollup struct {
38+ Contexts struct {
39+ Nodes []struct {
40+ State string
41+ Conclusion string
4942 }
5043 }
5144 }
@@ -64,23 +57,18 @@ func (pr PullRequest) HeadLabel() string {
6457type PullRequestReviewStatus struct {
6558 ChangesRequested bool
6659 Approved bool
60+ ReviewRequired bool
6761}
6862
6963func (pr * PullRequest ) ReviewStatus () PullRequestReviewStatus {
7064 status := PullRequestReviewStatus {}
71- reviewMap := map [string ]string {}
72- // Reviews will include every review on record, including consecutive ones
73- // from the same actor. Consolidate them into latest state per reviewer.
74- for _ , review := range pr .Reviews .Nodes {
75- reviewMap [review .Author .Login ] = review .State
76- }
77- for _ , state := range reviewMap {
78- switch state {
79- case "CHANGES_REQUESTED" :
80- status .ChangesRequested = true
81- case "APPROVED" :
82- status .Approved = true
83- }
65+ switch pr .ReviewDecision {
66+ case "CHANGES_REQUESTED" :
67+ status .ChangesRequested = true
68+ case "APPROVED" :
69+ status .Approved = true
70+ case "REVIEW_REQUIRED" :
71+ status .ReviewRequired = true
8472 }
8573 return status
8674}
@@ -97,32 +85,23 @@ func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
9785 return
9886 }
9987 commit := pr .Commits .Nodes [0 ].Commit
100- for _ , status := range commit .Status .Contexts {
101- switch status .State {
102- case "SUCCESS" :
88+ for _ , c := range commit .StatusCheckRollup .Contexts .Nodes {
89+ state := c .State
90+ if state == "" {
91+ state = c .Conclusion
92+ }
93+ switch state {
94+ case "SUCCESS" , "NEUTRAL" , "SKIPPED" :
10395 summary .Passing ++
104- case "EXPECTED " , "ERROR " , "FAILURE " :
96+ case "ERROR " , "FAILURE " , "CANCELLED" , "TIMED_OUT" , "ACTION_REQUIRED " :
10597 summary .Failing ++
106- case "PENDING" :
98+ case "EXPECTED" , "QUEUED" , " PENDING" , "IN_PROGRESS " :
10799 summary .Pending ++
108100 default :
109- panic (fmt .Errorf ("unsupported status: %q" , status . State ))
101+ panic (fmt .Errorf ("unsupported status: %q" , state ))
110102 }
111103 summary .Total ++
112104 }
113- for _ , checkSuite := range commit .CheckSuites .Nodes {
114- for _ , checkRun := range checkSuite .CheckRuns .Nodes {
115- switch checkRun .Conclusion {
116- case "SUCCESS" , "NEUTRAL" :
117- summary .Passing ++
118- case "FAILURE" , "CANCELLED" , "TIMED_OUT" , "ACTION_REQUIRED" :
119- summary .Failing ++
120- default :
121- panic (fmt .Errorf ("unsupported check conclusion: %q" , checkRun .Conclusion ))
122- }
123- summary .Total ++
124- }
125- }
126105 return
127106}
128107
@@ -131,109 +110,6 @@ type Repo interface {
131110 RepoOwner () string
132111}
133112
134- type IssuesPayload struct {
135- Assigned []Issue
136- Mentioned []Issue
137- Recent []Issue
138- }
139-
140- type Issue struct {
141- Number int
142- Title string
143- URL string
144- }
145-
146- func Issues (client * Client , ghRepo Repo , currentUsername string ) (* IssuesPayload , error ) {
147- type issues struct {
148- Issues struct {
149- Edges []struct {
150- Node Issue
151- }
152- }
153- }
154-
155- type response struct {
156- Assigned issues
157- Mentioned issues
158- Recent issues
159- }
160-
161- query := `
162- fragment issue on Issue {
163- number
164- title
165- }
166- query($owner: String!, $repo: String!, $since: DateTime!, $viewer: String!, $per_page: Int = 10) {
167- assigned: repository(owner: $owner, name: $repo) {
168- issues(filterBy: {assignee: $viewer}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
169- edges {
170- node {
171- ...issue
172- }
173- }
174- }
175- }
176- mentioned: repository(owner: $owner, name: $repo) {
177- issues(filterBy: {mentioned: $viewer}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
178- edges {
179- node {
180- ...issue
181- }
182- }
183- }
184- }
185- recent: repository(owner: $owner, name: $repo) {
186- issues(filterBy: {since: $since}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
187- edges {
188- node {
189- ...issue
190- }
191- }
192- }
193- }
194- }
195- `
196-
197- owner := ghRepo .RepoOwner ()
198- repo := ghRepo .RepoName ()
199- since := time .Now ().UTC ().Add (time .Hour * - 24 ).Format ("2006-01-02T15:04:05-0700" )
200- variables := map [string ]interface {}{
201- "owner" : owner ,
202- "repo" : repo ,
203- "viewer" : currentUsername ,
204- "since" : since ,
205- }
206-
207- var resp response
208- err := client .GraphQL (query , variables , & resp )
209- if err != nil {
210- return nil , err
211- }
212-
213- var assigned []Issue
214- for _ , edge := range resp .Assigned .Issues .Edges {
215- assigned = append (assigned , edge .Node )
216- }
217-
218- var mentioned []Issue
219- for _ , edge := range resp .Mentioned .Issues .Edges {
220- mentioned = append (mentioned , edge .Node )
221- }
222-
223- var recent []Issue
224- for _ , edge := range resp .Recent .Issues .Edges {
225- recent = append (recent , edge .Node )
226- }
227-
228- payload := IssuesPayload {
229- assigned ,
230- mentioned ,
231- recent ,
232- }
233-
234- return & payload , nil
235- }
236-
237113func PullRequests (client * Client , ghRepo Repo , currentBranch , currentUsername string ) (* PullRequestsPayload , error ) {
238114 type edges struct {
239115 Edges []struct {
@@ -267,15 +143,13 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
267143 commits(last: 1) {
268144 nodes {
269145 commit {
270- status {
271- contexts {
272- state
273- }
274- }
275- checkSuites(first: 50) {
276- nodes {
277- checkRuns(first: 50) {
278- nodes {
146+ statusCheckRollup {
147+ contexts(last: 100) {
148+ nodes {
149+ ...on StatusContext {
150+ state
151+ }
152+ ...on CheckRun {
279153 conclusion
280154 }
281155 }
@@ -287,16 +161,8 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
287161 }
288162 fragment prWithReviews on PullRequest {
289163 ...pr
290- reviews(last: 20) {
291- nodes {
292- state
293- author {
294- login
295- }
296- }
297- }
164+ reviewDecision
298165 }
299-
300166 query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
301167 repository(owner: $owner, name: $repo) {
302168 pullRequests(headRefName: $headRefName, states: OPEN, first: 1) {
@@ -374,6 +240,48 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
374240 return & payload , nil
375241}
376242
243+ func PullRequestByNumber (client * Client , ghRepo Repo , number int ) (* PullRequest , error ) {
244+ type response struct {
245+ Repository struct {
246+ PullRequest PullRequest
247+ }
248+ }
249+
250+ query := `
251+ query($owner: String!, $repo: String!, $pr_number: Int!) {
252+ repository(owner: $owner, name: $repo) {
253+ pullRequest(number: $pr_number) {
254+ headRefName
255+ headRepositoryOwner {
256+ login
257+ }
258+ headRepository {
259+ name
260+ defaultBranchRef {
261+ name
262+ }
263+ }
264+ isCrossRepository
265+ maintainerCanModify
266+ }
267+ }
268+ }`
269+
270+ variables := map [string ]interface {}{
271+ "owner" : ghRepo .RepoOwner (),
272+ "repo" : ghRepo .RepoName (),
273+ "pr_number" : number ,
274+ }
275+
276+ var resp response
277+ err := client .GraphQL (query , variables , & resp )
278+ if err != nil {
279+ return nil , err
280+ }
281+
282+ return & resp .Repository .PullRequest , nil
283+ }
284+
377285func PullRequestsForBranch (client * Client , ghRepo Repo , branch string ) ([]PullRequest , error ) {
378286 type response struct {
379287 Repository struct {
0 commit comments