Skip to content

Commit

Permalink
Merge pull request cli#667 from doi-t/view-the-current-state
Browse files Browse the repository at this point in the history
Show the state (open, closed, merged) in issue view and pr view
  • Loading branch information
mislav authored Apr 3, 2020
2 parents 43b28f7 + 7eabbf5 commit e10ccef
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 159 deletions.
3 changes: 3 additions & 0 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Issue struct {
URL string
State string
Body string
CreatedAt time.Time
UpdatedAt time.Time
Comments struct {
TotalCount int
Expand Down Expand Up @@ -278,6 +279,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
hasIssuesEnabled
issue(number: $issue_number) {
title
state
body
author {
login
Expand All @@ -292,6 +294,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
}
number
url
createdAt
}
}
}`
Expand Down
3 changes: 3 additions & 0 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
url
number
title
state
body
author {
login
Expand All @@ -320,6 +321,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
}
}
isCrossRepository
isDraft
maintainerCanModify
}
}
Expand Down Expand Up @@ -356,6 +358,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
nodes {
number
title
state
body
author {
login
Expand Down
16 changes: 10 additions & 6 deletions command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ func issueView(cmd *cobra.Command, args []string) error {

}

func issueStateTitleWithColor(state string) string {
colorFunc := colorFuncForState(state)
return colorFunc(strings.Title(strings.ToLower(state)))
}

func listHeader(repoName string, itemName string, matchCount int, totalMatchCount int, hasFilters bool) string {
if totalMatchCount == 0 {
if hasFilters {
Expand All @@ -248,17 +253,16 @@ func listHeader(repoName string, itemName string, matchCount int, totalMatchCoun
}

func printIssuePreview(out io.Writer, issue *api.Issue) error {
coloredLabels := labelList(*issue)
if coloredLabels != "" {
coloredLabels = utils.Gray(fmt.Sprintf("(%s)", coloredLabels))
}
now := time.Now()
ago := now.Sub(issue.CreatedAt)

fmt.Fprintln(out, utils.Bold(issue.Title))
fmt.Fprintf(out, "%s", issueStateTitleWithColor(issue.State))
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
"opened by %s. %s. %s",
" %s opened %s • %s",
issue.Author.Login,
utils.FuzzyAgo(ago),
utils.Pluralize(issue.Comments.TotalCount, "comment"),
coloredLabels,
)))

if issue.Body != "" {
Expand Down
149 changes: 83 additions & 66 deletions command/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cli/cli/internal/run"
"github.com/cli/cli/test"
"github.com/google/go-cmp/cmp"
)

func TestIssueStatus(t *testing.T) {
Expand Down Expand Up @@ -284,81 +285,77 @@ func TestIssueView_web_numberArgWithHash(t *testing.T) {
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
}

func TestIssueView(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
"number": 123,
"body": "**bold story**",
"title": "ix of coins",
"author": {
"login": "marseilles"
func TestIssueView_Preview(t *testing.T) {
tests := map[string]struct {
ownerRepo string
command string
fixture string
expectedOutputs []string
}{
"Open issue": {
ownerRepo: "master",
command: "issue view 123",
fixture: "../test/fixtures/issueView_preview.json",
expectedOutputs: []string{
"ix of coins",
"Open • marseilles opened about 292 years ago • 9 comments",
"bold story",
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
},
},
"labels": {
"nodes": [
{"name": "tarot"}
]
"Open issue with no label": {
ownerRepo: "master",
command: "issue view 123",
fixture: "../test/fixtures/issueView_previewNoLabel.json",
expectedOutputs: []string{
"ix of coins",
"Open • marseilles opened about 292 years ago • 9 comments",
"bold story",
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
},
},
"comments": {
"totalCount": 9
"Open issue with empty body": {
ownerRepo: "master",
command: "issue view 123",
fixture: "../test/fixtures/issueView_previewWithEmptyBody.json",
expectedOutputs: []string{
"ix of coins",
"Open • marseilles opened about 292 years ago • 9 comments",
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
},
},
"Closed issue": {
ownerRepo: "master",
command: "issue view 123",
fixture: "../test/fixtures/issueView_previewClosedState.json",
expectedOutputs: []string{
"ix of coins",
"Closed • marseilles opened about 292 years ago • 9 comments",
"bold story",
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
},
},
"url": "https://github.com/OWNER/REPO/issues/123"
} } } }
`))

