forked from mislav/hub
/
issue.go
111 lines (92 loc) · 2.77 KB
/
issue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package commands
import (
"fmt"
"github.com/github/hub/github"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var (
cmdIssue = &Command{
Run: issue,
Usage: "issue",
Short: "List issues on GitHub",
Long: `List summary of the open issues for the project that the "origin" remote points to.`,
}
cmdCreateIssue = &Command{
Key: "create",
Run: createIssue,
Usage: "issue create [-m <MESSAGE>|-f <FILE>] [-l <LABEL-1>,<LABEL-2>...,<LABEL-N>]",
Short: "Create an issue on GitHub",
Long: `Create an issue for the project that the "origin" remote points to.
Without <MESSAGE> or <FILE>, a text editor will open in which title and body
of the release can be entered in the same manner as git commit message.
Specify one or more labels via "-l".
`,
}
flagIssueMessage,
flagIssueFile string
flagIssueLabels listFlag
)
func init() {
cmdCreateIssue.Flag.StringVarP(&flagIssueMessage, "message", "m", "", "MESSAGE")
cmdCreateIssue.Flag.StringVarP(&flagIssueFile, "file", "f", "", "FILE")
cmdCreateIssue.Flag.VarP(&flagIssueLabels, "label", "l", "LABEL")
cmdIssue.Use(cmdCreateIssue)
CmdRunner.Use(cmdIssue)
}
/*
$ hub issue
*/
func issue(cmd *Command, args *Args) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
ui.Printf("Would request list of issues for %s\n", project)
} else {
issues, err := gh.Issues(project)
utils.Check(err)
for _, issue := range issues {
var url string
// use the pull request URL if we have one
if issue.PullRequest.HTMLURL != "" {
url = issue.PullRequest.HTMLURL
} else {
url = issue.HTMLURL
}
// "nobody" should have more than 1 million github issues
ui.Printf("% 7d] %s ( %s )\n", issue.Number, issue.Title, url)
}
}
})
}
func createIssue(cmd *Command, args *Args) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
ui.Printf("Would create an issue for %s\n", project)
} else {
title, body, err := getTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
utils.Check(err)
if title == "" {
title, body, err = writeIssueTitleAndBody(project)
utils.Check(err)
}
issue, err := gh.CreateIssue(project, title, body, flagIssueLabels)
utils.Check(err)
ui.Println(issue.HTMLURL)
}
})
}
func writeIssueTitleAndBody(project *github.Project) (string, string, error) {
message := `
# Creating issue for %s.
#
# Write a message for this issue. The first block of
# text is the title and the rest is the description.
`
message = fmt.Sprintf(message, project.Name)
editor, err := github.NewEditor("ISSUE", "issue", message)
if err != nil {
return "", "", err
}
defer editor.DeleteFile()
return editor.EditTitleAndBody()
}