X Tutup
Skip to content

Commit 70d4786

Browse files
authored
Merge pull request cli#2988 from cli/strict-status-checks-base
pr status: fix checking branch protection rules on the base branch
2 parents 3b117e6 + 05421db commit 70d4786

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

api/queries_pr.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ import (
1616
)
1717

1818
type PullRequestsPayload struct {
19-
ViewerCreated PullRequestAndTotalCount
20-
ReviewRequested PullRequestAndTotalCount
21-
CurrentPR *PullRequest
22-
DefaultBranch string
23-
StrictProtection bool
19+
ViewerCreated PullRequestAndTotalCount
20+
ReviewRequested PullRequestAndTotalCount
21+
CurrentPR *PullRequest
22+
DefaultBranch string
2423
}
2524

2625
type PullRequestAndTotalCount struct {
@@ -57,6 +56,12 @@ type PullRequest struct {
5756
IsDraft bool
5857
MaintainerCanModify bool
5958

59+
BaseRef struct {
60+
BranchProtectionRule struct {
61+
RequiresStrictStatusChecks bool
62+
}
63+
}
64+
6065
ReviewDecision string
6166

6267
Commits struct {
@@ -283,10 +288,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
283288
type response struct {
284289
Repository struct {
285290
DefaultBranchRef struct {
286-
Name string
287-
BranchProtectionRule struct {
288-
RequiresStrictStatusChecks bool
289-
}
291+
Name string
290292
}
291293
PullRequests edges
292294
PullRequest *PullRequest
@@ -342,6 +344,11 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
342344
headRepositoryOwner {
343345
login
344346
}
347+
baseRef {
348+
branchProtectionRule {
349+
requiresStrictStatusChecks
350+
}
351+
}
345352
isCrossRepository
346353
isDraft
347354
%s
@@ -467,9 +474,8 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
467474
PullRequests: reviewRequested,
468475
TotalCount: resp.ReviewRequested.TotalCount,
469476
},
470-
CurrentPR: currentPR,
471-
DefaultBranch: resp.Repository.DefaultBranchRef.Name,
472-
StrictProtection: resp.Repository.DefaultBranchRef.BranchProtectionRule.RequiresStrictStatusChecks,
477+
CurrentPR: currentPR,
478+
DefaultBranch: resp.Repository.DefaultBranchRef.Name,
473479
}
474480

475481
return &payload, nil

pkg/cmd/pr/status/status.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func statusRun(opts *StatusOptions) error {
114114
currentPR = nil
115115
}
116116
if currentPR != nil {
117-
printPrs(opts.IO, 1, prPayload.StrictProtection, *currentPR)
117+
printPrs(opts.IO, 1, *currentPR)
118118
} else if currentPRHeadRef == "" {
119119
shared.PrintMessage(opts.IO, " There is no current branch")
120120
} else {
@@ -124,15 +124,15 @@ func statusRun(opts *StatusOptions) error {
124124

125125
shared.PrintHeader(opts.IO, "Created by you")
126126
if prPayload.ViewerCreated.TotalCount > 0 {
127-
printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.StrictProtection, prPayload.ViewerCreated.PullRequests...)
127+
printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...)
128128
} else {
129129
shared.PrintMessage(opts.IO, " You have no open pull requests")
130130
}
131131
fmt.Fprintln(out)
132132

133133
shared.PrintHeader(opts.IO, "Requesting a code review from you")
134134
if prPayload.ReviewRequested.TotalCount > 0 {
135-
printPrs(opts.IO, prPayload.ReviewRequested.TotalCount, prPayload.StrictProtection, prPayload.ReviewRequested.PullRequests...)
135+
printPrs(opts.IO, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...)
136136
} else {
137137
shared.PrintMessage(opts.IO, " You have no pull requests to review")
138138
}
@@ -178,7 +178,7 @@ func prSelectorForCurrentBranch(baseRepo ghrepo.Interface, prHeadRef string, rem
178178
return
179179
}
180180

181-
func printPrs(io *iostreams.IOStreams, totalCount int, strictProtection bool, prs ...api.PullRequest) {
181+
func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) {
182182
w := io.Out
183183
cs := io.ColorScheme()
184184

@@ -228,15 +228,14 @@ func printPrs(io *iostreams.IOStreams, totalCount int, strictProtection bool, pr
228228
fmt.Fprint(w, cs.Green("✓ Approved"))
229229
}
230230

231-
// only check if the "up to date" setting is checked in repo settings
232-
if strictProtection {
233-
// add padding between reviews & merge status
234-
fmt.Fprint(w, " ")
235-
236-
if pr.MergeStateStatus == "BEHIND" {
237-
fmt.Fprint(w, cs.Yellow("- Not up to date"))
238-
} else {
239-
fmt.Fprint(w, cs.Green("✓ Up to date"))
231+
if pr.BaseRef.BranchProtectionRule.RequiresStrictStatusChecks {
232+
switch pr.MergeStateStatus {
233+
case "BEHIND":
234+
fmt.Fprintf(w, " %s", cs.Yellow("- Not up to date"))
235+
case "UNKNOWN", "DIRTY":
236+
// do not print anything
237+
default:
238+
fmt.Fprintf(w, " %s", cs.Green("✓ Up to date"))
240239
}
241240
}
242241

0 commit comments

Comments
 (0)
X Tutup