pr status: show number of approvals (#4240)

If the base branch has no minimum approval requirements, show "N Approved"
reviews. Otherwise, show "N/X Approved".

Co-authored-by: Mislav Marohnić <mislav@github.com>
This commit is contained in:
Des Preston 2022-01-21 10:58:15 -05:00 committed by GitHub
parent d3076463aa
commit 2e07d0f32c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 4 deletions

View file

@ -75,6 +75,8 @@ func (pr *PullRequest) ExportData(fields []string) map[string]interface{} {
data[f] = pr.ProjectCards.Nodes
case "reviews":
data[f] = pr.Reviews.Nodes
case "latestReviews":
data[f] = pr.LatestReviews.Nodes
case "files":
data[f] = pr.Files.Nodes
case "reviewRequests":

View file

@ -64,7 +64,8 @@ type PullRequest struct {
BaseRef struct {
BranchProtectionRule struct {
RequiresStrictStatusChecks bool
RequiresStrictStatusChecks bool
RequiredApprovingReviewCount int
}
}
@ -108,6 +109,7 @@ type PullRequest struct {
Comments Comments
ReactionGroups ReactionGroups
Reviews PullRequestReviews
LatestReviews PullRequestReviews
ReviewRequests ReviewRequests
}
@ -405,6 +407,11 @@ func PullRequestStatus(client *Client, repo ghrepo.Interface, options StatusOpti
}
pullRequest(number: $number) {
...prWithReviews
baseRef {
branchProtectionRule {
requiredApprovingReviewCount
}
}
}
}
`
@ -519,7 +526,7 @@ func pullRequestFragment(httpClient *http.Client, hostname string) (string, erro
var reviewFields []string
if prFeatures.HasReviewDecision {
reviewFields = append(reviewFields, "reviewDecision")
reviewFields = append(reviewFields, "reviewDecision", "latestReviews")
}
fragments := fmt.Sprintf(`

View file

@ -82,6 +82,18 @@ var prReviews = shortenQuery(`
}
`)
var prLatestReviews = shortenQuery(`
latestReviews(first: 100) {
nodes {
author{login},
authorAssociation,
submittedAt,
body,
state
}
}
`)
var prFiles = shortenQuery(`
files(first: 100) {
nodes {
@ -180,6 +192,7 @@ var PullRequestFields = append(IssueFields,
"headRepositoryOwner",
"isCrossRepository",
"isDraft",
"latestReviews",
"maintainerCanModify",
"mergeable",
"mergeCommit",
@ -229,6 +242,8 @@ func PullRequestGraphQL(fields []string) string {
q = append(q, prReviewRequests)
case "reviews":
q = append(q, prReviews)
case "latestReviews":
q = append(q, prLatestReviews)
case "files":
q = append(q, prFiles)
case "commits":

View file

@ -44,6 +44,33 @@
"url": "https://github.com/cli/cli/pull/7",
"headRefName": "banananana",
"reviewDecision": "APPROVED",
"baseRef": {
"branchProtectionRule": {
"requiredApprovingReviewCount": 0
}
},
"latestReviews": {
"nodes": [
{
"author": {
"login": "bob"
},
"state": "APPROVED"
},
{
"author": {
"login": "stella"
},
"state": "CHANGES_REQUESTED"
},
{
"author": {
"login": "alice"
},
"state": "APPROVED"
}
]
},
"statusCheckRollup": {
"nodes": [
{

View file

@ -199,6 +199,16 @@ func prSelectorForCurrentBranch(baseRepo ghrepo.Interface, prHeadRef string, rem
return
}
func totalApprovals(pr *api.PullRequest) int {
approvals := 0
for _, review := range pr.LatestReviews.Nodes {
if review.State == "APPROVED" {
approvals++
}
}
return approvals
}
func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) {
w := io.Out
cs := io.ColorScheme()
@ -246,7 +256,13 @@ func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) {
} else if reviews.ReviewRequired {
fmt.Fprint(w, cs.Yellow("- Review required"))
} else if reviews.Approved {
fmt.Fprint(w, cs.Green("✓ Approved"))
numRequiredApprovals := pr.BaseRef.BranchProtectionRule.RequiredApprovingReviewCount
gotApprovals := totalApprovals(&pr)
s := fmt.Sprintf("%d", gotApprovals)
if numRequiredApprovals > 0 {
s = fmt.Sprintf("%d/%d", gotApprovals, numRequiredApprovals)
}
fmt.Fprint(w, cs.Green(fmt.Sprintf("✓ %s Approved", s)))
}
if pr.BaseRef.BranchProtectionRule.RequiresStrictStatusChecks {

View file

@ -112,7 +112,7 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) {
expected := []string{
"✓ Checks passing + Changes requested",
"- Checks pending ✓ Approved",
"- Checks pending ✓ 2 Approved",
"× 1/3 checks failing - Review required",
}