Merge pull request #104 from github/issue-list-table

Align `issue list` output with that of `pr list`
This commit is contained in:
Mislav Marohnić 2019-11-25 11:22:43 +01:00 committed by GitHub
commit 704a9e88a5
5 changed files with 84 additions and 111 deletions

View file

@ -12,30 +12,24 @@ type IssuesPayload struct {
}
type Issue struct {
Number int
Title string
URL string
Labels []string
TotalLabelCount int
Number int
Title string
URL string
State string
Labels struct {
Nodes []IssueLabel
TotalCount int
}
}
type IssueLabel struct {
Name string
}
type apiIssues struct {
Issues struct {
Edges []struct {
Node struct {
Number int
Title string
URL string
Labels struct {
Edges []struct {
Node struct {
Name string
}
}
TotalCount int
}
}
}
Nodes []Issue
}
}
@ -43,11 +37,11 @@ const fragments = `
fragment issue on Issue {
number
title
url
state
labels(first: 3) {
edges {
node {
name
}
nodes {
name
}
totalCount
}
@ -104,28 +98,22 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa
query($owner: String!, $repo: String!, $since: DateTime!, $viewer: String!, $per_page: Int = 10) {
assigned: repository(owner: $owner, name: $repo) {
issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
node {
...issue
}
nodes {
...issue
}
}
}
mentioned: repository(owner: $owner, name: $repo) {
issues(filterBy: {mentioned: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
node {
...issue
}
nodes {
...issue
}
}
}
recent: repository(owner: $owner, name: $repo) {
issues(filterBy: {since: $since, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
node {
...issue
}
nodes {
...issue
}
}
}
@ -148,14 +136,10 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa
return nil, err
}
assigned := convertAPIToIssues(resp.Assigned)
mentioned := convertAPIToIssues(resp.Mentioned)
recent := convertAPIToIssues(resp.Recent)
payload := IssuesPayload{
assigned,
mentioned,
recent,
Assigned: resp.Assigned.Issues.Nodes,
Mentioned: resp.Mentioned.Issues.Nodes,
Recent: resp.Recent.Issues.Nodes,
}
return &payload, nil
@ -191,10 +175,8 @@ func IssueList(client *Client, ghRepo Repo, state string, labels []string, assig
query($owner: String!, $repo: String!, $limit: Int, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) {
repository(owner: $owner, name: $repo) {
issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) {
edges {
node {
...issue
}
nodes {
...issue
}
}
}
@ -221,27 +203,9 @@ func IssueList(client *Client, ghRepo Repo, state string, labels []string, assig
return nil, err
}
issues := convertAPIToIssues(resp.Repository)
return issues, nil
}
func convertAPIToIssues(i apiIssues) []Issue {
var issues []Issue
for _, edge := range i.Issues.Edges {
var labels []string
for _, labelEdge := range edge.Node.Labels.Edges {
labels = append(labels, labelEdge.Node.Name)
}
issue := Issue{
Number: edge.Node.Number,
Title: edge.Node.Title,
URL: edge.Node.URL,
Labels: labels,
TotalLabelCount: edge.Node.Labels.TotalCount,
}
issues := []Issue{}
for _, issue := range resp.Repository.Issues.Nodes {
issues = append(issues, issue)
}
return issues
return issues, nil
}

View file

@ -92,12 +92,28 @@ func issueList(cmd *cobra.Command, args []string) error {
return err
}
if len(issues) > 0 {
printIssues("", issues...)
} else {
message := fmt.Sprintf("There are no open issues")
printMessage(message)
if len(issues) == 0 {
printMessage("There are no open issues")
return nil
}
table := utils.NewTablePrinter(cmd.OutOrStdout())
for _, issue := range issues {
issueNum := strconv.Itoa(issue.Number)
if table.IsTTY() {
issueNum = "#" + issueNum
}
labels := labelList(issue)
if labels != "" && table.IsTTY() {
labels = fmt.Sprintf("(%s)", labels)
}
table.AddField(issueNum, nil, colorFuncForState(issue.State))
table.AddField(issue.Title, nil, nil)
table.AddField(labels, nil, utils.Gray)
table.EndRow()
}
table.Render()
return nil
}
@ -245,14 +261,27 @@ func issueCreate(cmd *cobra.Command, args []string) error {
func printIssues(prefix string, issues ...api.Issue) {
for _, issue := range issues {
number := utils.Green("#" + strconv.Itoa(issue.Number))
var coloredLabels string
if len(issue.Labels) > 0 {
var ellipse string
if issue.TotalLabelCount > len(issue.Labels) {
ellipse = "…"
}
coloredLabels = utils.Gray(fmt.Sprintf(" (%s%s)", strings.Join(issue.Labels, ", "), ellipse))
coloredLabels := labelList(issue)
if coloredLabels != "" {
coloredLabels = utils.Gray(fmt.Sprintf(" (%s)", coloredLabels))
}
fmt.Printf("%s%s %s %s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
fmt.Printf("%s%s %s%s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
}
}
func labelList(issue api.Issue) string {
if len(issue.Labels.Nodes) == 0 {
return ""
}
labelNames := []string{}
for _, label := range issue.Labels.Nodes {
labelNames = append(labelNames, label.Name)
}
list := strings.Join(labelNames, ", ")
if issue.Labels.TotalCount > len(issue.Labels.Nodes) {
list += ", …"
}
return list
}

View file

@ -55,9 +55,9 @@ func TestIssueList(t *testing.T) {
}
expectedIssues := []*regexp.Regexp{
regexp.MustCompile(`#1.*won`),
regexp.MustCompile(`#2.*too`),
regexp.MustCompile(`#4.*fore`),
regexp.MustCompile(`(?m)^1\t.*won`),
regexp.MustCompile(`(?m)^2\t.*too`),
regexp.MustCompile(`(?m)^4\t.*fore`),
}
for _, r := range expectedIssues {

View file

@ -2,57 +2,45 @@
"data": {
"repository": {
"issues": {
"edges": [
"nodes": [
{
"node": {
"number": 1,
"title": "number won",
"url": "https://wow.com",
"labels": {
"edges": [
"nodes": [
{
"node": {
"name": "label"
}
}
],
"totalCount": 1
}
}
},
{
"node": {
"number": 2,
"title": "number too",
"url": "https://wow.com",
"labels": {
"edges": [
"nodes": [
{
"node": {
"name": "label"
}
}
],
"totalCount": 1
}
}
},
{
"node": {
"number": 4,
"title": "number fore",
"url": "https://wow.com",
"labels": {
"edges": [
"nodes": [
{
"node": {
"name": "label"
}
}
],
"totalCount": 1
}
}
}
]
}

View file

@ -2,43 +2,35 @@
"data": {
"assigned": {
"issues": {
"edges": [
"nodes": [
{
"node": {
"number": 9,
"title": "corey thinks squash tastes bad"
}
},
{
"node": {
"number": 10,
"title": "broccoli is a superfood"
}
}
]
}
},
"mentioned": {
"issues": {
"edges": [
"nodes": [
{
"node": {
"number": 8,
"title": "rabbits eat carrots"
}
},
{
"node": {
"number": 11,
"title": "swiss chard is neutral"
}
}
]
}
},
"recent": {
"issues": {
"edges": []
"nodes": []
}
},