@@ -17,6 +17,99 @@ type PullRequest struct {
1717 State string
1818 URL string
1919 HeadRefName string
20+ Reviews struct {
21+ Nodes []struct {
22+ State string
23+ Author struct {
24+ Login string
25+ }
26+ }
27+ }
28+ Commits struct {
29+ Nodes []struct {
30+ Commit struct {
31+ Status struct {
32+ Contexts []struct {
33+ State string
34+ }
35+ }
36+ CheckSuites struct {
37+ Nodes []struct {
38+ CheckRuns struct {
39+ Nodes []struct {
40+ Conclusion string
41+ }
42+ }
43+ }
44+ }
45+ }
46+ }
47+ }
48+ }
49+
50+ type PullRequestReviewStatus struct {
51+ ChangesRequested bool
52+ Approved bool
53+ }
54+
55+ func (pr * PullRequest ) ReviewStatus () PullRequestReviewStatus {
56+ status := PullRequestReviewStatus {}
57+ reviewMap := map [string ]string {}
58+ // Reviews will include every review on record, including consecutive ones
59+ // from the same actor. Consolidate them into latest state per reviewer.
60+ for _ , review := range pr .Reviews .Nodes {
61+ reviewMap [review .Author .Login ] = review .State
62+ }
63+ for _ , state := range reviewMap {
64+ switch state {
65+ case "CHANGES_REQUESTED" :
66+ status .ChangesRequested = true
67+ case "APPROVED" :
68+ status .Approved = true
69+ }
70+ }
71+ return status
72+ }
73+
74+ type PullRequestChecksStatus struct {
75+ Pending int
76+ Failing int
77+ Passing int
78+ Total int
79+ }
80+
81+ func (pr * PullRequest ) ChecksStatus () (summary PullRequestChecksStatus ) {
82+ if len (pr .Commits .Nodes ) == 0 {
83+ return
84+ }
85+ commit := pr .Commits .Nodes [0 ].Commit
86+ for _ , status := range commit .Status .Contexts {
87+ switch status .State {
88+ case "SUCCESS" :
89+ summary .Passing ++
90+ case "EXPECTED" , "ERROR" , "FAILURE" :
91+ summary .Failing ++
92+ case "PENDING" :
93+ summary .Pending ++
94+ default :
95+ panic (fmt .Errorf ("unsupported status: %q" , status .State ))
96+ }
97+ summary .Total ++
98+ }
99+ for _ , checkSuite := range commit .CheckSuites .Nodes {
100+ for _ , checkRun := range checkSuite .CheckRuns .Nodes {
101+ switch checkRun .Conclusion {
102+ case "SUCCESS" , "NEUTRAL" :
103+ summary .Passing ++
104+ case "FAILURE" , "CANCELLED" , "TIMED_OUT" , "ACTION_REQUIRED" :
105+ summary .Failing ++
106+ default :
107+ panic (fmt .Errorf ("unsupported check conclusion: %q" , checkRun .Conclusion ))
108+ }
109+ summary .Total ++
110+ }
111+ }
112+ return
20113}
21114
22115type Repo interface {
@@ -147,27 +240,58 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
147240 }
148241
149242 query := `
150- fragment pr on PullRequest {
151- number
152- title
153- url
154- headRefName
155- }
243+ fragment pr on PullRequest {
244+ number
245+ title
246+ url
247+ headRefName
248+ commits(last: 1) {
249+ nodes {
250+ commit {
251+ status {
252+ contexts {
253+ state
254+ }
255+ }
256+ checkSuites(first: 50) {
257+ nodes {
258+ checkRuns(first: 50) {
259+ nodes {
260+ conclusion
261+ }
262+ }
263+ }
264+ }
265+ }
266+ }
267+ }
268+ }
269+ fragment prWithReviews on PullRequest {
270+ ...pr
271+ reviews(last: 20) {
272+ nodes {
273+ state
274+ author {
275+ login
276+ }
277+ }
278+ }
279+ }
156280
157281 query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
158282 repository(owner: $owner, name: $repo) {
159283 pullRequests(headRefName: $headRefName, states: OPEN, first: 1) {
160284 edges {
161285 node {
162- ...pr
286+ ...prWithReviews
163287 }
164288 }
165289 }
166290 }
167291 viewerCreated: search(query: $viewerQuery, type: ISSUE, first: $per_page) {
168292 edges {
169293 node {
170- ...pr
294+ ...prWithReviews
171295 }
172296 }
173297 pageInfo {
0 commit comments