From 61e0c5a294e4b97e4275e8d73dddaf2da4979c99 Mon Sep 17 00:00:00 2001 From: satotake Date: Wed, 17 Aug 2022 02:02:53 +0900 Subject: [PATCH] Add age field to `pr view` --- pkg/cmd/pr/view/view.go | 11 +++++++++-- pkg/cmd/pr/view/view_test.go | 17 +++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/pr/view/view.go b/pkg/cmd/pr/view/view.go index c385e1654..669da72b1 100644 --- a/pkg/cmd/pr/view/view.go +++ b/pkg/cmd/pr/view/view.go @@ -5,6 +5,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" @@ -31,12 +32,15 @@ type ViewOptions struct { SelectorArg string BrowserMode bool Comments bool + + Now func() time.Time } func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { opts := &ViewOptions{ IO: f.IOStreams, Browser: f.Browser, + Now: time.Now, } cmd := &cobra.Command{ @@ -81,7 +85,7 @@ var defaultFields = []string{ "isDraft", "maintainerCanModify", "mergeable", "additions", "deletions", "commitsCount", "baseRefName", "headRefName", "headRepositoryOwner", "headRepository", "isCrossRepository", "reviewRequests", "reviews", "assignees", "labels", "projectCards", "milestone", - "comments", "reactionGroups", + "comments", "reactionGroups", "createdAt", } func viewRun(opts *ViewOptions) error { @@ -167,16 +171,19 @@ func printRawPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error { func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error { out := opts.IO.Out cs := opts.IO.ColorScheme() + now := opts.Now() + ago := now.Sub(pr.CreatedAt) // Header (Title and State) fmt.Fprintf(out, "%s #%d\n", cs.Bold(pr.Title), pr.Number) fmt.Fprintf(out, - "%s • %s wants to merge %s into %s from %s • %s %s \n", + "%s • %s wants to merge %s into %s from %s • %s • %s %s \n", shared.StateTitleWithColor(cs, *pr), pr.Author.Login, utils.Pluralize(pr.Commits.TotalCount, "commit"), pr.BaseRefName, pr.HeadRefName, + utils.FuzzyAgo(ago), cs.Green("+"+strconv.Itoa(pr.Additions)), cs.Red("-"+strconv.Itoa(pr.Deletions)), ) diff --git a/pkg/cmd/pr/view/view_test.go b/pkg/cmd/pr/view/view_test.go index a909f0b6c..5791fb59d 100644 --- a/pkg/cmd/pr/view/view_test.go +++ b/pkg/cmd/pr/view/view_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "os" + "regexp" "testing" "github.com/cli/cli/v2/api" @@ -351,7 +352,7 @@ func TestPRView_Preview(t *testing.T) { }, expectedOutputs: []string{ `Blueberries are from a fork #12`, - `Open.*nobody wants to merge 12 commits into master from blueberries.+100.-10`, + `Open.*nobody wants to merge 12 commits into master from blueberries . about X years ago.+100.-10`, `blueberries taste good`, `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, }, @@ -364,7 +365,7 @@ func TestPRView_Preview(t *testing.T) { }, expectedOutputs: []string{ `Blueberries are from a fork #12`, - `Open.*nobody wants to merge 12 commits into master from blueberries.+100.-10`, + `Open.*nobody wants to merge 12 commits into master from blueberries . about X years ago.+100.-10`, `Reviewers:.*1 \(.*Requested.*\)\n`, `Assignees:.*marseilles, monaco\n`, `Labels:.*one, two, three, four, five\n`, @@ -396,7 +397,7 @@ func TestPRView_Preview(t *testing.T) { }, expectedOutputs: []string{ `Blueberries are from a fork #12`, - `Closed.*nobody wants to merge 12 commits into master from blueberries.+100.-10`, + `Closed.*nobody wants to merge 12 commits into master from blueberries . about X years ago.+100.-10`, `blueberries taste good`, `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, }, @@ -409,7 +410,7 @@ func TestPRView_Preview(t *testing.T) { }, expectedOutputs: []string{ `Blueberries are from a fork #12`, - `Merged.*nobody wants to merge 12 commits into master from blueberries.+100.-10`, + `Merged.*nobody wants to merge 12 commits into master from blueberries . about X years ago.+100.-10`, `blueberries taste good`, `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, }, @@ -422,7 +423,7 @@ func TestPRView_Preview(t *testing.T) { }, expectedOutputs: []string{ `Blueberries are from a fork #12`, - `Draft.*nobody wants to merge 12 commits into master from blueberries.+100.-10`, + `Draft.*nobody wants to merge 12 commits into master from blueberries . about X years ago.+100.-10`, `blueberries taste good`, `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, }, @@ -445,8 +446,12 @@ func TestPRView_Preview(t *testing.T) { assert.Equal(t, "", output.Stderr()) + out := output.String() + timeRE := regexp.MustCompile(`\d+ years`) + out = timeRE.ReplaceAllString(out, "X years") + //nolint:staticcheck // prefer exact matchers over ExpectLines - test.ExpectLines(t, output.String(), tc.expectedOutputs...) + test.ExpectLines(t, out, tc.expectedOutputs...) }) } }