issue.go 2.7 KB
Newer Older
1 2 3 4
package commands

import (
	"fmt"
J
Jingwen Owen Ou 已提交
5 6 7

	"github.com/github/hub/github"
	"github.com/github/hub/utils"
8 9
)

D
David Calavera 已提交
10 11 12 13 14
var (
	cmdIssue = &Command{
		Run:   issue,
		Usage: "issue",
		Short: "List issues on GitHub",
J
Jingwen Owen Ou 已提交
15
		Long:  `List summary of the open issues for the project that the "origin" remote points to.`,
D
David Calavera 已提交
16 17 18 19 20
	}

	cmdCreateIssue = &Command{
		Key:   "create",
		Run:   createIssue,
J
Jingwen Owen Ou 已提交
21
		Usage: "issue create [-m <MESSAGE>|-f <FILE>] [-l <LABEL-1>,<LABEL-2>...,<LABEL-N>]",
D
David Calavera 已提交
22
		Short: "Create an issue on GitHub",
J
Jingwen Owen Ou 已提交
23 24 25 26 27 28 29
		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 "-a".
`,
D
David Calavera 已提交
30 31 32 33 34 35 36
	}

	flagIssueMessage,
	flagIssueFile string

	flagIssueLabels listFlag
)
37

38
func init() {
D
David Calavera 已提交
39 40 41 42 43
	cmdCreateIssue.Flag.StringVarP(&flagIssueMessage, "message", "m", "", "MESSAGE")
	cmdCreateIssue.Flag.StringVarP(&flagIssueFile, "file", "f", "", "FILE")
	cmdCreateIssue.Flag.VarP(&flagIssueLabels, "label", "l", "LABEL")

	cmdIssue.Use(cmdCreateIssue)
44 45 46
	CmdRunner.Use(cmdIssue)
}

47
/*
J
Jingwen Owen Ou 已提交
48
  $ gh issue
49
*/
J
Jingwen Owen Ou 已提交
50
func issue(cmd *Command, args *Args) {
51
	runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
D
David Calavera 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		if args.Noop {
			fmt.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
				fmt.Printf("% 7d] %s ( %s )\n", issue.Number, issue.Title, url)
67 68
			}
		}
D
David Calavera 已提交
69 70 71 72
	})
}

func createIssue(cmd *Command, args *Args) {
73
	runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
D
David Calavera 已提交
74 75 76
		if args.Noop {
			fmt.Printf("Would create an issue for %s\n", project)
		} else {
77
			title, body, err := getTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
D
David Calavera 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
			utils.Check(err)

			if title == "" {
				title, body, err = writeIssueTitleAndBody(project)
				utils.Check(err)
			}

			issue, err := gh.CreateIssue(project, title, body, flagIssueLabels)
			utils.Check(err)

			fmt.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
98
# of text is the title and the rest is description.
D
David Calavera 已提交
99 100
`
	message = fmt.Sprintf(message, project.Name)
101

102
	editor, err := github.NewEditor("ISSUE", "issue", message)
103 104 105 106
	if err != nil {
		return "", "", err
	}

107 108
	defer editor.DeleteFile()

109
	return editor.EditTitleAndBody()
110
}