output, err := RunCommand(issueViewCmd, "issue view 123")
if err != nil {
t.Errorf("error running command `issue view`: %v", err)
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
initBlankContext("OWNER/REPO", tc.ownerRepo)
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

eq(t, output.Stderr(), "")

test.ExpectLines(t, output.String(),
"ix of coins",
`opened by marseilles. 9 comments. \(tarot\)`,
"bold story",
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
}
jsonFile, _ := os.Open(tc.fixture)
defer jsonFile.Close()
http.StubResponse(200, jsonFile)

func TestIssueView_WithEmptyBody(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
output, err := RunCommand(issueViewCmd, tc.command)
if err != nil {
t.Errorf("error running command `%v`: %v", tc.command, err)
}

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
"number": 123,
"body": "",
"title": "ix of coins",
"author": {
"login": "marseilles"
},
"labels": {
"nodes": [
{"name": "tarot"}
]
},
"comments": {
"totalCount": 9
},
"url": "https://github.com/OWNER/REPO/issues/123"
} } } }
`))
eq(t, output.Stderr(), "")

output, err := RunCommand(issueViewCmd, "issue view 123")
if err != nil {
t.Errorf("error running command `issue view`: %v", err)
test.ExpectLines(t, output.String(), tc.expectedOutputs...)
})
}

eq(t, output.Stderr(), "")

test.ExpectLines(t, output.String(),
"ix of coins",
`opened by marseilles. 9 comments. \(tarot\)`,
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
}

func TestIssueView_web_notFound(t *testing.T) {
Expand Down Expand Up @@ -659,3 +656,23 @@ func Test_listHeader(t *testing.T) {
})
}
}

func TestIssueStateTitleWithColor(t *testing.T) {
tests := map[string]struct {
state string
want string
}{
"Open state": {state: "OPEN", want: "Open"},
"Closed state": {state: "CLOSED", want: "Closed"},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := issueStateTitleWithColor(tc.state)
diff := cmp.Diff(tc.want, got)
if diff != "" {
t.Fatalf(diff)
}
})
}
}
18 changes: 13 additions & 5 deletions command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,22 @@ func prList(cmd *cobra.Command, args []string) error {
return nil
}

func prStateTitleWithColor(pr api.PullRequest) string {
prStateColorFunc := colorFuncForPR(pr)
if pr.State == "OPEN" && pr.IsDraft {
return prStateColorFunc(strings.Title(strings.ToLower("Draft")))
}
return prStateColorFunc(strings.Title(strings.ToLower(pr.State)))
}

func colorFuncForPR(pr api.PullRequest) func(string) string {
if pr.State == "OPEN" && pr.IsDraft {
return utils.Gray
} else {
return colorFuncForState(pr.State)
}
return colorFuncForState(pr.State)
}

// colorFuncForState returns a color function for a PR/Issue state
func colorFuncForState(state string) func(string) string {
switch state {
case "OPEN":
Expand Down Expand Up @@ -320,8 +328,9 @@ func prView(cmd *cobra.Command, args []string) error {

func printPrPreview(out io.Writer, pr *api.PullRequest) error {
fmt.Fprintln(out, utils.Bold(pr.Title))
fmt.Fprintf(out, "%s", prStateTitleWithColor(*pr))
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
"%s wants to merge %s into %s from %s",
"%s wants to merge %s into %s from %s",
pr.Author.Login,
utils.Pluralize(pr.Commits.TotalCount, "commit"),
pr.BaseRefName,
Expand Down Expand Up @@ -453,8 +462,7 @@ func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
fmt.Fprint(w, utils.Green("✓ Approved"))
}
} else {
s := strings.Title(strings.ToLower(pr.State))
fmt.Fprintf(w, " - %s", prStateColorFunc(s))
fmt.Fprintf(w, " - %s", prStateTitleWithColor(pr))
}

fmt.Fprint(w, "\n")
Expand Down
Loading

0 comments on commit e10ccef

Please sign in to comment.