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