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