Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp hub issue commands #1099

Merged
merged 23 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8563e67
Reimplement `hub issues` commands using simpleApi
mislav Feb 3, 2016
486df9e
Print colored labels for `hub issue`
mislav Feb 3, 2016
2a6c091
First version of a formatter that works like `git log --format`.
pcorpet Feb 18, 2016
0746859
Add padding in the expander.
pcorpet Feb 18, 2016
87f7e14
Split huge test in smaller ones.
pcorpet Feb 28, 2016
eafcc38
Add more colors
pcorpet Feb 28, 2016
c142db3
Use formatter when listing issues.
pcorpet May 11, 2016
2455fa5
Account for nil User as well.
pcorpet May 12, 2016
9531c26
Fix integration test and add one more
pcorpet May 12, 2016
15d5f40
Merge pull request #1119 from pcorpet/formatter
mislav Aug 7, 2016
9f83b38
Merge remote-tracking branch 'origin/master' into revamp-issues
mislav Aug 14, 2016
efa3ce7
Consolidate `github.PullRequest` structs
mislav Aug 14, 2016
df990c4
Fix copy/pasta in `issue` flags
mislav Aug 15, 2016
6f584d7
Remove unnecessary `IssueParams` struct
mislav Aug 15, 2016
4c02e6a
Add assignees & milestone support to `issue create`
mislav Aug 15, 2016
d2792ba
Switch `pull-request` assignees and labels flags to `listFlag`
mislav Aug 15, 2016
ac9ff97
Add `issue create --browse` support to reflect `pull-request`
mislav Aug 15, 2016
15b8637
Refactor some `--format` placeholders
mislav Aug 15, 2016
ba263fd
Add created/updated dates to `issue` formatting options
mislav Aug 16, 2016
24ed64e
Add relative date format to `issue` output
mislav Aug 16, 2016
28f8729
Have `issue` command fetch all pages of API results
mislav Aug 16, 2016
35e9dfe
Fix test broken by verbose output change
mislav Aug 16, 2016
cb8c17e
Avoid using timezone abbreviations such as "PST" or "CEST"
mislav Aug 16, 2016
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
354 changes: 290 additions & 64 deletions commands/issue.go

Large diffs are not rendered by default.

309 changes: 309 additions & 0 deletions commands/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
package commands

import (
"testing"
"time"

"github.com/github/hub/github"
)

type formatIssueTest struct {
name string
issue github.Issue
format string
colorize bool
expect string
}

func testFormatIssue(t *testing.T, tests []formatIssueTest) {
for _, test := range tests {
if got := formatIssue(test.issue, test.format, test.colorize); got != test.expect {
t.Errorf("%s: formatIssue(..., %q, %t) = %q, want %q", test.name, test.format, test.colorize, got, test.expect)
}
}
}

func TestFormatIssue(t *testing.T) {
format := "%sC%>(8)%i%Creset %t% l%n"
testFormatIssue(t, []formatIssueTest{
{
name: "standard usage",
issue: github.Issue{
Number: 42,
Title: "Just an Issue",
State: "open",
User: &github.User{Login: "pcorpet"},
Body: "Body of the\nissue",
Assignees: []github.User{{Login: "mislav"}},
},
format: format,
colorize: true,
expect: "\033[32m #42\033[m Just an Issue\n",
},
{
name: "closed issue colored differently",
issue: github.Issue{
Number: 42,
Title: "Just an Issue",
State: "closed",
User: &github.User{Login: "octocat"},
},
format: format,
colorize: true,
expect: "\033[31m #42\033[m Just an Issue\n",
},
{
name: "labels",
issue: github.Issue{
Number: 42,
Title: "An issue with labels",
State: "open",
User: &github.User{Login: "octocat"},
Labels: []github.IssueLabel{
{Name: "bug", Color: "800000"},
{Name: "reproduced", Color: "55ff55"},
},
},
format: format,
colorize: true,
expect: "\033[32m #42\033[m An issue with labels \033[38;5;15;48;2;128;0;0m bug \033[m \033[38;5;16;48;2;85;255;85m reproduced \033[m\n",
},
{
name: "not colorized",
issue: github.Issue{
Number: 42,
Title: "Just an Issue",
State: "open",
User: &github.User{Login: "octocat"},
},
format: format,
colorize: false,
expect: " #42 Just an Issue\n",
},
{
name: "labels not colorized",
issue: github.Issue{
Number: 42,
Title: "An issue with labels",
State: "open",
User: &github.User{Login: "octocat"},
Labels: []github.IssueLabel{
{Name: "bug", Color: "880000"},
{Name: "reproduced", Color: "55ff55"},
},
},
format: format,
colorize: false,
expect: " #42 An issue with labels bug reproduced \n",
},
})
}

