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"
|
2016-08-08 01:43:48 +03:00
|
|
|
"strconv"
|
2013-04-28 21:45:00 +04:00
|
|
|
"strings"
|
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
|
|
|
)
|
|
|
|
|
2013-06-30 00:54:40 +04:00
|
|
|
var cmdPullRequest = &Command{
|
2016-01-24 11:56:18 +03:00
|
|
|
Run: pullRequest,
|
|
|
|
Usage: `
|
2016-08-10 12:44:38 +03:00
|
|
|
pull-request [-fo] [-b <BASE>] [-h <HEAD>] [-a <USERS>] [-M <MILESTONE>] [-l <LABELS>]
|
2016-01-24 11:56:18 +03:00
|
|
|
pull-request -m <MESSAGE>
|
|
|
|
pull-request -F <FILE>
|
|
|
|
pull-request -i <ISSUE>
|
|
|
|
`,
|
|
|
|
Long: `Create a GitHub pull request.
|
|
|
|
|
|
|
|
## Options:
|
|
|
|
-f, --force
|
|
|
|
Skip the check for unpushed commits.
|
|
|
|
|
|
|
|
-m, --message <MESSAGE>
|
|
|
|
Use the first line of <MESSAGE> as pull request title, and the rest as pull
|
|
|
|
request description.
|
|
|
|
|
|
|
|
-F, --file <FILE>
|
|
|
|
Read the pull request title and description from <FILE>.
|
|
|
|
|
|
|
|
-i, --issue <ISSUE>, <ISSUE-URL>
|
|
|
|
(Deprecated) Convert <ISSUE> to a pull request.
|
|
|
|
|
|
|
|
-o, --browse
|
|
|
|
Open the new pull request in a web browser.
|
|
|
|
|
|
|
|
-b, --base <BASE>
|
|
|
|
The base branch in "[OWNER:]BRANCH" format. Defaults to the default branch
|
|
|
|
(usually "master").
|
|
|
|
|
|
|
|
-h, --head <HEAD>
|
2016-04-21 13:15:07 +03:00
|
|
|
The head branch in "[OWNER:]BRANCH" format. Defaults to the current branch.
|
2016-01-24 11:56:18 +03:00
|
|
|
|
2016-08-10 12:44:38 +03:00
|
|
|
-a, --assign <USERS>
|
|
|
|
A comma-separated list of GitHub handles to assign to this pull request.
|
2016-01-24 11:56:18 +03:00
|
|
|
|
|
|
|
-M, --milestone <ID>
|
|
|
|
Add this pull request to a GitHub milestone with id <ID>.
|
|
|
|
|
|
|
|
-l, --labels <LABELS>
|
|
|
|
Add a comma-separated list of labels to this pull request.
|
2016-01-24 18:50:01 +03:00
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub(1), hub-merge(1), hub-checkout(1)
|
2013-04-11 19:33:43 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-07 19:43:55 +04:00
|
|
|
var (
|
|
|
|
flagPullRequestBase,
|
|
|
|
flagPullRequestHead,
|
|
|
|
flagPullRequestIssue,
|
|
|
|
flagPullRequestMessage,
|
2015-04-02 16:53:10 +03:00
|
|
|
flagPullRequestAssignee,
|
2015-08-06 00:29:51 +03:00
|
|
|
flagPullRequestLabels,
|
2013-12-07 19:43:55 +04:00
|
|
|
flagPullRequestFile string
|
2014-04-11 08:16:50 +04:00
|
|
|
flagPullRequestBrowse,
|
2013-12-07 19:43:55 +04:00
|
|
|
flagPullRequestForce bool
|
2015-08-06 00:29:51 +03:00
|
|
|
flagPullRequestMilestone uint64
|
2013-12-07 19:43:55 +04:00
|
|
|
)
|
2013-04-11 19:33:43 +04:00
|
|
|
|
|
|
|
func init() {
|
2013-12-30 03:07:58 +04:00
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestBase, "base", "b", "", "BASE")
|
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestHead, "head", "h", "", "HEAD")
|
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestIssue, "issue", "i", "", "ISSUE")
|
2014-04-11 08:16:50 +04:00
|
|
|
cmdPullRequest.Flag.BoolVarP(&flagPullRequestBrowse, "browse", "o", false, "BROWSE")
|
2013-12-30 03:07:58 +04:00
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestMessage, "message", "m", "", "MESSAGE")
|
|
|
|
cmdPullRequest.Flag.BoolVarP(&flagPullRequestForce, "force", "f", false, "FORCE")
|
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestFile, "file", "F", "", "FILE")
|
2015-04-02 16:53:10 +03:00
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestAssignee, "assign", "a", "", "USER")
|
2015-08-06 00:29:51 +03:00
|
|
|
cmdPullRequest.Flag.Uint64VarP(&flagPullRequestMilestone, "milestone", "M", 0, "MILESTONE")
|
|
|
|
cmdPullRequest.Flag.StringVarP(&flagPullRequestLabels, "labels", "l", "", "LABELS")
|
2013-12-30 02:18:14 +04:00
|
|
|
|
|
|
|
CmdRunner.Use(cmdPullRequest)
|
2013-04-11 19:33:43 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 00:54:40 +04:00
|
|
|
func pullRequest(cmd *Command, args *Args) {
|
2014-04-02 00:40:02 +04:00
|
|
|
localRepo, err := github.LocalRepo()
|
|
|
|
utils.Check(err)
|
2013-12-06 22:50:19 +04:00
|
|
|
|
|
|
|
currentBranch, err := localRepo.CurrentBranch()
|
2013-12-06 22:54:11 +04:00
|
|
|
utils.Check(err)
|
2013-12-06 22:50:19 +04:00
|
|
|
|
|
|
|
baseProject, err := localRepo.MainProject()
|
2013-12-06 22:54:11 +04:00
|
|
|
utils.Check(err)
|
2013-12-06 22:50:19 +04:00
|
|
|
|
2014-09-15 04:46:13 +04:00
|
|
|
host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
|
2014-03-28 03:42:14 +04:00
|
|
|
if err != nil {
|
|
|
|
utils.Check(github.FormatError("creating pull request", err))
|
|
|
|
}
|
2016-08-09 22:40:16 +03:00
|
|
|
client := github.NewClientWithHost(host)
|
2013-12-28 13:14:07 +04:00
|
|
|
|
2014-04-09 20:22:20 +04:00
|
|
|
trackedBranch, headProject, err := localRepo.RemoteBranchAndProject(host.User, false)
|
2013-12-06 22:50:19 +04:00
|
|
|
utils.Check(err)
|
|
|
|
|
2013-12-07 04:51:11 +04:00
|
|
|
var (
|
2013-12-28 13:14:07 +04:00
|
|
|
base, head string
|
|
|
|
force bool
|
2013-12-07 04:51:11 +04:00
|
|
|
)
|
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
|
|
|
|
2013-12-06 22:50:19 +04:00
|
|
|
if flagPullRequestBase != "" {
|
2013-12-08 12:33:48 +04:00
|
|
|
baseProject, base = parsePullRequestProject(baseProject, flagPullRequestBase)
|
2013-12-06 22:50:19 +04:00
|
|
|
}
|
2013-12-07 04:51:11 +04:00
|
|
|
|
2013-12-06 22:50:19 +04:00
|
|
|
if flagPullRequestHead != "" {
|
2013-12-08 12:33:48 +04:00
|
|
|
headProject, head = parsePullRequestProject(headProject, flagPullRequestHead)
|
2013-12-06 22:50:19 +04:00
|
|
|
}
|
2013-12-07 04:51:11 +04:00
|
|
|
|
2013-07-02 22:56:45 +04:00
|
|
|
if args.ParamsSize() == 1 {
|
2013-12-06 22:50:19 +04:00
|
|
|
arg := args.RemoveParam(0)
|
2013-12-08 12:33:48 +04:00
|
|
|
flagPullRequestIssue = parsePullRequestIssueNumber(arg)
|
2013-06-02 05:16:18 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 22:50:19 +04:00
|
|
|
if base == "" {
|
2013-12-28 13:14:07 +04:00
|
|
|
masterBranch := localRepo.MasterBranch()
|
2013-12-06 22:50:19 +04:00
|
|
|
base = masterBranch.ShortName()
|
|
|
|
}
|
|
|
|
|
2014-03-05 20:37:17 +04:00
|
|
|
if head == "" && trackedBranch != nil {
|
2013-12-28 13:14:07 +04:00
|
|
|
if !trackedBranch.IsRemote() {
|
|
|
|
// the current branch tracking another branch
|
|
|
|
// pretend there's no upstream at all
|
|
|
|
trackedBranch = nil
|
|
|
|
} else {
|
2014-07-30 09:25:23 +04:00
|
|
|
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)
|
2013-12-06 22:50:19 +04:00
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
2013-12-06 22:50:19 +04:00
|
|
|
}
|
|
|
|
|
2013-12-31 23:06:58 +04:00
|
|
|
title, body, err := getTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
|
2013-12-20 04:54:14 +04:00
|
|
|
utils.Check(err)
|
2013-12-07 04:51:11 +04:00
|
|
|
|
2016-08-09 22:40:16 +03:00
|
|
|
if headRepo, err := client.Repository(headProject); err == nil {
|
|
|
|
headProject.Owner = headRepo.Owner.Login
|
|
|
|
headProject.Name = headRepo.Name
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:50:19 +04:00
|
|
|
fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base)
|
|
|
|
fullHead := fmt.Sprintf("%s:%s", headProject.Owner, head)
|
|
|
|
|
2013-12-19 22:19:38 +04:00
|
|
|
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 == "" {
|
2014-10-20 04:49:05 +04:00
|
|
|
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 {
|
2013-12-06 22:50:19 +04:00
|
|
|
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 {
|
2016-08-08 01:43:48 +03:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"base": base,
|
|
|
|
"head": fullHead,
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:10:24 +04:00
|
|
|
if title != "" {
|
2016-08-08 01:43:48 +03:00
|
|
|
params["title"] = title
|
|
|
|
if body != "" {
|
|
|
|
params["body"] = body
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
issueNum, _ := strconv.Atoi(flagPullRequestIssue)
|
|
|
|
params["issue"] = issueNum
|
2013-07-05 22:10:24 +04:00
|
|
|
}
|
2016-08-08 01:43:48 +03:00
|
|
|
pr, err := client.CreatePullRequest(baseProject, params)
|
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)
|
2015-04-02 16:53:10 +03:00
|
|
|
|
2016-08-14 20:54:31 +03:00
|
|
|
pullRequestURL = pr.HtmlUrl
|
2015-04-02 16:53:10 +03:00
|
|
|
|
2015-08-06 00:29:51 +03:00
|
|
|
if flagPullRequestAssignee != "" || flagPullRequestMilestone > 0 ||
|
|
|
|
flagPullRequestLabels != "" {
|
|
|
|
|
2016-08-10 12:44:38 +03:00
|
|
|
assignees := []string{}
|
|
|
|
for _, assignee := range strings.Split(flagPullRequestAssignee, ",") {
|
|
|
|
if assignee != "" {
|
|
|
|
assignees = append(assignees, assignee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 05:46:59 +03:00
|
|
|
labels := []string{}
|
|
|
|
for _, label := range strings.Split(flagPullRequestLabels, ",") {
|
|
|
|
if label != "" {
|
|
|
|
labels = append(labels, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:16:05 +03:00
|
|
|
params := map[string]interface{}{}
|
2016-08-10 12:44:38 +03:00
|
|
|
if len(assignees) > 0 {
|
|
|
|
params["assignees"] = assignees
|
2016-08-10 12:16:05 +03:00
|
|
|
}
|
|
|
|
if flagPullRequestMilestone > 0 {
|
|
|
|
params["milestone"] = flagPullRequestMilestone
|
|
|
|
}
|
|
|
|
if len(labels) > 0 {
|
|
|
|
params["labels"] = labels
|
2015-08-06 00:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = client.UpdateIssue(baseProject, pr.Number, params)
|
2015-04-02 16:53:10 +03:00
|
|
|
utils.Check(err)
|
|
|
|
}
|
2013-07-05 22:10:24 +04:00
|
|
|
}
|
2013-07-06 00:45:22 +04:00
|
|
|
|
2014-04-11 08:16:50 +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)
|
|
|
|
}
|
|
|
|
|
2013-12-28 13:14:07 +04:00
|
|
|
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)
|
2013-12-02 20:51:45 +04:00
|
|
|
if len(commits) == 1 {
|
2014-07-16 23:24:07 +04:00
|
|
|
defaultMsg, err = git.Show(commits[0])
|
2013-12-02 20:51:45 +04:00
|
|
|
if err != nil {
|
2013-12-27 08:57:08 +04:00
|
|
|
return "", err
|
2013-12-02 20:51:45 +04:00
|
|
|
}
|
|
|
|
} else if len(commits) > 1 {
|
2014-07-16 23:24:07 +04:00
|
|
|
commitLogs, err = git.Log(base, head)
|
2013-12-02 20:51:45 +04:00
|
|
|
if err != nil {
|
2013-12-27 08:57:08 +04:00
|
|
|
return "", err
|
2013-12-02 20:51:45 +04:00
|
|
|
}
|
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 ""
|
|
|
|
}
|