提交 bb4e5f2d 编写于 作者: D David Calavera

Add command to create issues.

上级 755d067f
......@@ -4,17 +4,36 @@ import (
"fmt"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"os"
)
var cmdIssue = &Command{
Run: issue,
Usage: "issue",
Short: "Manipulate issues on GitHub",
Long: `Lists summary of the open issues for the project that the "origin" remove points to.`,
}
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
)
func init() {
cmdCreateIssue.Flag.StringVarP(&flagIssueMessage, "message", "m", "", "MESSAGE")
cmdCreateIssue.Flag.StringVarP(&flagIssueFile, "file", "f", "", "FILE")
cmdCreateIssue.Flag.VarP(&flagIssueLabels, "label", "l", "LABEL")
cmdIssue.Use(cmdCreateIssue)
CmdRunner.Use(cmdIssue)
}
......@@ -22,29 +41,56 @@ func init() {
$ gh issue
*/
func issue(cmd *Command, args *Args) {
localRepo := github.LocalRepo()
project, err := localRepo.CurrentProject()
utils.Check(err)
gh := github.NewClient(project.Host)
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
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
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)
}
// "nobody" should have more than 1 million github issues
fmt.Printf("% 7d] %s ( %s )\n", issue.Number, issue.Title, url)
}
}
})
}
func createIssue(cmd *Command, args *Args) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
fmt.Printf("Would create an issue for %s\n", project)
} else {
title, body, err := github.GetTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
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)
os.Exit(0)
return github.GetTitleAndBodyFromEditor("ISSUE", message)
}
......@@ -54,7 +54,7 @@ func init() {
}
func release(cmd *Command, args *Args) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
fmt.Printf("Would request list of releases for %s\n", project)
} else {
......@@ -82,7 +82,7 @@ func createRelease(cmd *Command, args *Args) {
assetsDir, err := getAssetsDirectory(flagReleaseAssetsDir, tag)
utils.Check(err)
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
currentBranch, err := localRepo.CurrentBranch()
utils.Check(err)
branchName := currentBranch.ShortName()
......@@ -124,17 +124,6 @@ func writeReleaseTitleAndBody(project *github.Project, tag, currentBranch string
return github.GetTitleAndBodyFromEditor("RELEASE", message)
}
func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
localRepo := github.LocalRepo()
project, err := localRepo.CurrentProject()
utils.Check(err)
client := github.NewClient(project.Host)
fn(localRepo, project, client)
os.Exit(0)
}
func getAssetsDirectory(assetsDir, tag string) (string, error) {
if assetsDir == "" {
pwd, err := os.Getwd()
......
......@@ -53,3 +53,27 @@ func isEmptyDir(path string) bool {
match, _ := filepath.Glob(fullPath)
return match == nil
}
func RunInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
localRepo := github.LocalRepo()
project, err := localRepo.CurrentProject()
utils.Check(err)
client := github.NewClient(project.Host)
fn(localRepo, project, client)
os.Exit(0)
}
type listFlag []string
func (l *listFlag) String() string {
return strings.Join([]string(*l), ",")
}
func (l *listFlag) Set(value string) error {
for _, flag := range strings.Split(value, ",") {
*l = append(*l, flag)
}
return nil
}
......@@ -206,6 +206,27 @@ func (client *Client) Issues(project *Project) (issues []octokit.Issue, err erro
return
}
func (client *Client) CreateIssue(project *Project, title, body string, labels []string) (issue *octokit.Issue, err error) {
url, err := octokit.RepoIssuesURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
params := octokit.IssueParams{
Title: title,
Body: body,
Labels: labels,
}
issue, result := client.octokit().Issues(client.requestURL(url)).Create(params)
if result.HasError() {
err = result.Err
return
}
return
}
func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) {
url, e := octokit.AuthorizationsURL.Expand(nil)
if e != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册