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