Skip to content

Commit

Permalink
Merge pull request cli#745 from doi-t/add-metadata-to-view
Browse files Browse the repository at this point in the history
Add relevant metadata to issue view in CLI
  • Loading branch information
Nate Smith authored Apr 9, 2020
2 parents d14b5d3 + 054ec3c commit c670049
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 62 deletions.
59 changes: 49 additions & 10 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type IssuesAndTotalCount struct {
TotalCount int
}

// Ref. https://developer.github.com/v4/object/issue/
type Issue struct {
Number int
Title string
Expand All @@ -32,15 +33,32 @@ type Issue struct {
Author struct {
Login string
}

Assignees struct {
Nodes []struct {
Login string
}
TotalCount int
}
Labels struct {
Nodes []IssueLabel
Nodes []struct {
Name string
}
TotalCount int
}
}

type IssueLabel struct {
Name string
ProjectCards struct {
Nodes []struct {
Project struct {
Name string
}
Column struct {
Name string
}
}
TotalCount int
}
Milestone struct {
Title string
}
}

const fragments = `
Expand Down Expand Up @@ -287,14 +305,35 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
comments {
totalCount
}
labels(first: 3) {
number
url
createdAt
assignees(first: 100) {
nodes {
login
}
totalCount
}
labels(first: 100) {
nodes {
name
}
totalCount
}
projectCards(first: 100) {
nodes {
project {
name
}
column {
name
}
}
totalCount
}
milestone{
title
}
number
url
createdAt
}
}
}`
Expand Down
64 changes: 60 additions & 4 deletions command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,47 @@ func printIssuePreview(out io.Writer, issue *api.Issue) error {
now := time.Now()
ago := now.Sub(issue.CreatedAt)

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

// Metadata
fmt.Fprintln(out)
if assignees := issueAssigneeList(*issue); assignees != "" {
fmt.Fprint(out, utils.Bold("Assignees: "))
fmt.Fprintln(out, assignees)
}
if labels := issueLabelList(*issue); labels != "" {
fmt.Fprint(out, utils.Bold("Labels: "))
fmt.Fprintln(out, labels)
}
if projects := issueProjectList(*issue); projects != "" {
fmt.Fprint(out, utils.Bold("Projects: "))
fmt.Fprintln(out, projects)
}
if issue.Milestone.Title != "" {
fmt.Fprint(out, utils.Bold("Milestone: "))
fmt.Fprintln(out, issue.Milestone.Title)
}

// Body
if issue.Body != "" {
fmt.Fprintln(out)
md, err := utils.RenderMarkdown(issue.Body)
if err != nil {
return err
}
fmt.Fprintln(out, md)
fmt.Fprintln(out)
}
fmt.Fprintln(out)

// Footer
fmt.Fprintf(out, utils.Gray("View this issue on GitHub: %s\n"), issue.URL)
return nil
}
Expand Down Expand Up @@ -422,7 +444,7 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue)
issueNum = "#" + issueNum
}
issueNum = prefix + issueNum
labels := labelList(issue)
labels := issueLabelList(issue)
if labels != "" && table.IsTTY() {
labels = fmt.Sprintf("(%s)", labels)
}
Expand All @@ -441,7 +463,24 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue)
}
}

func labelList(issue api.Issue) string {
func issueAssigneeList(issue api.Issue) string {
if len(issue.Assignees.Nodes) == 0 {
return ""
}

AssigneeNames := make([]string, 0, len(issue.Assignees.Nodes))
for _, assignee := range issue.Assignees.Nodes {
AssigneeNames = append(AssigneeNames, assignee.Login)
}

list := strings.Join(AssigneeNames, ", ")
if issue.Assignees.TotalCount > len(issue.Assignees.Nodes) {
list += ", …"
}
return list
}

func issueLabelList(issue api.Issue) string {
if len(issue.Labels.Nodes) == 0 {
return ""
}
Expand All @@ -458,6 +497,23 @@ func labelList(issue api.Issue) string {
return list
}

func issueProjectList(issue api.Issue) string {
if len(issue.ProjectCards.Nodes) == 0 {
return ""
}

projectNames := make([]string, 0, len(issue.ProjectCards.Nodes))
for _, project := range issue.ProjectCards.Nodes {
projectNames = append(projectNames, fmt.Sprintf("%s (%s)", project.Project.Name, project.Column.Name))
}

list := strings.Join(projectNames, ", ")
if issue.ProjectCards.TotalCount > len(issue.ProjectCards.Nodes) {
list += ", …"
}
return list
}

func displayURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
Expand Down
40 changes: 22 additions & 18 deletions command/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,47 +292,51 @@ func TestIssueView_Preview(t *testing.T) {
fixture string
expectedOutputs []string
}{
"Open issue": {
"Open issue without metadata": {
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",
`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`,
},
},
"Open issue with no label": {
"Open issue with metadata": {
ownerRepo: "master",
command: "issue view 123",
fixture: "../test/fixtures/issueView_previewNoLabel.json",
fixture: "../test/fixtures/issueView_previewWithMetadata.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",
`ix of coins`,
`Open • marseilles opened about 292 years ago • 9 comments`,
`Assignees: marseilles, monaco\n`,
`Labels: one, two, three, four, five\n`,
`Projects: Project 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\)\n`,
`Milestone: uluru\n`,
`bold story`,
`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`,
},
},
"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",
`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",
`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`,
},
},
}
Expand Down
18 changes: 13 additions & 5 deletions test/fixtures/issueView_preview.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
"author": {
"login": "marseilles"
},
"assignees": {
"nodes": [],
"totalcount": 0
},
"labels": {
"nodes": [
{
"name": "tarot"
}
]
"nodes": [],
"totalcount": 0
},
"projectcards": {
"nodes": [],
"totalcount": 0
},
"milestone": {
"title": ""
},
"comments": {
"totalCount": 9
Expand Down
25 changes: 0 additions & 25 deletions test/fixtures/issueView_previewNoLabel.json

This file was deleted.

Loading

0 comments on commit c670049

Please sign in to comment.