hub/commands/pull_request.go

306 строки
8.6 KiB
Go
Исходник Обычный вид История

2013-05-29 22:58:46 +04:00
package commands
2013-04-11 19:33:43 +04:00
import (
2013-04-29 00:41:45 +04:00
"fmt"
2013-04-24 14:00:45 +04:00
"regexp"
2013-04-28 21:45:00 +04:00
"strings"
2014-02-24 01:34:10 +04:00
"github.com/github/hub/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit"
2014-02-24 01:34:10 +04:00
"github.com/github/hub/git"
"github.com/github/hub/github"
"github.com/github/hub/utils"
2013-04-11 19:33:43 +04:00
)
var cmdPullRequest = &Command{
Run: pullRequest,
Usage: "pull-request [-f] [-m <MESSAGE>|-F <FILE>|-i <ISSUE>|<ISSUE-URL>] [-o] [-b <BASE>] [-h <HEAD>] [-a <USER>]",
2013-04-11 19:33:43 +04:00
Short: "Open a pull request on GitHub",
Long: `Opens a pull request on GitHub for the project that the "origin" remote
points to. The default head of the pull request is the current branch.
Both base and head of the pull request can be explicitly given in one of
the following formats: "branch", "owner:branch", "owner/repo:branch".
This command will abort operation if it detects that the current topic
branch has local commits that are not yet pushed to its upstream branch
2013-12-08 21:35:11 +04:00
on the remote. To skip this check, use "-f".
2013-04-11 19:33:43 +04:00
2013-12-08 21:35:11 +04:00
Without <MESSAGE> or <FILE>, a text editor will open in which title and body
of the pull request can be entered in the same manner as git commit message.
Pull request message can also be passed via stdin with "-F -".
2013-04-11 19:33:43 +04:00
2013-12-08 21:35:11 +04:00
If instead of normal <TITLE> an issue number is given with "-i", the pull
2013-04-11 19:33:43 +04:00
request will be attached to an existing GitHub issue. Alternatively, instead
of title you can paste a full URL to an issue on GitHub.
You can assign the new pull request to a user with "-a <USER>".
2013-04-11 19:33:43 +04:00
`,
}
2013-12-07 19:43:55 +04:00
var (
flagPullRequestBase,
flagPullRequestHead,
flagPullRequestIssue,
flagPullRequestMessage,
flagPullRequestAssignee,
flagPullRequestLabels,
2013-12-07 19:43:55 +04:00
flagPullRequestFile string
flagPullRequestBrowse,
2013-12-07 19:43:55 +04:00
flagPullRequestForce bool
flagPullRequestMilestone uint64
2013-12-07 19:43:55 +04:00
)
2013-04-11 19:33:43 +04:00
func init() {
cmdPullRequest.Flag.StringVarP(&flagPullRequestBase, "base", "b", "", "BASE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestHead, "head", "h", "", "HEAD")
cmdPullRequest.Flag.StringVarP(&flagPullRequestIssue, "issue", "i", "", "ISSUE")
cmdPullRequest.Flag.BoolVarP(&flagPullRequestBrowse, "browse", "o", false, "BROWSE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestMessage, "message", "m", "", "MESSAGE")
cmdPullRequest.Flag.BoolVarP(&flagPullRequestForce, "force", "f", false, "FORCE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestFile, "file", "F", "", "FILE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestAssignee, "assign", "a", "", "USER")
cmdPullRequest.Flag.Uint64VarP(&flagPullRequestMilestone, "milestone", "M", 0, "MILESTONE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestLabels, "labels", "l", "", "LABELS")
CmdRunner.Use(cmdPullRequest)
2013-04-11 19:33:43 +04:00
}
2013-07-06 00:45:22 +04:00
/*
# while on a topic branch called "feature":
$ gh pull-request
[ opens text editor to edit title & body for the request ]
[ opened pull request on GitHub for "YOUR_USER:feature" ]
# explicit pull base & head:
$ gh pull-request -b jingweno:master -h jingweno:feature
$ gh pull-request -m "title\n\nbody"
[ create pull request with title & body ]
2013-07-06 00:45:22 +04:00
$ gh pull-request -i 123
[ attached pull request to issue #123 ]
$ gh pull-request https://github.com/jingweno/gh/pull/123
[ attached pull request to issue #123 ]
2013-12-08 21:35:11 +04:00
$ gh pull-request -F FILE
[ create pull request with title & body from FILE ]
2013-07-06 00:45:22 +04:00
*/
func pullRequest(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
currentBranch, err := localRepo.CurrentBranch()
2013-12-06 22:54:11 +04:00
utils.Check(err)
baseProject, err := localRepo.MainProject()
2013-12-06 22:54:11 +04:00
utils.Check(err)
host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
if err != nil {
utils.Check(github.FormatError("creating pull request", err))
}
trackedBranch, headProject, err := localRepo.RemoteBranchAndProject(host.User, false)
utils.Check(err)
var (
base, head string
force bool
)
2013-12-08 12:33:48 +04:00
2013-12-07 19:43:55 +04:00
force = flagPullRequestForce
2013-12-08 12:33:48 +04:00
if flagPullRequestBase != "" {
2013-12-08 12:33:48 +04:00
baseProject, base = parsePullRequestProject(baseProject, flagPullRequestBase)
}
if flagPullRequestHead != "" {
2013-12-08 12:33:48 +04:00
headProject, head = parsePullRequestProject(headProject, flagPullRequestHead)
}
2013-07-02 22:56:45 +04:00
if args.ParamsSize() == 1 {
arg := args.RemoveParam(0)
2013-12-08 12:33:48 +04:00
flagPullRequestIssue = parsePullRequestIssueNumber(arg)
}
if base == "" {
masterBranch := localRepo.MasterBranch()
base = masterBranch.ShortName()
}
2014-03-05 20:37:17 +04:00
if head == "" && trackedBranch != nil {
if !trackedBranch.IsRemote() {
// the current branch tracking another branch
// pretend there's no upstream at all
trackedBranch = nil
} else {
if baseProject.SameAs(headProject) && base == trackedBranch.ShortName() {
2013-12-08 12:33:48 +04:00
e := fmt.Errorf(`Aborted: head branch is the same as base ("%s")`, base)
e = fmt.Errorf("%s\n(use `-h <branch>` to specify an explicit pull request head)", e)
utils.Check(e)
}
}
2014-03-05 20:37:17 +04:00
}
2014-03-05 20:39:23 +04:00
if head == "" {
if trackedBranch == nil {
head = currentBranch.ShortName()
} else {
head = trackedBranch.ShortName()
}
}
title, body, err := getTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
utils.Check(err)
fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base)
fullHead := fmt.Sprintf("%s:%s", headProject.Owner, head)
if !force && trackedBranch != nil {
remoteCommits, _ := git.RefList(trackedBranch.LongName(), "")
if len(remoteCommits) > 0 {
err = fmt.Errorf("Aborted: %d commits are not yet pushed to %s", len(remoteCommits), trackedBranch.LongName())
err = fmt.Errorf("%s\n(use `-f` to force submit a pull request anyway)", err)
utils.Check(err)
}
2013-12-07 19:43:55 +04:00
}
2014-03-02 23:43:43 +04:00
var editor *github.Editor
2013-06-02 06:09:17 +04:00
if title == "" && flagPullRequestIssue == "" {
baseTracking := base
headTracking := head
remote := gitRemoteForProject(baseProject)
if remote != nil {
baseTracking = fmt.Sprintf("%s/%s", remote.Name, base)
}
if remote == nil || !baseProject.SameAs(headProject) {
remote = gitRemoteForProject(headProject)
}
if remote != nil {
headTracking = fmt.Sprintf("%s/%s", remote.Name, head)
}
message, err := pullRequestChangesMessage(baseTracking, headTracking, fullBase, fullHead)
2014-03-02 23:43:43 +04:00
utils.Check(err)
editor, err = github.NewEditor("PULLREQ", "pull request", message)
utils.Check(err)
title, body, err = editor.EditTitleAndBody()
2013-11-07 02:18:30 +04:00
utils.Check(err)
2013-07-05 22:10:24 +04:00
}
2013-04-28 21:45:00 +04:00
2013-07-05 22:10:24 +04:00
if title == "" && flagPullRequestIssue == "" {
utils.Check(fmt.Errorf("Aborting due to empty pull request title"))
}
2013-05-28 21:13:55 +04:00
2013-07-06 00:45:22 +04:00
var pullRequestURL string
2013-07-05 22:10:24 +04:00
if args.Noop {
args.Before(fmt.Sprintf("Would request a pull request to %s from %s", fullBase, fullHead), "")
2013-07-06 00:45:22 +04:00
pullRequestURL = "PULL_REQUEST_URL"
2013-07-05 22:10:24 +04:00
} else {
2014-02-24 02:24:37 +04:00
var (
pr *octokit.PullRequest
err error
)
client := github.NewClientWithHost(host)
2013-07-05 22:10:24 +04:00
if title != "" {
2014-02-24 02:24:37 +04:00
pr, err = client.CreatePullRequest(baseProject, base, fullHead, title, body)
} else if flagPullRequestIssue != "" {
pr, err = client.CreatePullRequestForIssue(baseProject, base, fullHead, flagPullRequestIssue)
2013-07-05 22:10:24 +04:00
}
2013-04-28 21:45:00 +04:00
2014-03-02 23:43:43 +04:00
if err == nil && editor != nil {
defer editor.DeleteFile()
}
2014-02-24 02:24:37 +04:00
utils.Check(err)
2014-02-24 02:24:37 +04:00
pullRequestURL = pr.HTMLURL
if flagPullRequestAssignee != "" || flagPullRequestMilestone > 0 ||
flagPullRequestLabels != "" {
params := octokit.IssueParams{
Assignee: flagPullRequestAssignee,
Milestone: flagPullRequestMilestone,
Labels: strings.Split(flagPullRequestLabels, ","),
}
err = client.UpdateIssue(baseProject, pr.Number, params)
utils.Check(err)
}
2013-07-05 22:10:24 +04:00
}
2013-07-06 00:45:22 +04:00
if flagPullRequestBrowse {
launcher, err := utils.BrowserLauncher()
utils.Check(err)
args.Replace(launcher[0], "", launcher[1:]...)
args.AppendParams(pullRequestURL)
} else {
args.Replace("echo", "", pullRequestURL)
}
if flagPullRequestIssue != "" {
args.After("echo", "Warning: Issue to pull request conversion is deprecated and might not work in the future.")
}
2013-07-05 22:10:24 +04:00
}
2013-06-30 20:11:25 +04:00
2014-07-16 23:24:07 +04:00
func pullRequestChangesMessage(base, head, fullBase, fullHead string) (string, error) {
var (
defaultMsg string
commitLogs string
err error
)
commits, _ := git.RefList(base, head)
if len(commits) == 1 {
2014-07-16 23:24:07 +04:00
defaultMsg, err = git.Show(commits[0])
if err != nil {
return "", err
}
} else if len(commits) > 1 {
2014-07-16 23:24:07 +04:00
commitLogs, err = git.Log(base, head)
if err != nil {
return "", err
}
2014-07-16 02:44:48 +04:00
}
2014-07-16 23:24:07 +04:00
cs := git.CommentChar()
2014-07-16 02:44:48 +04:00
2014-07-16 23:24:07 +04:00
return renderPullRequestTpl(defaultMsg, cs, fullBase, fullHead, commitLogs)
2014-07-16 02:44:48 +04:00
}
2013-12-08 12:33:48 +04:00
func parsePullRequestProject(context *github.Project, s string) (p *github.Project, ref string) {
p = context
ref = s
if strings.Contains(s, ":") {
split := strings.SplitN(s, ":", 2)
ref = split[1]
var name string
if !strings.Contains(split[0], "/") {
name = context.Name
}
p = github.NewProject(split[0], name, context.Host)
}
return
}
func parsePullRequestIssueNumber(url string) string {
u, e := github.ParseURL(url)
if e != nil {
return ""
}
r := regexp.MustCompile(`^issues\/(\d+)`)
p := u.ProjectPath()
if r.MatchString(p) {
return r.FindStringSubmatch(p)[1]
}
return ""
}