Skip to content

Commit 3e06cff

Browse files
committed
Improve mapping current branch to a PR in pr status
1 parent 3b4c9a5 commit 3e06cff

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

api/queries_pr.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type Repo interface {
117117
RepoOwner() string
118118
}
119119

120-
func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername string) (*PullRequestsPayload, error) {
120+
func PullRequests(client *Client, ghRepo Repo, currentPRNumber int, currentPRHeadRef, currentUsername string) (*PullRequestsPayload, error) {
121121
type edges struct {
122122
Edges []struct {
123123
Node PullRequest
@@ -131,12 +131,13 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
131131
type response struct {
132132
Repository struct {
133133
PullRequests edges
134+
PullRequest *PullRequest
134135
}
135136
ViewerCreated edges
136137
ReviewRequested edges
137138
}
138139

139-
query := `
140+
fragments := `
140141
fragment pr on PullRequest {
141142
number
142143
title
@@ -170,16 +171,32 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
170171
...pr
171172
reviewDecision
172173
}
173-
query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
174-
repository(owner: $owner, name: $repo) {
175-
pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page) {
176-
edges {
177-
node {
178-
...prWithReviews
179-
}
180-
}
181-
}
182-
}
174+
`
175+
176+
queryPrefix := `
177+
query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
178+
repository(owner: $owner, name: $repo) {
179+
pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page) {
180+
edges {
181+
node {
182+
...prWithReviews
183+
}
184+
}
185+
}
186+
}
187+
`
188+
if currentPRNumber > 0 {
189+
queryPrefix = `
190+
query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
191+
repository(owner: $owner, name: $repo) {
192+
pullRequest(number: $number) {
193+
...prWithReviews
194+
}
195+
}
196+
`
197+
}
198+
199+
query := fragments + queryPrefix + `
183200
viewerCreated: search(query: $viewerQuery, type: ISSUE, first: $per_page) {
184201
edges {
185202
node {
@@ -201,17 +218,17 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
201218
}
202219
}
203220
}
204-
`
221+
`
205222

206223
owner := ghRepo.RepoOwner()
207224
repo := ghRepo.RepoName()
208225

209226
viewerQuery := fmt.Sprintf("repo:%s/%s state:open is:pr author:%s", owner, repo, currentUsername)
210227
reviewerQuery := fmt.Sprintf("repo:%s/%s state:open review-requested:%s", owner, repo, currentUsername)
211228

212-
branchWithoutOwner := currentBranch
213-
if idx := strings.Index(currentBranch, ":"); idx >= 0 {
214-
branchWithoutOwner = currentBranch[idx+1:]
229+
branchWithoutOwner := currentPRHeadRef
230+
if idx := strings.Index(currentPRHeadRef, ":"); idx >= 0 {
231+
branchWithoutOwner = currentPRHeadRef[idx+1:]
215232
}
216233

217234
variables := map[string]interface{}{
@@ -220,6 +237,7 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
220237
"owner": owner,
221238
"repo": repo,
222239
"headRefName": branchWithoutOwner,
240+
"number": currentPRNumber,
223241
}
224242

225243
var resp response
@@ -238,10 +256,12 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
238256
reviewRequested = append(reviewRequested, edge.Node)
239257
}
240258

241-
var currentPR *PullRequest
242-
for _, edge := range resp.Repository.PullRequests.Edges {
243-
if edge.Node.HeadLabel() == currentBranch {
244-
currentPR = &edge.Node
259+
var currentPR = resp.Repository.PullRequest
260+
if currentPR == nil {
261+
for _, edge := range resp.Repository.PullRequests.Edges {
262+
if edge.Node.HeadLabel() == currentPRHeadRef {
263+
currentPR = &edge.Node
264+
}
245265
}
246266
}
247267

command/pr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
6868
if err != nil {
6969
return err
7070
}
71-
currentBranch, err := ctx.Branch()
71+
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx)
7272
if err != nil {
7373
return err
7474
}
@@ -77,7 +77,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
7777
return err
7878
}
7979

80-
prPayload, err := api.PullRequests(apiClient, baseRepo, currentBranch, currentUser)
80+
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)
8181
if err != nil {
8282
return err
8383
}
@@ -88,7 +88,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
8888
if prPayload.CurrentPR != nil {
8989
printPrs(out, *prPayload.CurrentPR)
9090
} else {
91-
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentBranch+"]"))
91+
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
9292
printMessage(out, message)
9393
}
9494
fmt.Fprintln(out)

0 commit comments

Comments
 (0)