Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
refactor main + fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-chrismc committed Mar 16, 2021
1 parent ebf2490 commit 081984d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 51 deletions.
30 changes: 30 additions & 0 deletions internal/format/merge_ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package format

import (
"fmt"

"github.com/prince-chrismc/conan-center-index-pending-review/v2/pkg/pending_review"
)

func ReadyToMerge(prs []*pending_review.PullRequestSummary) string {
tableBody, rowCount := ReviewsToMarkdownRows(prs, true)

if rowCount == 0 {
return ""
}

breif := "**1** pull request is"
if rowCount > 1 {
breif = "***" + fmt.Sprint(rowCount) + "** pull requests are"
}

return `
### :heavy_check_mark: Ready to Merge
Currently ` + breif + ` waiting to be merged :tada:
PR | By | Recipe | Reviews | :stop_sign: Blockers | :star2: Approvers
:---: | --- | --- | :---: | --- | ---
` + tableBody
}
6 changes: 4 additions & 2 deletions internal/format/reviews.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
)

// ReviewsToMarkdownRows Converts pull request review status to GitHub markdown table rows
func ReviewsToMarkdownRows(prs []*pending_review.ReviewSummary, canMerge bool) string {
func ReviewsToMarkdownRows(prs []*pending_review.PullRequestSummary, canMerge bool) (string, int) {
count := 0
var retval string
for _, pr := range prs {
if pr.Summary.IsApproved() != canMerge {
continue
}

count++
title := title(pr.Change, pr.Recipe)
if !pr.CciBotPassed && pr.Summary.IsApproved() { //TODO(prince-chrismc): Always display bad commit statuses?
title = ":warning: " + pr.Recipe
Expand All @@ -31,7 +33,7 @@ func ReviewsToMarkdownRows(prs []*pending_review.ReviewSummary, canMerge bool) s
retval += strings.Join(columns, "|")
retval += "\n"
}
return retval
return retval, count
}

func title(change pending_review.Category, recipe string) string {
Expand Down
11 changes: 8 additions & 3 deletions internal/format/reviews_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestFormatTitles(t *testing.T) {
}

func TestFormatMarkdownRows(t *testing.T) {
var rs []*pending_review.ReviewSummary
var rs []*pending_review.PullRequestSummary
if err := json.Unmarshal([]byte(`[
{
"Number": 4556,
Expand Down Expand Up @@ -62,6 +62,11 @@ func TestFormatMarkdownRows(t *testing.T) {
t.Fatal("Broken test - invalid JSON content:", err)
}

assert.Equal(t, ReviewsToMarkdownRows(rs, false), "[#4556](https://github.com/conan-io/conan-center-index/pull/4556)|[anton-danielsson](https://github.com/anton-danielsson)|:memo: protobuf|36|uilianries|prince-chrismc\n")
assert.Equal(t, ReviewsToMarkdownRows(rs, true), "[#4682](https://github.com/conan-io/conan-center-index/pull/4682)|[floriansimon1](https://github.com/floriansimon1)|:warning: protobuf|13||prince-chrismc\n")
mergeRow, mergeCount := ReviewsToMarkdownRows(rs, false)
assert.Equal(t, mergeCount, 1)
assert.Equal(t, mergeRow, "[#4556](https://github.com/conan-io/conan-center-index/pull/4556)|[anton-danielsson](https://github.com/anton-danielsson)|:memo: protobuf|36|uilianries|prince-chrismc\n")

reviewRow, reviewCount := ReviewsToMarkdownRows(rs, true)
assert.Equal(t, reviewCount, 1)
assert.Equal(t, reviewRow, "[#4682](https://github.com/conan-io/conan-center-index/pull/4682)|[floriansimon1](https://github.com/floriansimon1)|:warning: protobuf|13||prince-chrismc\n")
}
28 changes: 28 additions & 0 deletions internal/format/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package format

import (
"fmt"
"os"

"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/duration"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/stats"
)

func Statistics(stats stats.Stats) string {
return `
#### :bar_chart: Statistics
> :warning: These are just rough metrics counting the labels and may not reflect the acutal state of pull requests
- Commit: ` + os.Getenv("GITHUB_SHA") + `
- Pull Requests:
- Open: ` + fmt.Sprint(stats.Open) + `
- Draft: ` + fmt.Sprint(stats.Draft) + `
- Average Age: ` + duration.String(stats.Age.GetCurrentAverage()) + `
- Labels:
- Stale: ` + fmt.Sprint(stats.Stale) + `
- Failed: ` + fmt.Sprint(stats.Failed) + `
- Blocked: ` + fmt.Sprint(stats.Blocked) + `
`
}
32 changes: 32 additions & 0 deletions internal/format/under_way.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package format

import (
"fmt"

"github.com/prince-chrismc/conan-center-index-pending-review/v2/pkg/pending_review"
)

func UnderReview(prs []*pending_review.PullRequestSummary) string {
tableBody, rowCount := ReviewsToMarkdownRows(prs, false)

if rowCount == 0 {
return `
:confused: There's nothing within the review process... You should [open a bug report](https://github.com/prince-chrismc/conan-center-index-pending-review/issues/new)
`
}

breif := "is **1** pull request"
if rowCount > 1 {
breif = "are ***" + fmt.Sprint(rowCount) + "** pull requests"
}

return `
### :nerd_face: Please Review!
There ` + breif + ` currently under way :eyes:
PR | By | Recipe | Reviews | :stop_sign: Blockers | :star2: Approvers
:---: | --- | --- | :---: | --- | ---
` + tableBody
}
47 changes: 5 additions & 42 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/google/go-github/v33/github"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/duration"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/format"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/stats"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/validate"
Expand Down Expand Up @@ -45,7 +44,7 @@ func Run(token string, dryRun bool) error {
fmt.Printf("%+v\n-----\n", repo)

var stats stats.Stats
var retval []*pending_review.ReviewSummary
var retval []*pending_review.PullRequestSummary
opt := &github.PullRequestListOptions{
Sort: "created",
Direction: "asc",
Expand All @@ -65,7 +64,6 @@ func Run(token string, dryRun bool) error {
retval = append(retval, out...)
stats.Add(s)

// Handle Pagination: https://github.com/google/go-github#pagination
if resp.NextPage == 0 {
break
}
Expand Down Expand Up @@ -95,19 +93,6 @@ func Run(token string, dryRun bool) error {
return nil
}

var ready string
if stats.Merge > 0 {
ready = `
### :heavy_check_mark: Ready to Merge
Currently **` + fmt.Sprint(stats.Merge) + `** pull request(s) is/are waiting to be merged :tada:
PR | By | Recipe | Reviews | :stop_sign: Blockers | :star2: Approvers
:---: | --- | --- | :---: | --- | ---
` + format.ReviewsToMarkdownRows(retval, true)
}

_, _, err = client.Issues.Edit(context, "prince-chrismc", "conan-center-index-pending-review", 1, &github.IssueRequest{
Body: github.String(`## :sparkles: Summary of Pull Requests Pending Review!
Expand All @@ -125,30 +110,8 @@ Icon | Description | Notes
:arrow_up: | Version bump | _closely_ matches the label
:memo: | Modification to an existing recipe |
:green_book: | Documentation change | matches the label
:warning: | The merge commit status does **not** indicate success | only displayed when ready to merge
### :nerd_face: Please Review!
There are **` + fmt.Sprint(stats.Review) + `** pull requests currently under way :eyes:
PR | By | Recipe | Reviews | :stop_sign: Blockers | :star2: Approvers
:---: | --- | --- | :---: | --- | ---
` + format.ReviewsToMarkdownRows(retval, false) + ready + `
#### :bar_chart: Statistics
> :warning: These are just rough metrics counthing the labels and may not reflect the acutal state of pull requests
- Commit: ` + os.Getenv("GITHUB_SHA") + `
- PRs
- Open: ` + fmt.Sprint(stats.Open) + `
- Draft: ` + fmt.Sprint(stats.Draft) + `
- Age: ` + duration.String(stats.Age.GetCurrentAverage()) + `
- Labels
- Stale: ` + fmt.Sprint(stats.Stale) + `
- Failed: ` + fmt.Sprint(stats.Failed) + `
- Blocked: ` + fmt.Sprint(stats.Blocked) + `
` +
:warning: | The merge commit status does **not** indicate success | only displayed when ready to merge` +
format.UnderReview(retval) + format.ReadyToMerge(retval) + format.Statistics(stats) +
"\n\n<details><summary>Raw JSON data</summary>\n\n```json\n" + string(bytes) + "\n```\n\n</details>"),
})
if err != nil {
Expand All @@ -173,9 +136,9 @@ func validateContentIsDifferent(context context.Context, client *pending_review.
return obtained != expected, nil
}

func gatherReviewStatus(context context.Context, client *pending_review.Client, prs []*pending_review.PullRequest) ([]*pending_review.ReviewSummary, stats.Stats) {
func gatherReviewStatus(context context.Context, client *pending_review.Client, prs []*pending_review.PullRequest) ([]*pending_review.PullRequestSummary, stats.Stats) {
var stats stats.Stats
var out []*pending_review.ReviewSummary
var out []*pending_review.PullRequestSummary
for _, pr := range prs {
stats.Age.Append(time.Now().Sub(pr.GetCreatedAt()))
stats.Open++
Expand Down
8 changes: 4 additions & 4 deletions pkg/pending_review/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const (
DOCS Category = iota
)

// ReviewSummary of a pull request based on the rules of conan-center-index.
// PullRequestSummary regarding its location in the review process of conan-center-index.
// See https://github.com/conan-io/conan-center-index/blob/master/docs/review_process.md
// for more inforamtion
type ReviewSummary struct {
type PullRequestSummary struct {
Number int
OpenedBy string
Recipe string
Expand Down Expand Up @@ -66,8 +66,8 @@ func (s *PullRequestService) ListAllReviews(ctx context.Context, owner string, r
}

// GetReviewSummary of a specific pull request
func (s *PullRequestService) GetReviewSummary(ctx context.Context, owner string, repo string, pr *PullRequest) (*ReviewSummary, *Response, error) {
p := &ReviewSummary{
func (s *PullRequestService) GetReviewSummary(ctx context.Context, owner string, repo string, pr *PullRequest) (*PullRequestSummary, *Response, error) {
p := &PullRequestSummary{
Number: pr.GetNumber(),
OpenedBy: pr.GetUser().GetLogin(),
ReviewURL: pr.GetHTMLURL(),
Expand Down

0 comments on commit 081984d

Please sign in to comment.