issue.go 2.5 KB
Newer Older
1 2 3 4 5 6 7 8
package commands

import (
	"fmt"
	"github.com/jingweno/gh/github"
	"github.com/jingweno/gh/utils"
)

D
David Calavera 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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" remove points to.`,
	}

	cmdCreateIssue = &Command{
		Key:   "create",
		Run:   createIssue,
		Usage: "issue create [-m <MESSAGE>|-f <FILE>] -l LABEL1,LABEL2",
		Short: "Create an issue on GitHub",
		Long:  `Create an issue for the project that the "origin" remove points to.`,
	}

	flagIssueMessage,
	flagIssueFile string

	flagIssueLabels listFlag
)
30

31
func init() {
D
David Calavera 已提交
32 33 34 35 36
	cmdCreateIssue.Flag.StringVarP(&flagIssueMessage, "message", "m", "", "MESSAGE")
	cmdCreateIssue.Flag.StringVarP(&flagIssueFile, "file", "f", "", "FILE")
	cmdCreateIssue.Flag.VarP(&flagIssueLabels, "label", "l", "LABEL")

	cmdIssue.Use(cmdCreateIssue)
37 38 39
	CmdRunner.Use(cmdIssue)
}

40
/*
J
Jingwen Owen Ou 已提交
41
  $ gh issue
42
*/
J
Jingwen Owen Ou 已提交
43
func issue(cmd *Command, args *Args) {
44
	runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
D
David Calavera 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		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)
60 61
			}
		}
D
David Calavera 已提交
62 63 64 65
	})
}

func createIssue(cmd *Command, args *Args) {
66
	runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
D
David Calavera 已提交
67 68 69
		if args.Noop {
			fmt.Printf("Would create an issue for %s\n", project)
		} else {
70
			title, body, err := getTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
D
David Calavera 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
			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
# of the text is the title and the rest is description.
`
	message = fmt.Sprintf(message, project.Name)
94

95 96 97 98 99 100
	editor, err := github.NewEditor("ISSUE", message)
	if err != nil {
		return "", "", err
	}

	return editor.EditTitleAndBody()
101
}