Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pr status: fix checking branch protection rules on the base branch #2988

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import (
)

type PullRequestsPayload struct {
ViewerCreated PullRequestAndTotalCount
ReviewRequested PullRequestAndTotalCount
CurrentPR *PullRequest
DefaultBranch string
StrictProtection bool
ViewerCreated PullRequestAndTotalCount
ReviewRequested PullRequestAndTotalCount
CurrentPR *PullRequest
DefaultBranch string
}

type PullRequestAndTotalCount struct {
Expand Down Expand Up @@ -57,6 +56,12 @@ type PullRequest struct {
IsDraft bool
MaintainerCanModify bool

BaseRef struct {
BranchProtectionRule struct {
RequiresStrictStatusChecks bool
}
}

ReviewDecision string

Commits struct {
Expand Down Expand Up @@ -283,10 +288,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
type response struct {
Repository struct {
DefaultBranchRef struct {
Name string
BranchProtectionRule struct {
RequiresStrictStatusChecks bool
}
Name string
}
PullRequests edges
PullRequest *PullRequest
Expand Down Expand Up @@ -342,6 +344,11 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
headRepositoryOwner {
login
}
baseRef {
branchProtectionRule {
requiresStrictStatusChecks
}
}
isCrossRepository
isDraft
%s
Expand Down Expand Up @@ -467,9 +474,8 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
PullRequests: reviewRequested,
TotalCount: resp.ReviewRequested.TotalCount,
},
CurrentPR: currentPR,
DefaultBranch: resp.Repository.DefaultBranchRef.Name,
StrictProtection: resp.Repository.DefaultBranchRef.BranchProtectionRule.RequiresStrictStatusChecks,
CurrentPR: currentPR,
DefaultBranch: resp.Repository.DefaultBranchRef.Name,
}

return &payload, nil
Expand Down
25 changes: 12 additions & 13 deletions pkg/cmd/pr/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func statusRun(opts *StatusOptions) error {
currentPR = nil
}
if currentPR != nil {
printPrs(opts.IO, 1, prPayload.StrictProtection, *currentPR)
printPrs(opts.IO, 1, *currentPR)
} else if currentPRHeadRef == "" {
shared.PrintMessage(opts.IO, " There is no current branch")
} else {
Expand All @@ -124,15 +124,15 @@ func statusRun(opts *StatusOptions) error {

shared.PrintHeader(opts.IO, "Created by you")
if prPayload.ViewerCreated.TotalCount > 0 {
printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.StrictProtection, prPayload.ViewerCreated.PullRequests...)
printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...)
} else {
shared.PrintMessage(opts.IO, " You have no open pull requests")
}
fmt.Fprintln(out)

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

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

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

// only check if the "up to date" setting is checked in repo settings
if strictProtection {
// add padding between reviews & merge status
fmt.Fprint(w, " ")

if pr.MergeStateStatus == "BEHIND" {
fmt.Fprint(w, cs.Yellow("- Not up to date"))
} else {
fmt.Fprint(w, cs.Green("✓ Up to date"))
if pr.BaseRef.BranchProtectionRule.RequiresStrictStatusChecks {
switch pr.MergeStateStatus {
case "BEHIND":
fmt.Fprintf(w, " %s", cs.Yellow("- Not up to date"))
case "UNKNOWN", "DIRTY":
// do not print anything
default:
fmt.Fprintf(w, " %s", cs.Green("✓ Up to date"))
}
}

Expand Down