-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrepo-operations.go
632 lines (514 loc) · 23.2 KB
/
repo-operations.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package repository
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/sirupsen/logrus"
"github.com/google/go-github/v43/github"
"github.com/gruntwork-io/git-xargs/common"
"github.com/gruntwork-io/git-xargs/config"
"github.com/gruntwork-io/git-xargs/stats"
"github.com/gruntwork-io/git-xargs/types"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/go-commons/logging"
)
// cloneLocalRepository clones a remote GitHub repo via SSH to a local temporary directory so that the supplied command
// can be run against the repo locally and any git changes handled thereafter. The local directory has
// git-xargs-<repo-name> appended to it to make it easier to find when you are looking for it while debugging
func cloneLocalRepository(config *config.GitXargsConfig, repo *github.Repository) (string, *git.Repository, error) {
logger := logging.GetLogger("git-xargs")
config.CloneJobsLimiter <- struct{}{}
logger.WithFields(logrus.Fields{
"Repo": repo.GetName(),
}).Debug("Attempting to clone repository using GITHUB_OAUTH_TOKEN")
repositoryDir, tmpDirErr := ioutil.TempDir("", fmt.Sprintf("git-xargs-%s", repo.GetName()))
if tmpDirErr != nil {
logger.WithFields(logrus.Fields{
"Error": tmpDirErr,
"Repo": repo.GetName(),
}).Debug("Failed to create temporary directory to hold repo")
return repositoryDir, nil, errors.WithStackTrace(tmpDirErr)
}
gitProgressBuffer := bytes.NewBuffer(nil)
localRepository, err := config.GitClient.PlainClone(repositoryDir, false, &git.CloneOptions{
URL: repo.GetCloneURL(),
Progress: gitProgressBuffer,
Auth: &http.BasicAuth{
Username: repo.GetOwner().GetLogin(),
Password: os.Getenv("GITHUB_OAUTH_TOKEN"),
},
})
logger.WithFields(logrus.Fields{
"Repo": repo.GetName(),
}).Debug(gitProgressBuffer)
<-config.CloneJobsLimiter
if err != nil {
logger.WithFields(logrus.Fields{
"Error": err,
"Repo": repo.GetName(),
}).Debug("Error cloning repository")
// Track failure to clone for our final run report
config.Stats.TrackSingle(stats.RepoFailedToClone, repo)
return repositoryDir, nil, errors.WithStackTrace(err)
}
config.Stats.TrackSingle(stats.RepoSuccessfullyCloned, repo)
return repositoryDir, localRepository, nil
}
// getLocalRepoHeadRef looks up the HEAD reference of the locally cloned git repository, which is required by
// downstream operations such as branching
func getLocalRepoHeadRef(config *config.GitXargsConfig, localRepository *git.Repository, repo *github.Repository) (*plumbing.Reference, error) {
logger := logging.GetLogger("git-xargs")
ref, headErr := localRepository.Head()
if headErr != nil {
logger.WithFields(logrus.Fields{
"Error": headErr,
"Repo": repo.GetName(),
}).Debug("Error getting HEAD ref from local repo")
config.Stats.TrackSingle(stats.GetHeadRefFailed, repo)
return nil, errors.WithStackTrace(headErr)
}
return ref, nil
}
// executeCommand runs the user-supplied command against the given repository
func executeCommand(config *config.GitXargsConfig, repositoryDir string, repo *github.Repository) error {
return executeCommandWithLogger(config, repositoryDir, repo, logging.GetLogger("git-xargs"))
}
// executeCommandWithLogger runs the user-supplied command against the given repository, and sends the log output
// to the given logger
func executeCommandWithLogger(config *config.GitXargsConfig, repositoryDir string, repo *github.Repository, logger *logrus.Logger) error {
if len(config.Args) < 1 {
return errors.WithStackTrace(types.NoCommandSuppliedErr{})
}
cmdArgs := config.Args
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Dir = repositoryDir
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("XARGS_DRY_RUN=%t", config.DryRun))
cmd.Env = append(cmd.Env, fmt.Sprintf("XARGS_REPO_NAME=%s", repo.GetName()))
cmd.Env = append(cmd.Env, fmt.Sprintf("XARGS_REPO_OWNER=%s", repo.GetOwner().GetLogin()))
logger.WithFields(logrus.Fields{
"Repo": repo.GetName(),
"Directory": repositoryDir,
"Command": config.Args,
}).Debug("Executing command against local clone of repo...")
stdoutStdErr, err := cmd.CombinedOutput()
logger.Debugf("Output of command %v for repo %s in directory %s:\n%s", config.Args, repo.GetName(), repositoryDir, string(stdoutStdErr))
if err != nil {
logger.WithFields(logrus.Fields{
"Error": err,
}).Debug("Error getting output of command execution")
// Track the command error against the repo
config.Stats.TrackSingle(stats.CommandErrorOccurredDuringExecution, repo)
return errors.WithStackTrace(err)
}
return nil
}
// getLocalWorkTree looks up the working tree of the locally cloned repository and returns it if possible, or an error
func getLocalWorkTree(repositoryDir string, localRepository *git.Repository, repo *github.Repository) (*git.Worktree, error) {
logger := logging.GetLogger("git-xargs")
worktree, worktreeErr := localRepository.Worktree()
if worktreeErr != nil {
logger.WithFields(logrus.Fields{
"Error": worktreeErr,
"Repo": repo.GetName(),
"Dir": repositoryDir,
}).Debug("Error looking up local repository's worktree")
return nil, errors.WithStackTrace(worktreeErr)
}
return worktree, nil
}
// checkoutLocalBranch creates a local branch specific to this tool in the locally checked out copy of the repo in the /tmp folder
func checkoutLocalBranch(config *config.GitXargsConfig, ref *plumbing.Reference, worktree *git.Worktree, remoteRepository *github.Repository, localRepository *git.Repository) (plumbing.ReferenceName, error) {
logger := logging.GetLogger("git-xargs")
// BranchName is a global variable that is set in cmd/root.go. It is override-able by the operator via the --branch-name or -b flag. It defaults to "git-xargs"
branchName := plumbing.NewBranchReferenceName(config.BranchName)
logger.WithFields(logrus.Fields{
"Branch Name": branchName,
"Repo": remoteRepository.GetName(),
}).Debug("Created branch")
// Create a branch specific to the multi repo script runner
co := &git.CheckoutOptions{
Hash: ref.Hash(),
Branch: branchName,
Create: true,
}
// Attempt to checkout the new tool-specific branch on which the supplied command will be executed
checkoutErr := worktree.Checkout(co)
if checkoutErr != nil {
if config.SkipPullRequests &&
remoteRepository.GetDefaultBranch() == config.BranchName &&
strings.Contains(checkoutErr.Error(), "already exists") {
// User has requested pull requess be skipped, meaning they want their commits pushed on their target branch
// If the target branch is also the repo's default branch and therefore already exists, we don't have an error
} else {
logger.WithFields(logrus.Fields{
"Error": checkoutErr,
"Repo": remoteRepository.GetName(),
}).Debug("Error creating new branch")
// Track the error checking out the branch
config.Stats.TrackSingle(stats.BranchCheckoutFailed, remoteRepository)
return branchName, errors.WithStackTrace(checkoutErr)
}
}
// Pull latest code from remote branch if it exists to avoid fast-forwarding errors
gitProgressBuffer := bytes.NewBuffer(nil)
po := &git.PullOptions{
RemoteName: "origin",
ReferenceName: branchName,
Auth: &http.BasicAuth{
Username: remoteRepository.GetOwner().GetLogin(),
Password: os.Getenv("GITHUB_OAUTH_TOKEN"),
},
Progress: gitProgressBuffer,
}
logger.WithFields(logrus.Fields{
"Repo": remoteRepository.GetName(),
}).Debug(gitProgressBuffer)
pullErr := worktree.Pull(po)
if pullErr != nil {
if pullErr == plumbing.ErrReferenceNotFound {
// The supplied branch just doesn't exist yet on the remote - this is not a fatal error and will
// allow the new branch to be pushed in pushLocalBranch
config.Stats.TrackSingle(stats.BranchRemoteDidntExistYet, remoteRepository)
return branchName, nil
}
if pullErr == git.NoErrAlreadyUpToDate {
// The local branch is already up to date, which is not a fatal error
return branchName, nil
}
// Track the error pulling the latest from the remote branch
config.Stats.TrackSingle(stats.BranchRemotePullFailed, remoteRepository)
return branchName, errors.WithStackTrace(pullErr)
}
return branchName, nil
}
// updateRepo will check for any changes in worktree as a result of script execution, and if any are present,
// add any untracked, deleted or modified files, create a commit using the supplied or default commit message,
// push the code to the remote repo, and open a pull request.
func updateRepo(config *config.GitXargsConfig,
repositoryDir string,
worktree *git.Worktree,
remoteRepository *github.Repository,
localRepository *git.Repository,
branchName string,
) error {
logger := logging.GetLogger("git-xargs")
status, statusErr := worktree.Status()
if statusErr != nil {
logger.WithFields(logrus.Fields{
"Error": statusErr,
"Repo": remoteRepository.GetName(),
"Dir": repositoryDir,
}).Debug("Error looking up worktree status")
// Track the status check failure
config.Stats.TrackSingle(stats.WorktreeStatusCheckFailedCommand, remoteRepository)
return errors.WithStackTrace(statusErr)
}
// If there are no changes, we log it, track it, and return
if status.IsClean() {
logger.WithFields(logrus.Fields{
"Repo": remoteRepository.GetName(),
}).Debug("Local repository status is clean - nothing to stage or commit")
// Track the fact that repo had no file changes post command execution
config.Stats.TrackSingle(stats.WorktreeStatusClean, remoteRepository)
return nil
}
// Commit any untracked files, modified or deleted files that resulted from script execution
commitErr := commitLocalChanges(status, config, repositoryDir, worktree, remoteRepository, localRepository)
if commitErr != nil {
return commitErr
}
// Push the local branch containing all of our changes from executing the supplied command
pushBranchErr := pushLocalBranch(config, remoteRepository, localRepository)
if pushBranchErr != nil {
return pushBranchErr
}
// Create an OpenPrRequest that tracks retries
opr := types.OpenPrRequest{
Repo: remoteRepository,
Branch: branchName,
Retries: 0,
}
return openPullRequestsWithThrottling(config, opr)
}
// commitLocalChanges will check for any changes in worktree as a result of script execution, and if any are present,
// add any untracked, deleted or modified files and create a commit using the supplied or default commit message.
func commitLocalChanges(status git.Status, config *config.GitXargsConfig, repositoryDir string, worktree *git.Worktree, remoteRepository *github.Repository, localRepository *git.Repository) error {
logger := logging.GetLogger("git-xargs")
// If there are changes, we need to stage, add and commit them
logger.WithFields(logrus.Fields{
"Repo": remoteRepository.GetName(),
}).Debug("Local repository worktree no longer clean, will stage and add new files and commit changes")
// Track the fact that worktree changes were made following execution
config.Stats.TrackSingle(stats.WorktreeStatusDirty, remoteRepository)
for filepath := range status {
if status.IsUntracked(filepath) {
logger.WithFields(logrus.Fields{
"Filepath": filepath,
}).Debug("Found untracked file. Adding to stage")
_, addErr := worktree.Add(filepath)
if addErr != nil {
logger.WithFields(logrus.Fields{
"Error": addErr,
"Filepath": filepath,
}).Debug("Error adding file to git stage")
// Track the file staging failure
config.Stats.TrackSingle(stats.WorktreeAddFileFailed, remoteRepository)
return errors.WithStackTrace(addErr)
}
}
}
// With all our untracked files staged, we can now create a commit, passing the All
// option when configuring our commit option so that all modified and deleted files
// will have their changes committed
commitOps := &git.CommitOptions{
All: true,
}
_, commitErr := worktree.Commit(config.CommitMessage, commitOps)
if commitErr != nil {
logger.WithFields(logrus.Fields{
"Error": commitErr,
"Repo": remoteRepository.GetName(),
})
// If we reach this point, we were unable to commit our changes, so we'll
// continue rather than attempt to push an empty branch and open an empty PR
config.Stats.TrackSingle(stats.CommitChangesFailed, remoteRepository)
return errors.WithStackTrace(commitErr)
}
// If --skip-pull-requests was passed, track the repos whose changes were committed directly to the main branch
if config.SkipPullRequests {
config.Stats.TrackSingle(stats.CommitsMadeDirectlyToBranch, remoteRepository)
}
return nil
}
// pushLocalBranch pushes the branch in the local clone of the /tmp/ directory repository to the GitHub remote origin
// so that a pull request can be opened against it via the GitHub API
func pushLocalBranch(config *config.GitXargsConfig, remoteRepository *github.Repository, localRepository *git.Repository) error {
logger := logging.GetLogger("git-xargs")
if config.DryRun {
logger.WithFields(logrus.Fields{
"Repo": remoteRepository.GetName(),
}).Debug("Skipping branch push to remote origin because --dry-run flag is set")
config.Stats.TrackSingle(stats.PushBranchSkipped, remoteRepository)
return nil
}
// Push the changes to the remote repo
po := &git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: remoteRepository.GetOwner().GetLogin(),
Password: os.Getenv("GITHUB_OAUTH_TOKEN"),
},
}
pushErr := localRepository.Push(po)
if pushErr != nil {
logger.WithFields(logrus.Fields{
"Error": pushErr,
"Repo": remoteRepository.GetName(),
}).Debug("Error pushing new branch to remote origin")
// Track the push failure
config.Stats.TrackSingle(stats.PushBranchFailed, remoteRepository)
return errors.WithStackTrace(pushErr)
}
logger.WithFields(logrus.Fields{
"Repo": remoteRepository.GetName(),
}).Debug("Successfully pushed local branch to remote origin")
// If --skip-pull-requests was passed, track the fact that these changes were pushed directly to the main branch
if config.SkipPullRequests {
config.Stats.TrackSingle(stats.DirectCommitsPushedToRemoteBranch, remoteRepository)
}
return nil
}
// Attempt to open a pull request via the GitHub API, of the supplied branch specific to this tool, against the main
// branch for the remote origin
func openPullRequest(config *config.GitXargsConfig, pr types.OpenPrRequest) error {
logger := logging.GetLogger("git-xargs")
// If the current request has already exhausted the configured number of PR retries, short-circuit
if pr.Retries > config.PullRequestRetries {
config.Stats.TrackSingle(stats.PRFailedAfterMaximumRetriesErr, pr.Repo)
return nil
}
if config.DryRun || config.SkipPullRequests {
logger.WithFields(logrus.Fields{
"Repo": pr.Repo.GetName(),
}).Debug("--dry-run and / or --skip-pull-requests is set to true, so skipping opening a pull request!")
return nil
}
logger.Debugf("openPullRequest received job with retries: %d. Config max retries for this run: %d", pr.Retries, config.PullRequestRetries)
repoDefaultBranch := config.BaseBranchName
if repoDefaultBranch == "" {
repoDefaultBranch = pr.Repo.GetDefaultBranch()
}
pullRequestAlreadyExists, err := pullRequestAlreadyExistsForBranch(config, pr.Repo, pr.Branch, repoDefaultBranch)
if err != nil {
logger.WithFields(logrus.Fields{
"Error": err,
"Head": pr.Branch,
"Base": repoDefaultBranch,
}).Debug("Error listing pull requests")
// Track pull request open failure
config.Stats.TrackSingle(stats.PullRequestOpenErr, pr.Repo)
return errors.WithStackTrace(err)
}
if pullRequestAlreadyExists {
logger.WithFields(logrus.Fields{
"Repo": pr.Repo.GetName(),
"Head": pr.Branch,
"Base": repoDefaultBranch,
}).Debug("Pull request already exists for this branch, so skipping opening a pull request!")
// Track that we skipped opening a pull request
config.Stats.TrackSingle(stats.PullRequestAlreadyExists, pr.Repo)
return nil
}
// If the user only supplies a commit message, use that for both the pull request title and descriptions,
// unless they are provided separately
titleToUse := config.PullRequestTitle
descriptionToUse := config.PullRequestDescription
commitMessage := config.CommitMessage
if commitMessage != common.DefaultCommitMessage {
if titleToUse == common.DefaultPullRequestTitle {
titleToUse = commitMessage
}
if descriptionToUse == common.DefaultPullRequestDescription {
descriptionToUse = commitMessage
}
}
// Configure pull request options that the GitHub client accepts when making calls to open new pull requests
newPR := &github.NewPullRequest{
Title: github.String(titleToUse),
Head: github.String(pr.Branch),
Base: github.String(repoDefaultBranch),
Body: github.String(descriptionToUse),
MaintainerCanModify: github.Bool(true),
Draft: github.Bool(config.Draft),
}
// Make a pull request via the Github API
githubPR, resp, err := config.GithubClient.PullRequests.Create(context.Background(), *pr.Repo.GetOwner().Login, pr.Repo.GetName(), newPR)
// The go-github library's CheckResponse method can return two different types of rate limiting error:
// 1. AbuseRateLimitError which may contain a Retry-After header whose value we can use to slow down, or
// 2. RateLimitError which may contain information about when the rate limit will be removed, that we can also use to slow down
// Therefore, we need to use type assertions to test for each type of error response, and accordingly fetch the data it may contain
// about how long git-xargs should wait before its next attempt to open a pull request
githubErr := github.CheckResponse(resp.Response)
if githubErr != nil {
isRateLimited := false
// Create a new open pull request struct that we'll eventually send on the PRChan
opr := types.OpenPrRequest{
Repo: pr.Repo,
Branch: pr.Branch,
Retries: 1,
}
// If this request has been seen before, increment its retries count, taking into account previous iterations
opr.Retries = (pr.Retries + 1)
// If GitHub returned an error of type RateLimitError, we can attempt to compute the next time to try the request again
// by reading its rate limit information
if rateLimitError, ok := githubErr.(*github.RateLimitError); ok {
isRateLimited = true
retryAfter := time.Until(rateLimitError.Rate.Reset.Time)
opr.Delay = retryAfter
logger.Debugf("git-xargs parsed retryAfter %d from GitHub rate limit error's reset time", retryAfter)
}
// If GitHub returned a Retry-After header, use its value, otherwise use the default
if abuseRateLimitError, ok := githubErr.(*github.AbuseRateLimitError); ok {
isRateLimited = true
if abuseRateLimitError.RetryAfter != nil {
if abuseRateLimitError.RetryAfter.Seconds() > 0 {
opr.Delay = *abuseRateLimitError.RetryAfter
}
}
}
if isRateLimited {
// If we couldn't determine a more accurate delay from GitHub API response headers, then fall back to our user-configurable default
if opr.Delay == 0 {
opr.Delay = time.Duration(config.SecondsToSleepWhenRateLimited)
}
logger.Debugf("Retrying PR for repo: %s again later with %d second delay due to secondary rate limiting.", pr.Repo.GetName(), opr.Delay)
// Put another pull request on the channel so this can effectively be retried after a cooldown
// Keep track of the repo's PR initially failing due to rate limiting
config.Stats.TrackSingle(stats.PRFailedDueToRateLimitsErr, pr.Repo)
return openPullRequestsWithThrottling(config, opr)
}
}
// Otherwise, if we reach this point, we can assume we are not rate limited, and hence must do some
// further inspection on the error values returned to us to determine what went wrong
prErrorMessage := "Error opening pull request"
// Github's API will return HTTP status code 422 for several different errors
// Currently, there are two such errors that git-xargs is concerned with:
// 1. User passes the --draft flag, but the targeted repo does not support draft pull requests
// 2. User passes the --base-branch-name flag, specifying a branch that does not exist in the repo
if err != nil {
if resp.StatusCode == 422 {
switch {
case strings.Contains(err.Error(), "Draft pull requests are not supported"):
prErrorMessage = "Error opening pull request: draft PRs not supported for this repo. See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests"
config.Stats.TrackSingle(stats.RepoDoesntSupportDraftPullRequestsErr, pr.Repo)
case strings.Contains(err.Error(), "Field:base Code:invalid"):
prErrorMessage = fmt.Sprintf("Error opening pull request: Base branch name: %s is invalid", config.BaseBranchName)
config.Stats.TrackSingle(stats.BaseBranchTargetInvalidErr, pr.Repo)
default:
config.Stats.TrackSingle(stats.PullRequestOpenErr, pr.Repo)
}
}
// If the Github reponse's status code is not 422, fallback to logging and tracking a generic pull request error
config.Stats.TrackSingle(stats.PullRequestOpenErr, pr.Repo)
logger.WithFields(logrus.Fields{
"Error": err,
"Head": pr.Branch,
"Base": repoDefaultBranch,
"Body": descriptionToUse,
}).Debug(prErrorMessage)
return errors.WithStackTrace(err)
}
// There was no error opening the pull request
logger.WithFields(logrus.Fields{
"Pull Request URL": githubPR.GetHTMLURL(),
}).Debug("Successfully opened pull request")
reviewersRequest := github.ReviewersRequest{
NodeID: githubPR.NodeID,
Reviewers: config.Reviewers,
TeamReviewers: config.TeamReviewers,
}
// If the user supplied reviewer information on the pull request, initiate a separate request to ask for reviews
if config.HasReviewers() {
_, _, reviewRequestErr := config.GithubClient.PullRequests.RequestReviewers(context.Background(), *pr.Repo.GetOwner().Login, pr.Repo.GetName(), githubPR.GetNumber(), reviewersRequest)
if reviewRequestErr != nil {
config.Stats.TrackSingle(stats.RequestReviewersErr, pr.Repo)
}
}
if config.HasAssignees() {
assignees := config.Assignees
_, _, assigneeErr := config.GithubClient.Issues.AddAssignees(context.Background(), *pr.Repo.GetOwner().Login, pr.Repo.GetName(), githubPR.GetNumber(), assignees)
if assigneeErr != nil {
config.Stats.TrackSingle(stats.AddAssigneesErr, pr.Repo)
}
}
if config.Draft {
config.Stats.TrackDraftPullRequest(pr.Repo.GetName(), githubPR.GetHTMLURL())
} else {
// Track successful opening of the pull request, extracting the HTML url to the PR itself for easier review
config.Stats.TrackPullRequest(pr.Repo.GetName(), githubPR.GetHTMLURL())
}
return nil
}
// Returns true if a pull request already exists in the given repo for the given branch
func pullRequestAlreadyExistsForBranch(config *config.GitXargsConfig, repo *github.Repository, branch string, repoDefaultBranch string) (bool, error) {
opts := &github.PullRequestListOptions{
// Filter pulls by head user or head organization and branch name in the format of user:ref-name or organization:ref-name
// https://docs.github.com/en/rest/reference/pulls#list-pull-requests
Head: fmt.Sprintf("%s:%s", *repo.GetOwner().Login, branch),
Base: repoDefaultBranch,
}
prs, _, err := config.GithubClient.PullRequests.List(context.Background(), *repo.GetOwner().Login, repo.GetName(), opts)
if err != nil {
return false, errors.WithStackTrace(err)
}
return len(prs) > 0, nil
}