func TestFormatIssue_customFormatString(t *testing.T) {
createdAt, err := time.Parse(time.RFC822Z, "16 Mar 15 12:34 +0000")
if err != nil {
t.Fatal(err)
}
updatedAt, err := time.Parse(time.RFC822Z, "17 Mar 15 12:34 +0900")
if err != nil {
t.Fatal(err)
}

issue := github.Issue{
Number: 42,
Title: "Just an Issue",
State: "open",
User: &github.User{Login: "pcorpet"},
Body: "Body of the\nissue",
Assignees: []github.User{
{Login: "mislav"},
{Login: "josh"},
},
Labels: []github.IssueLabel{
{Name: "bug", Color: "880000"},
{Name: "feature", Color: "008800"},
},
HtmlUrl: "the://url",
Comments: 12,
Milestone: &github.Milestone{
Number: 31,
Title: "2.2-stable",
},
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}

testFormatIssue(t, []formatIssueTest{
{
name: "number",
issue: issue,
format: "%I",
colorize: true,
expect: "42",
},
{
name: "hashed number",
issue: issue,
format: "%i",
colorize: true,
expect: "#42",
},
{
name: "state as text",
issue: issue,
format: "%S",
colorize: true,
expect: "open",
},
{
name: "state as color switch",
issue: issue,
format: "%sC",
colorize: true,
expect: "\033[32m",
},
{
name: "state as color switch non colorized",
issue: issue,
format: "%sC",
colorize: false,
expect: "",
},
{
name: "title",
issue: issue,
format: "%t",
colorize: true,
expect: "Just an Issue",
},
{
name: "label colorized",
issue: issue,
format: "%l",
colorize: true,
expect: "\033[38;5;15;48;2;136;0;0m bug \033[m \033[38;5;15;48;2;0;136;0m feature \033[m",
},
{
name: "label not colorized",
issue: issue,
format: "%l",
colorize: false,
expect: " bug feature ",
},
{
name: "raw labels",
issue: issue,
format: "%L",
colorize: true,
expect: "bug, feature",
},
{
name: "body",
issue: issue,
format: "%b",
colorize: true,
expect: "Body of the\nissue",
},
{
name: "user login",
issue: issue,
format: "%au",
colorize: true,
expect: "pcorpet",
},
{
name: "assignee login",
issue: issue,
format: "%as",
colorize: true,
expect: "mislav, josh",
},
{
name: "assignee login but not assigned",
issue: github.Issue{
State: "open",
User: &github.User{Login: "pcorpet"},
},
format: "%as",
colorize: true,
expect: "",
},
{
name: "milestone number",
issue: issue,
format: "%Mn",
colorize: true,
expect: "31",
},
{
name: "milestone title",
issue: issue,
format: "%Mt",
colorize: true,
expect: "2.2-stable",
},
{
name: "comments number",
issue: issue,
format: "%Nc",
colorize: true,
expect: "(12)",
},
{
name: "raw comments number",
issue: issue,
format: "%NC",
colorize: true,
expect: "12",
},
{
name: "issue URL",
issue: issue,
format: "%U",
colorize: true,
expect: "the://url",
},
{
name: "created date",
issue: issue,
format: "%cD",
colorize: true,
expect: "16 Mar 2015",
},
{
name: "created time ISO 8601",
issue: issue,
format: "%cI",
colorize: true,
expect: "2015-03-16T12:34:00Z",
},
{
name: "created time Unix",
issue: issue,
format: "%ct",
colorize: true,
expect: "1426509240",
},
{
name: "updated date",
issue: issue,
format: "%uD",
colorize: true,
expect: "17 Mar 2015",
},
{
name: "updated time ISO 8601",
issue: issue,
format: "%uI",
colorize: true,
expect: "2015-03-17T12:34:00+09:00",
},
{
name: "updated time Unix",
issue: issue,
format: "%ut",
colorize: true,
expect: "1426563240",
},
})
}
40 changes: 13 additions & 27 deletions commands/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ var (
flagPullRequestHead,
flagPullRequestIssue,
flagPullRequestMessage,
flagPullRequestAssignee,
flagPullRequestLabels,
flagPullRequestFile string

flagPullRequestBrowse,
flagPullRequestForce bool

flagPullRequestMilestone uint64

flagPullRequestAssignees,
flagPullRequestLabels listFlag
)

func init() {
Expand All @@ -81,9 +84,9 @@ func init() {
cmdPullRequest.Flag.StringVarP(&flagPullRequestMessage, "message", "m", "", "MESSAGE")
cmdPullRequest.Flag.BoolVarP(&flagPullRequestForce, "force", "f", false, "FORCE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestFile, "file", "F", "", "FILE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestAssignee, "assign", "a", "", "USER")
cmdPullRequest.Flag.VarP(&flagPullRequestAssignees, "assign", "a", "USERS")
cmdPullRequest.Flag.Uint64VarP(&flagPullRequestMilestone, "milestone", "M", 0, "MILESTONE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestLabels, "labels", "l", "", "LABELS")
cmdPullRequest.Flag.VarP(&flagPullRequestLabels, "labels", "l", "LABELS")

CmdRunner.Use(cmdPullRequest)
}
Expand Down Expand Up @@ -231,35 +234,18 @@ func pullRequest(cmd *Command, args *Args) {

utils.Check(err)

pullRequestURL = pr.HTMLURL

if flagPullRequestAssignee != "" || flagPullRequestMilestone > 0 ||
flagPullRequestLabels != "" {
pullRequestURL = pr.HtmlUrl

assignees := []string{}
for _, assignee := range strings.Split(flagPullRequestAssignee, ",") {
if assignee != "" {
assignees = append(assignees, assignee)
}
}

labels := []string{}
for _, label := range strings.Split(flagPullRequestLabels, ",") {
if label != "" {
labels = append(labels, label)
}
}
if len(flagPullRequestAssignees) > 0 || flagPullRequestMilestone > 0 ||
len(flagPullRequestLabels) > 0 {

params := map[string]interface{}{}
if len(assignees) > 0 {
params["assignees"] = assignees
params := map[string]interface{}{
"labels": flagPullRequestLabels,
"assignees": flagPullRequestAssignees,
}
if flagPullRequestMilestone > 0 {
params["milestone"] = flagPullRequestMilestone
}
if len(labels) > 0 {
params["labels"] = labels
}

err = client.UpdateIssue(baseProject, pr.Number, params)
utils.Check(err)
Expand Down
1 change: 1 addition & 0 deletions features/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Feature: hub create
"""
> GET https://api.github.com/repos/mislav/dotfiles
> Authorization: token [REDACTED]
> Accept: application/vnd.github.v3+json;charset=utf-8
< HTTP 404
"""
And the stderr should contain:
Expand Down
Loading