Add age field to pr view

This commit is contained in:
satotake 2022-08-17 02:02:53 +09:00
parent 837fac0da3
commit 61e0c5a294
2 changed files with 20 additions and 8 deletions

View file

@ -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)),
)

View file

@ -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...)
})
}
}