From 081984d9ad1e1243a3c472458db96192a985cf79 Mon Sep 17 00:00:00 2001 From: Chris McArthur Date: Mon, 15 Mar 2021 20:07:02 -0400 Subject: [PATCH] refactor main + fix naming --- internal/format/merge_ready.go | 30 +++++++++++++++++++ internal/format/reviews.go | 6 ++-- internal/format/reviews_test.go | 11 +++++-- internal/format/statistics.go | 28 ++++++++++++++++++ internal/format/under_way.go | 32 ++++++++++++++++++++ internal/main.go | 47 ++++-------------------------- pkg/pending_review/pull_request.go | 8 ++--- 7 files changed, 111 insertions(+), 51 deletions(-) create mode 100644 internal/format/merge_ready.go create mode 100644 internal/format/statistics.go create mode 100644 internal/format/under_way.go diff --git a/internal/format/merge_ready.go b/internal/format/merge_ready.go new file mode 100644 index 0000000000..1835efd213 --- /dev/null +++ b/internal/format/merge_ready.go @@ -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 +} diff --git a/internal/format/reviews.go b/internal/format/reviews.go index 4ad70126bb..e160026f1a 100644 --- a/internal/format/reviews.go +++ b/internal/format/reviews.go @@ -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 @@ -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 { diff --git a/internal/format/reviews_test.go b/internal/format/reviews_test.go index 5ce0cb5a59..032234ec4d 100644 --- a/internal/format/reviews_test.go +++ b/internal/format/reviews_test.go @@ -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, @@ -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") } diff --git a/internal/format/statistics.go b/internal/format/statistics.go new file mode 100644 index 0000000000..cac2da47b9 --- /dev/null +++ b/internal/format/statistics.go @@ -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) + ` + ` +} diff --git a/internal/format/under_way.go b/internal/format/under_way.go new file mode 100644 index 0000000000..f934e3e129 --- /dev/null +++ b/internal/format/under_way.go @@ -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 +} diff --git a/internal/main.go b/internal/main.go index 4a759e8605..372b8a9ec7 100644 --- a/internal/main.go +++ b/internal/main.go @@ -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" @@ -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", @@ -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 } @@ -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! @@ -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
Raw JSON data\n\n```json\n" + string(bytes) + "\n```\n\n
"), }) if err != nil { @@ -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++ diff --git a/pkg/pending_review/pull_request.go b/pkg/pending_review/pull_request.go index 2de55a8849..fe5f3dc217 100644 --- a/pkg/pending_review/pull_request.go +++ b/pkg/pending_review/pull_request.go @@ -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 @@ -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(),