|
7 | 7 | "io/ioutil"
|
8 | 8 | "os"
|
9 | 9 | "os/exec"
|
| 10 | + "strings" |
10 | 11 |
|
11 | 12 | "github.com/go-git/go-git/v5"
|
12 | 13 | "github.com/go-git/go-git/v5/plumbing"
|
@@ -390,8 +391,11 @@ func openPullRequest(config *config.GitXargsConfig, repo *github.Repository, bra
|
390 | 391 | }).Debug("--dry-run and / or --skip-pull-requests is set to true, so skipping opening a pull request!")
|
391 | 392 | return nil
|
392 | 393 | }
|
| 394 | + repoDefaultBranch := config.BaseBranchName |
| 395 | + if repoDefaultBranch == "" { |
| 396 | + repoDefaultBranch = repo.GetDefaultBranch() |
| 397 | + } |
393 | 398 |
|
394 |
| - repoDefaultBranch := repo.GetDefaultBranch() |
395 | 399 | pullRequestAlreadyExists, err := pullRequestAlreadyExistsForBranch(config, repo, branch, repoDefaultBranch)
|
396 | 400 |
|
397 | 401 | if err != nil {
|
@@ -449,31 +453,41 @@ func openPullRequest(config *config.GitXargsConfig, repo *github.Repository, bra
|
449 | 453 | pr, resp, err := config.GithubClient.PullRequests.Create(context.Background(), *repo.GetOwner().Login, repo.GetName(), newPR)
|
450 | 454 |
|
451 | 455 | prErrorMessage := "Error opening pull request"
|
452 |
| - prDraftModeNotSupported := false |
453 | 456 |
|
| 457 | + // Github's API will return HTTP status code 422 for several different errors |
| 458 | + // Currently, there are two such errors that git-xargs is concerned with: |
| 459 | + // 1. User passes the --draft flag, but the targeted repo does not support draft pull requests |
| 460 | + // 2. User passes the --base-branch-name flag, specifying a branch that does not exist in the repo |
454 | 461 | if err != nil {
|
455 | 462 | if resp.StatusCode == 422 {
|
456 |
| - // Update the error to be more RepoDoesntSupportDraftPullRequestsErra Draft PR |
457 |
| - 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" |
458 |
| - prDraftModeNotSupported = true |
| 463 | + switch { |
| 464 | + case strings.Contains(err.Error(), "Draft pull requests are not supported"): |
| 465 | + 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" |
| 466 | + config.Stats.TrackSingle(stats.RepoDoesntSupportDraftPullRequestsErr, repo) |
| 467 | + |
| 468 | + case strings.Contains(err.Error(), "Field:base Code:invalid"): |
| 469 | + prErrorMessage = fmt.Sprintf("Error opening pull request: Base branch name: %s is invalid", config.BaseBranchName) |
| 470 | + config.Stats.TrackSingle(stats.BaseBranchTargetInvalidErr, repo) |
| 471 | + |
| 472 | + default: |
| 473 | + config.Stats.TrackSingle(stats.PullRequestOpenErr, repo) |
| 474 | + } |
459 | 475 | }
|
460 | 476 |
|
| 477 | + // If the Github reponse's status code is not 422, fallback to logging and tracking a generic pull request error |
| 478 | + config.Stats.TrackSingle(stats.PullRequestOpenErr, repo) |
| 479 | + |
461 | 480 | logger.WithFields(logrus.Fields{
|
462 | 481 | "Error": err,
|
463 | 482 | "Head": branch,
|
464 | 483 | "Base": repoDefaultBranch,
|
465 | 484 | "Body": descriptionToUse,
|
466 | 485 | }).Debug(prErrorMessage)
|
467 | 486 |
|
468 |
| - // Track pull request open failure |
469 |
| - if prDraftModeNotSupported { |
470 |
| - config.Stats.TrackSingle(stats.RepoDoesntSupportDraftPullRequestsErr, repo) |
471 |
| - } else { |
472 |
| - config.Stats.TrackSingle(stats.PullRequestOpenErr, repo) |
473 |
| - } |
474 | 487 | return errors.WithStackTrace(err)
|
475 | 488 | }
|
476 | 489 |
|
| 490 | + // There was no error opening the pull request |
477 | 491 | logger.WithFields(logrus.Fields{
|
478 | 492 | "Pull Request URL": pr.GetHTMLURL(),
|
479 | 493 | }).Debug("Successfully opened pull request")
|
|
0 commit comments