diff --git a/api/queries_pr.go b/api/queries_pr.go index d20e7baf9..59cf04460 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -16,10 +16,11 @@ import ( ) type PullRequestsPayload struct { - ViewerCreated PullRequestAndTotalCount - ReviewRequested PullRequestAndTotalCount - CurrentPR *PullRequest - DefaultBranch string + ViewerCreated PullRequestAndTotalCount + ReviewRequested PullRequestAndTotalCount + CurrentPR *PullRequest + DefaultBranch string + StrictProtection bool } type PullRequestAndTotalCount struct { @@ -28,16 +29,17 @@ type PullRequestAndTotalCount struct { } type PullRequest struct { - ID string - Number int - Title string - State string - Closed bool - URL string - BaseRefName string - HeadRefName string - Body string - Mergeable string + ID string + Number int + Title string + State string + Closed bool + URL string + BaseRefName string + HeadRefName string + Body string + Mergeable string + MergeStateStatus string Author struct { Login string @@ -281,7 +283,10 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu type response struct { Repository struct { DefaultBranchRef struct { - Name string + Name string + BranchProtectionRule struct { + RequiresStrictStatusChecks bool + } } PullRequests edges PullRequest *PullRequest @@ -333,6 +338,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu state url headRefName + mergeStateStatus headRepositoryOwner { login } @@ -349,7 +355,12 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix := ` query PullRequestStatus($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - defaultBranchRef { name } + defaultBranchRef { + name + branchProtectionRule { + requiresStrictStatusChecks + } + } pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) { totalCount edges { @@ -364,7 +375,12 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix = ` query PullRequestStatus($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - defaultBranchRef { name } + defaultBranchRef { + name + branchProtectionRule { + requiresStrictStatusChecks + } + } pullRequest(number: $number) { ...prWithReviews } @@ -451,8 +467,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu PullRequests: reviewRequested, TotalCount: resp.ReviewRequested.TotalCount, }, - CurrentPR: currentPR, - DefaultBranch: resp.Repository.DefaultBranchRef.Name, + CurrentPR: currentPR, + DefaultBranch: resp.Repository.DefaultBranchRef.Name, + StrictProtection: resp.Repository.DefaultBranchRef.BranchProtectionRule.RequiresStrictStatusChecks, } return &payload, nil diff --git a/pkg/cmd/factory/http.go b/pkg/cmd/factory/http.go index 8cd1bf12a..2e7619540 100644 --- a/pkg/cmd/factory/http.go +++ b/pkg/cmd/factory/http.go @@ -87,6 +87,8 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg config.Config, appVersion string api.AddHeaderFunc("Accept", func(req *http.Request) (string, error) { // antiope-preview: Checks accept := "application/vnd.github.antiope-preview+json" + // introduced for #2952: pr branch up to date status + accept += ", application/vnd.github.merge-info-preview+json" if ghinstance.IsEnterprise(req.URL.Hostname()) { // shadow-cat-preview: Draft pull requests accept += ", application/vnd.github.shadow-cat-preview" diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index bec49f259..5a210941a 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -114,7 +114,7 @@ func statusRun(opts *StatusOptions) error { currentPR = nil } if currentPR != nil { - printPrs(opts.IO, 1, *currentPR) + printPrs(opts.IO, 1, prPayload.StrictProtection, *currentPR) } else if currentPRHeadRef == "" { shared.PrintMessage(opts.IO, " There is no current branch") } else { @@ -124,7 +124,7 @@ func statusRun(opts *StatusOptions) error { shared.PrintHeader(opts.IO, "Created by you") if prPayload.ViewerCreated.TotalCount > 0 { - printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...) + printPrs(opts.IO, prPayload.ViewerCreated.TotalCount, prPayload.StrictProtection, prPayload.ViewerCreated.PullRequests...) } else { shared.PrintMessage(opts.IO, " You have no open pull requests") } @@ -132,7 +132,7 @@ func statusRun(opts *StatusOptions) error { shared.PrintHeader(opts.IO, "Requesting a code review from you") if prPayload.ReviewRequested.TotalCount > 0 { - printPrs(opts.IO, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...) + printPrs(opts.IO, prPayload.ReviewRequested.TotalCount, prPayload.StrictProtection, prPayload.ReviewRequested.PullRequests...) } else { shared.PrintMessage(opts.IO, " You have no pull requests to review") } @@ -178,7 +178,7 @@ func prSelectorForCurrentBranch(baseRepo ghrepo.Interface, prHeadRef string, rem return } -func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) { +func printPrs(io *iostreams.IOStreams, totalCount int, strictProtection bool, prs ...api.PullRequest) { w := io.Out cs := io.ColorScheme() @@ -227,6 +227,19 @@ func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) { } else if reviews.Approved { 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")) + } + } + } else { fmt.Fprintf(w, " - %s", shared.StateTitleWithColor(cs, pr)) }