From 0d55f8648c0540c4a0aaaf568a39967fbbff93ed Mon Sep 17 00:00:00 2001 From: Michael Neeley Date: Thu, 11 Feb 2021 16:27:23 -0500 Subject: [PATCH 1/4] adds merge state status --- api/client.go | 1 + api/queries_pr.go | 2 ++ pkg/cmd/pr/status/status.go | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/api/client.go b/api/client.go index 09195181b..17534b8b2 100644 --- a/api/client.go +++ b/api/client.go @@ -239,6 +239,7 @@ func (c Client) GraphQL(hostname string, query string, variables map[string]inte } req.Header.Set("Content-Type", "application/json; charset=utf-8") + req.Header.Set("Accept", "application/vnd.github.merge-info-preview+json") resp, err := c.http.Do(req) if err != nil { diff --git a/api/queries_pr.go b/api/queries_pr.go index 8b526bdc6..898d3297a 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -38,6 +38,7 @@ type PullRequest struct { HeadRefName string Body string Mergeable string + MergeStateStatus string Author struct { Login string @@ -353,6 +354,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu state url headRefName + mergeStateStatus headRepositoryOwner { login } diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index bec49f259..3758a9d67 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -227,6 +227,16 @@ func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) { } else if reviews.Approved { fmt.Fprint(w, cs.Green("✓ Approved")) } + + // 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)) } From 8511365afb56a3ea24b3c7f17768ef2080b519ad Mon Sep 17 00:00:00 2001 From: Michael Neeley Date: Thu, 11 Feb 2021 16:46:16 -0500 Subject: [PATCH 2/4] linter --- api/queries_pr.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index 898d3297a..273f95f40 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -28,17 +28,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 - MergeStateStatus 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 From 9be9229a48c2fca10942eb707b94fcc16286cd05 Mon Sep 17 00:00:00 2001 From: Michael Neeley Date: Fri, 12 Feb 2021 08:51:47 -0500 Subject: [PATCH 3/4] adds strict status checks --- api/queries_pr.go | 33 ++++++++++++++++++++++++--------- pkg/cmd/pr/status/status.go | 23 +++++++++++++---------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index 273f95f40..b40b9a32e 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 { @@ -302,7 +303,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 @@ -371,7 +375,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 { @@ -386,7 +395,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 } @@ -473,8 +487,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/pr/status/status.go b/pkg/cmd/pr/status/status.go index 3758a9d67..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() @@ -228,13 +228,16 @@ func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) { fmt.Fprint(w, cs.Green("✓ Approved")) } - // add padding between reviews & merge status - fmt.Fprint(w, " ") + // 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.MergeStateStatus == "BEHIND" { + fmt.Fprint(w, cs.Yellow("- Not up to date")) + } else { + fmt.Fprint(w, cs.Green("✓ Up to date")) + } } } else { From 57140ad35e8eb600fcc5b36ac86835a4c126364e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 16 Feb 2021 12:25:09 -0600 Subject: [PATCH 4/4] add header in correct place --- api/client.go | 1 - pkg/cmd/factory/http.go | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/api/client.go b/api/client.go index 17534b8b2..09195181b 100644 --- a/api/client.go +++ b/api/client.go @@ -239,7 +239,6 @@ func (c Client) GraphQL(hostname string, query string, variables map[string]inte } req.Header.Set("Content-Type", "application/json; charset=utf-8") - req.Header.Set("Accept", "application/vnd.github.merge-info-preview+json") resp, err := c.http.Do(req) if err != 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"