@@ -2,6 +2,7 @@ package api
22
33import (
44 "fmt"
5+ "strings"
56)
67
78type PullRequestsPayload struct {
@@ -141,7 +142,6 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
141142 title
142143 url
143144 headRefName
144- headRefName
145145 headRepositoryOwner {
146146 login
147147 }
@@ -172,7 +172,7 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
172172 }
173173 query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
174174 repository(owner: $owner, name: $repo) {
175- pullRequests(headRefName: $headRefName, states: OPEN, first: 1 ) {
175+ pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page ) {
176176 edges {
177177 node {
178178 ...prWithReviews
@@ -209,12 +209,17 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
209209 viewerQuery := fmt .Sprintf ("repo:%s/%s state:open is:pr author:%s" , owner , repo , currentUsername )
210210 reviewerQuery := fmt .Sprintf ("repo:%s/%s state:open review-requested:%s" , owner , repo , currentUsername )
211211
212+ branchWithoutOwner := currentBranch
213+ if idx := strings .Index (currentBranch , ":" ); idx >= 0 {
214+ branchWithoutOwner = currentBranch [idx + 1 :]
215+ }
216+
212217 variables := map [string ]interface {}{
213218 "viewerQuery" : viewerQuery ,
214219 "reviewerQuery" : reviewerQuery ,
215220 "owner" : owner ,
216221 "repo" : repo ,
217- "headRefName" : currentBranch ,
222+ "headRefName" : branchWithoutOwner ,
218223 }
219224
220225 var resp response
@@ -235,7 +240,9 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
235240
236241 var currentPR * PullRequest
237242 for _ , edge := range resp .Repository .PullRequests .Edges {
238- currentPR = & edge .Node
243+ if edge .Node .HeadLabel () == currentBranch {
244+ currentPR = & edge .Node
245+ }
239246 }
240247
241248 payload := PullRequestsPayload {
@@ -289,36 +296,42 @@ func PullRequestByNumber(client *Client, ghRepo Repo, number int) (*PullRequest,
289296 return & resp .Repository .PullRequest , nil
290297}
291298
292- func PullRequestsForBranch (client * Client , ghRepo Repo , branch string ) ([] PullRequest , error ) {
299+ func PullRequestForBranch (client * Client , ghRepo Repo , branch string ) (* PullRequest , error ) {
293300 type response struct {
294301 Repository struct {
295302 PullRequests struct {
296- Edges []struct {
297- Node PullRequest
298- }
303+ Nodes []PullRequest
299304 }
300305 }
301306 }
302307
303308 query := `
304- query($owner: String!, $repo: String!, $headRefName: String!) {
305- repository(owner: $owner, name: $repo) {
306- pullRequests(headRefName: $headRefName, states: OPEN, first: 1) {
307- edges {
308- node {
309- number
310- title
311- url
312- }
313- }
314- }
315- }
316- }`
309+ query($owner: String!, $repo: String!, $headRefName: String!) {
310+ repository(owner: $owner, name: $repo) {
311+ pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
312+ nodes {
313+ number
314+ title
315+ url
316+ headRefName
317+ headRepositoryOwner {
318+ login
319+ }
320+ isCrossRepository
321+ }
322+ }
323+ }
324+ }`
325+
326+ branchWithoutOwner := branch
327+ if idx := strings .Index (branch , ":" ); idx >= 0 {
328+ branchWithoutOwner = branch [idx + 1 :]
329+ }
317330
318331 variables := map [string ]interface {}{
319332 "owner" : ghRepo .RepoOwner (),
320333 "repo" : ghRepo .RepoName (),
321- "headRefName" : branch ,
334+ "headRefName" : branchWithoutOwner ,
322335 }
323336
324337 var resp response
@@ -327,12 +340,13 @@ func PullRequestsForBranch(client *Client, ghRepo Repo, branch string) ([]PullRe
327340 return nil , err
328341 }
329342
330- prs := []PullRequest {}
331- for _ , edge := range resp .Repository .PullRequests .Edges {
332- prs = append (prs , edge .Node )
343+ for _ , pr := range resp .Repository .PullRequests .Nodes {
344+ if pr .HeadLabel () == branch {
345+ return & pr , nil
346+ }
333347 }
334348
335- return prs , nil
349+ return nil , fmt . Errorf ( "no open pull requests found for branch %q" , branch )
336350}
337351
338352func CreatePullRequest (client * Client , ghRepo Repo , params map [string ]interface {}) (* PullRequest , error ) {
0 commit comments