Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
94a1e6a
Add 'state' and 'createdAt' to the query for IssueByNumber
doi-t Mar 15, 2020
d429bab
Add 'state' to queries for pull requests
doi-t Mar 15, 2020
faa4bed
Move colorFuncForState for a PR/issue state to the utils package
doi-t Mar 15, 2020
caf26e4
Show the issue state for viewing issues in CLI
doi-t Mar 15, 2020
b8edb9f
Show the PR state for viewing PRs in CLI
doi-t Mar 15, 2020
e0bfd67
Fix tests for preview messages with a issue/PR state
doi-t Mar 15, 2020
0475cf0
Extract test fixtures for 'issue view --preview'
doi-t Mar 15, 2020
8cd6932
Add a test for the closed issue preview
doi-t Mar 15, 2020
5a23113
Add tests for the Closed/Merged PR preview
doi-t Mar 15, 2020
c8a72c3
Add a test for viewing an issue without labels in CLI
doi-t Mar 15, 2020
9a6026e
Format PR/Issue states with color
doi-t Mar 15, 2020
e85259d
Remove labels from the issue preview header
doi-t Mar 17, 2020
46a632c
Fix utility function scopes
doi-t Mar 17, 2020
0cfa4be
Add tests for utilities of PR/issue state format
doi-t Mar 17, 2020
7b5a0b5
Add a missing isDraft for querying PR by number
doi-t Mar 17, 2020
762e806
Add tests for viewing a Draft PR in CLI
doi-t Mar 17, 2020
7ceffd0
Merge branch 'master' into view-the-current-state
doi-t Mar 17, 2020
cf69d7e
Add test fixtures for viewing Draft state in CLI
doi-t Mar 17, 2020
e50ba54
Rename OPEN state to Draft for a Draft PR
doi-t Mar 18, 2020
327a804
Fix tests to support the "Draft" state badge
doi-t Mar 18, 2020
a5bd313
Cleanup
doi-t Mar 18, 2020
1a5e9f1
Apply table driven testing for pr/issue preview commands
doi-t Mar 20, 2020
0ba0a07
Merge branch 'master' into view-the-current-state
doi-t Mar 20, 2020
7d42f46
Merge branch 'master' into view-the-current-state
doi-t Mar 25, 2020
0095fe9
Merge branch 'master' into view-the-current-state
doi-t Apr 2, 2020
4c2f15f
Roll back the place of colorFuncForState
doi-t Apr 2, 2020
7eabbf5
Simplify expected output definitions with new line testing helper
doi-t Apr 2, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Massive props for table-driven test 🥇

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL go-cmp! 🏆

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