-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathpull_request.go
484 lines (398 loc) · 13.7 KB
/
pull_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package commands
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/github/hub/v2/git"
"github.com/github/hub/v2/github"
"github.com/github/hub/v2/utils"
)
var cmdPullRequest = &Command{
Run: pullRequest,
Usage: `
pull-request [-focpd] [-b <BASE>] [-h <HEAD>] [-r <REVIEWERS> ] [-a <ASSIGNEES>] [-M <MILESTONE>] [-l <LABELS>]
pull-request -m <MESSAGE> [--edit]
pull-request -F <FILE> [--edit]
pull-request -i <ISSUE>
`,
Long: `Create a GitHub Pull Request.
## Options:
-f, --force
Skip the check for unpushed commits.
-m, --message <MESSAGE>
The text up to the first blank line in <MESSAGE> is treated as the pull
request title, and the rest is used as pull request description in Markdown
format.
When multiple ''--message'' are passed, their values are concatenated with a
blank line in-between.
When neither ''--message'' nor ''--file'' were supplied, a text editor will open
to author the title and description in.
--no-edit
Use the message from the first commit on the branch as pull request title
and description without opening a text editor.
-F, --file <FILE>
Read the pull request title and description from <FILE>. Pass "-" to read
from standard input instead. See ''--message'' for the formatting rules.
-e, --edit
Open the pull request title and description in a text editor before
submitting. This can be used in combination with ''--message'' or ''--file''.
-i, --issue <ISSUE>
Convert <ISSUE> (referenced by its number) to a pull request.
You can only convert issues authored by you or that which you have admin
rights over. In most workflows it is not necessary to convert issues to
pull requests; you can simply reference the original issue in the body of
the new pull request.
-o, --browse
Open the new pull request in a web browser.
-c, --copy
Put the URL of the new pull request to clipboard instead of printing it.
-p, --push
Push the current branch to <HEAD> before creating the pull request.
-b, --base <BASE>
The base branch in the "[<OWNER>:]<BRANCH>" format. Defaults to the default
branch of the upstream repository (usually "master").
See the "CONVENTIONS" section of hub(1) for more information on how hub
selects the defaults in case of multiple git remotes.
-h, --head <HEAD>
The head branch in "[<OWNER>:]<BRANCH>" format. Defaults to the currently
checked out branch.
-r, --reviewer <USERS>
A comma-separated list (no spaces around the comma) of GitHub handles to
request a review from.
-a, --assign <USERS>
A comma-separated list (no spaces around the comma) of GitHub handles to
assign to this pull request.
-M, --milestone <NAME>
The milestone name to add to this pull request. Passing the milestone number
is deprecated.
-l, --labels <LABELS>
A comma-separated list (no spaces around the comma) of labels to add to
this pull request. Labels will be created if they do not already exist.
-d, --draft
Create the pull request as a draft.
--no-maintainer-edits
When creating a pull request from a fork, this disallows projects
maintainers from being able to push to the head branch of this fork.
Maintainer edits are allowed by default.
## Examples:
$ hub pull-request
[ opens a text editor for writing title and message ]
[ creates a pull request for the current branch ]
$ hub pull-request --base OWNER:master --head MYUSER:my-branch
[ creates a pull request with explicit base and head branches ]
$ hub pull-request --browse -m "My title"
[ creates a pull request with the given title and opens it in a browser ]
$ hub pull-request -F - --edit < path/to/message-template.md
[ further edit the title and message received on standard input ]
## Configuration:
* ''HUB_RETRY_TIMEOUT'':
The maximum time to keep retrying after HTTP 422 on ''--push'' (default: 9).
## See also:
hub(1), hub-merge(1), hub-checkout(1)
`,
}
func init() {
CmdRunner.Use(cmdPullRequest)
}
func pullRequest(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
currentBranch, currentBranchErr := localRepo.CurrentBranch()
baseProject, err := localRepo.MainProject()
utils.Check(err)
host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
if err != nil {
utils.Check(github.FormatError("creating pull request", err))
}
client := github.NewClientWithHost(host)
trackedBranch, headProject, _ := localRepo.RemoteBranchAndProject(host.User, false)
if headProject == nil {
utils.Check(fmt.Errorf("could not determine project for head branch"))
}
var (
base, head string
)
if flagPullRequestBase := args.Flag.Value("--base"); flagPullRequestBase != "" {
baseProject, base = parsePullRequestProject(baseProject, flagPullRequestBase)
}
if flagPullRequestHead := args.Flag.Value("--head"); flagPullRequestHead != "" {
headProject, head = parsePullRequestProject(headProject, flagPullRequestHead)
}
baseRemote, _ := localRepo.RemoteForProject(baseProject)
if base == "" && baseRemote != nil {
base = localRepo.DefaultBranch(baseRemote).ShortName()
}
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() {
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)
}
}
}
force := args.Flag.Bool("--force")
flagPullRequestPush := args.Flag.Bool("--push")
if head == "" {
if trackedBranch == nil {
utils.Check(currentBranchErr)
if !force && !flagPullRequestPush {
branchRemote, branchMerge, err := branchTrackingInformation(currentBranch)
if err != nil || (baseRemote != nil && branchRemote == baseRemote.Name && branchMerge.ShortName() == base) {
if localRepo.RemoteForBranch(currentBranch, host.User) == nil {
err = fmt.Errorf("Aborted: the current branch seems not yet pushed to a remote")
err = fmt.Errorf("%s\n(use `-p` to push the branch or `-f` to skip this check)", err)
utils.Check(err)
}
}
}
head = currentBranch.ShortName()
} else {
head = trackedBranch.ShortName()
}
}
if headRepo, err := client.Repository(headProject); err == nil {
headProject.Owner = headRepo.Owner.Login
headProject.Name = headRepo.Name
}
fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base)
fullHead := fmt.Sprintf("%s:%s", headProject.Owner, head)
if !force && trackedBranch != nil {
remoteCommits, err := git.RefList(trackedBranch.LongName(), "")
if err == nil && 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)
}
}
messageBuilder := &github.MessageBuilder{
Filename: "PULLREQ_EDITMSG",
Title: "pull request",
}
baseTracking := base
headTracking := head
remote := baseRemote
if remote != nil {
baseTracking = fmt.Sprintf("%s/%s", remote.Name, base)
}
if remote == nil || !baseProject.SameAs(headProject) {
remote, _ = localRepo.RemoteForProject(headProject)
}
if remote != nil {
headTracking = fmt.Sprintf("%s/%s", remote.Name, head)
}
if flagPullRequestPush && remote == nil {
utils.Check(fmt.Errorf("Can't find remote for %s", head))
}
messageBuilder.AddCommentedSection(fmt.Sprintf(`Requesting a pull to %s from %s
Write a message for this pull request. The first block
of text is the title and the rest is the description.`, fullBase, fullHead))
flagPullRequestMessage := args.Flag.AllValues("--message")
flagPullRequestEdit := args.Flag.Bool("--edit")
flagPullRequestIssue := args.Flag.Value("--issue")
if !args.Flag.HasReceived("--issue") && args.ParamsSize() > 0 {
flagPullRequestIssue = parsePullRequestIssueNumber(args.GetParam(0))
}
if len(flagPullRequestMessage) > 0 {
messageBuilder.Message = strings.Join(flagPullRequestMessage, "\n\n")
messageBuilder.Edit = flagPullRequestEdit
} else if args.Flag.HasReceived("--file") {
messageBuilder.Message, err = msgFromFile(args.Flag.Value("--file"))
utils.Check(err)
messageBuilder.Edit = flagPullRequestEdit
} else if args.Flag.Bool("--no-edit") {
commits, _ := git.RefList(baseTracking, head)
if len(commits) == 0 {
utils.Check(fmt.Errorf("Aborted: no commits detected between %s and %s", baseTracking, head))
}
message, err := git.Show(commits[len(commits)-1])
utils.Check(err)
messageBuilder.Message = message
} else if flagPullRequestIssue == "" {
messageBuilder.Edit = true
headForMessage := headTracking
if flagPullRequestPush {
headForMessage = head
}
message := ""
commits, _ := git.RefList(baseTracking, headForMessage)
if len(commits) == 1 {
message, err = git.Show(commits[0])
utils.Check(err)
re := regexp.MustCompile(`\n(Co-authored-by|Signed-off-by):[^\n]+`)
message = re.ReplaceAllString(message, "")
} else if len(commits) > 1 {
commitLogs, err := git.Log(baseTracking, headForMessage)
utils.Check(err)
if commitLogs != "" {
messageBuilder.AddCommentedSection("\nChanges:\n\n" + strings.TrimSpace(commitLogs))
}
}
workdir, _ := git.WorkdirName()
if workdir != "" {
template, _ := github.ReadTemplate(github.PullRequestTemplate, workdir)
if template != "" {
message = message + "\n\n\n" + template
}
}
messageBuilder.Message = message
}
title, body, err := messageBuilder.Extract()
utils.Check(err)
if title == "" && flagPullRequestIssue == "" {
utils.Check(fmt.Errorf("Aborting due to empty pull request title"))
}
if flagPullRequestPush {
if args.Noop {
args.Before(fmt.Sprintf("Would push to %s/%s", remote.Name, head), "")
} else {
err = git.Spawn("push", "--set-upstream", remote.Name, fmt.Sprintf("HEAD:%s", head))
utils.Check(err)
}
}
milestoneNumber, err := milestoneValueToNumber(args.Flag.Value("--milestone"), client, baseProject)
utils.Check(err)
var pullRequestURL string
if args.Noop {
args.Before(fmt.Sprintf("Would request a pull request to %s from %s", fullBase, fullHead), "")
pullRequestURL = "PULL_REQUEST_URL"
} else {
params := map[string]interface{}{
"base": base,
"head": fullHead,
"maintainer_can_modify": !args.Flag.Bool("--no-maintainer-edits"),
}
if args.Flag.Bool("--draft") {
params["draft"] = true
}
if title != "" {
params["title"] = title
if body != "" {
params["body"] = body
}
} else {
issueNum, _ := strconv.Atoi(flagPullRequestIssue)
params["issue"] = issueNum
}
startedAt := time.Now()
numRetries := 0
retryDelay := 2
retryAllowance := 0
if flagPullRequestPush {
if allowanceFromEnv := os.Getenv("HUB_RETRY_TIMEOUT"); allowanceFromEnv != "" {
retryAllowance, err = strconv.Atoi(allowanceFromEnv)
utils.Check(err)
} else {
retryAllowance = 9
}
}
var pr *github.PullRequest
for {
pr, err = client.CreatePullRequest(baseProject, params)
if err != nil && strings.Contains(err.Error(), `Invalid value for "head"`) {
if retryAllowance > 0 {
retryAllowance -= retryDelay
time.Sleep(time.Duration(retryDelay) * time.Second)
retryDelay++
numRetries++
} else {
if numRetries > 0 {
duration := time.Since(startedAt)
err = fmt.Errorf("%s\nGiven up after retrying for %.1f seconds.", err, duration.Seconds())
}
break
}
} else {
break
}
}
if err == nil {
defer messageBuilder.Cleanup()
}
utils.Check(err)
pullRequestURL = pr.HTMLURL
params = map[string]interface{}{}
flagPullRequestLabels := commaSeparated(args.Flag.AllValues("--labels"))
if len(flagPullRequestLabels) > 0 {
params["labels"] = flagPullRequestLabels
}
flagPullRequestAssignees := commaSeparated(args.Flag.AllValues("--assign"))
if len(flagPullRequestAssignees) > 0 {
params["assignees"] = flagPullRequestAssignees
}
if milestoneNumber > 0 {
params["milestone"] = milestoneNumber
}
if len(params) > 0 {
err = client.UpdateIssue(baseProject, pr.Number, params)
utils.Check(err)
}
flagPullRequestReviewers := commaSeparated(args.Flag.AllValues("--reviewer"))
if len(flagPullRequestReviewers) > 0 {
userReviewers := []string{}
teamReviewers := []string{}
for _, reviewer := range flagPullRequestReviewers {
if strings.Contains(reviewer, "/") {
teamName := strings.SplitN(reviewer, "/", 2)[1]
if !pr.HasRequestedTeam(teamName) {
teamReviewers = append(teamReviewers, teamName)
}
} else if !pr.HasRequestedReviewer(reviewer) {
userReviewers = append(userReviewers, reviewer)
}
}
if len(userReviewers) > 0 || len(teamReviewers) > 0 {
err = client.RequestReview(baseProject, pr.Number, map[string]interface{}{
"reviewers": userReviewers,
"team_reviewers": teamReviewers,
})
utils.Check(err)
}
}
}
args.NoForward()
printBrowseOrCopy(args, pullRequestURL, args.Flag.Bool("--browse"), args.Flag.Bool("--copy"))
}
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 ""
}
func commaSeparated(l []string) []string {
res := []string{}
for _, i := range l {
if i == "" {
continue
}
res = append(res, strings.Split(i, ",")...)
}
return res
}