Skip to content

Commit 3dc1d1a

Browse files
authored
Support for overriding base branch names (#64)
1 parent 720616a commit 3dc1d1a

File tree

7 files changed

+38
-12
lines changed

7 files changed

+38
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ Passing the `--branch-name` (`-b`) flag is required when running `git-xargs`. If
265265
266266
## Default repository branch
267267
268-
Any pull requests opened will be opened against the repository's default branch (whether that's `main`, or `master` or something else).
268+
Any pull requests opened will be opened against the repository's default branch (whether that's `main`, or `master` or something else). You can supply an additional `--base-branch-name` flag to change the target for your pull requests. Be aware that this will override the base branch name for **ALL** targeted repositories.
269269
270270
## Git file staging behavior
271271

cmd/git-xargs.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func parseGitXargsConfig(c *cli.Context) (*config.GitXargsConfig, error) {
2525
config.SkipPullRequests = c.Bool("skip-pull-requests")
2626
config.SkipArchivedRepos = c.Bool("skip-archived-repos")
2727
config.BranchName = c.String("branch-name")
28+
config.BaseBranchName = c.String("base-branch-name")
2829
config.CommitMessage = c.String("commit-message")
2930
config.PullRequestTitle = c.String("pull-request-title")
3031
config.PullRequestDescription = c.String("pull-request-description")

common/common.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
ReposFileFlagName = "repos"
1313
CommitMessageFlagName = "commit-message"
1414
BranchFlagName = "branch-name"
15+
BaseBranchFlagName = "base-branch-name"
1516
PullRequestTitleFlagName = "pull-request-title"
1617
PullRequestDescriptionFlagName = "pull-request-description"
1718
MaxConcurrentReposFlagName = "max-concurrent-repos"
@@ -54,6 +55,10 @@ var (
5455
Name: BranchFlagName,
5556
Usage: "The name of the branch on which changes will be made",
5657
}
58+
GenericBaseBranchFlag = cli.StringFlag{
59+
Name: BaseBranchFlagName,
60+
Usage: "The base branch that changes should be merged into",
61+
}
5762
GenericCommitMessageFlag = cli.StringFlag{
5863
Name: CommitMessageFlagName,
5964
Usage: "The commit message to use when creating commits from changes introduced by your command or script",

config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type GitXargsConfig struct {
1818
SkipArchivedRepos bool
1919
MaxConcurrentRepos int
2020
BranchName string
21+
BaseBranchName string
2122
CommitMessage string
2223
PullRequestTitle string
2324
PullRequestDescription string
@@ -40,6 +41,7 @@ func NewGitXargsConfig() *GitXargsConfig {
4041
SkipArchivedRepos: false,
4142
MaxConcurrentRepos: 0,
4243
BranchName: "",
44+
BaseBranchName: "",
4345
CommitMessage: common.DefaultCommitMessage,
4446
PullRequestTitle: common.DefaultPullRequestTitle,
4547
PullRequestDescription: common.DefaultPullRequestDescription,

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func setupApp() *cli.App {
6969
common.GenericRepoFlag,
7070
common.GenericRepoFileFlag,
7171
common.GenericBranchFlag,
72+
common.GenericBaseBranchFlag,
7273
common.GenericCommitMessageFlag,
7374
common.GenericPullRequestTitleFlag,
7475
common.GenericPullRequestDescriptionFlag,

repository/repo-operations.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"os"
99
"os/exec"
10+
"strings"
1011

1112
"github.com/go-git/go-git/v5"
1213
"github.com/go-git/go-git/v5/plumbing"
@@ -390,8 +391,11 @@ func openPullRequest(config *config.GitXargsConfig, repo *github.Repository, bra
390391
}).Debug("--dry-run and / or --skip-pull-requests is set to true, so skipping opening a pull request!")
391392
return nil
392393
}
394+
repoDefaultBranch := config.BaseBranchName
395+
if repoDefaultBranch == "" {
396+
repoDefaultBranch = repo.GetDefaultBranch()
397+
}
393398

394-
repoDefaultBranch := repo.GetDefaultBranch()
395399
pullRequestAlreadyExists, err := pullRequestAlreadyExistsForBranch(config, repo, branch, repoDefaultBranch)
396400

397401
if err != nil {
@@ -449,31 +453,41 @@ func openPullRequest(config *config.GitXargsConfig, repo *github.Repository, bra
449453
pr, resp, err := config.GithubClient.PullRequests.Create(context.Background(), *repo.GetOwner().Login, repo.GetName(), newPR)
450454

451455
prErrorMessage := "Error opening pull request"
452-
prDraftModeNotSupported := false
453456

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
454461
if err != nil {
455462
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+
}
459475
}
460476

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+
461480
logger.WithFields(logrus.Fields{
462481
"Error": err,
463482
"Head": branch,
464483
"Base": repoDefaultBranch,
465484
"Body": descriptionToUse,
466485
}).Debug(prErrorMessage)
467486

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-
}
474487
return errors.WithStackTrace(err)
475488
}
476489

490+
// There was no error opening the pull request
477491
logger.WithFields(logrus.Fields{
478492
"Pull Request URL": pr.GetHTMLURL(),
479493
}).Debug("Successfully opened pull request")

stats/stats.go

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const (
7070
RepoFlagSuppliedRepoMalformed types.Event = "repo-flag-supplied-repo-malformed"
7171
// RepoDoesntSupportDraftPullRequestsErr denotes a repo that is incompatible with the submitted pull request configuration
7272
RepoDoesntSupportDraftPullRequestsErr types.Event = "repo-not-compatible-with-pull-config"
73+
// BaseBranchTargetInvalidErr denotes a repo that does not have the base branch specified by the user
74+
BaseBranchTargetInvalidErr types.Event = "base-branch-target-invalid"
7375
)
7476

7577
var allEvents = []types.AnnotatedEvent{
@@ -100,6 +102,7 @@ var allEvents = []types.AnnotatedEvent{
100102
{Event: BranchRemoteDidntExistYet, Description: "Repos whose specified branches did not exist on the remote, and so were first created locally"},
101103
{Event: RepoFlagSuppliedRepoMalformed, Description: "Repos passed via the --repo flag that were malformed (missing their Github org prefix?) and therefore unprocessable"},
102104
{Event: RepoDoesntSupportDraftPullRequestsErr, Description: "Repos that do not support Draft PRs (--draft flag was passed)"},
105+
{Event: BaseBranchTargetInvalidErr, Description: "Repos that did not have the branch specified by --base-branch-name"},
103106
}
104107

105108
// RunStats will be a stats-tracker class that keeps score of which repos were touched, which were considered for update, which had branches made, PRs made, which were missing workflows or contexts, or had out of date workflows syntax values, etc

0 commit comments

Comments
 (0)