Skip to content

Commit

Permalink
feat: --push-only flag to allow developers to only push without creat…
Browse files Browse the repository at this point in the history
…ing a PR (#466)
  • Loading branch information
daemocles committed Mar 14, 2024
1 parent 28f6a82 commit 76dcabd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cmd/cmd-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func RunCmd() *cobra.Command {
cmd.Flags().IntP("max-team-reviewers", "", 0, "If this value is set, team reviewers will be randomized")
cmd.Flags().IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
cmd.Flags().BoolP("skip-pr", "", false, "Skip pull request and directly push to the branch.")
cmd.Flags().BoolP("push-only", "", false, "Skip pull request and only push the feature branch.")
cmd.Flags().StringSliceP("skip-repo", "s", nil, "Skip changes on specified repositories, the name is including the owner of repository in the format \"ownerName/repoName\".")
cmd.Flags().BoolP("interactive", "i", false, "Take manual decision before committing any change. Requires git to be installed.")
cmd.Flags().BoolP("dry-run", "d", false, "Run without pushing changes or creating pull requests.")
Expand Down Expand Up @@ -89,6 +90,7 @@ func run(cmd *cobra.Command, _ []string) error {
maxTeamReviewers, _ := flag.GetInt("max-team-reviewers")
concurrent, _ := flag.GetInt("concurrent")
skipPullRequest, _ := flag.GetBool("skip-pr")
pushOnly, _ := flag.GetBool("push-only")
skipRepository, _ := flag.GetStringSlice("skip-repo")
interactive, _ := flag.GetBool("interactive")
dryRun, _ := flag.GetBool("dry-run")
Expand Down Expand Up @@ -129,6 +131,14 @@ func run(cmd *cobra.Command, _ []string) error {
}
}

if pushOnly && forkMode {
return errors.New("--push-only and --fork can't be used at the same time")
}

if skipPullRequest && pushOnly {
return errors.New("--push-only and --skip-pr can't be used at the same time")
}

if skipPullRequest && forkMode {
return errors.New("--fork and --skip-pr can't be used at the same time")
}
Expand Down Expand Up @@ -228,6 +238,7 @@ func run(cmd *cobra.Command, _ []string) error {
Fork: forkMode,
ForkOwner: forkOwner,
SkipPullRequest: skipPullRequest,
PushOnly: pushOnly,
SkipRepository: skipRepository,
CommitAuthor: commitAuthor,
BaseBranch: baseBranchName,
Expand Down
9 changes: 8 additions & 1 deletion internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Runner struct {

Concurrent int
SkipPullRequest bool // If set, the script will run directly on the base-branch without creating any PR
PushOnly bool // If set, the script will only publish the feature branch without creating a PR
SkipRepository []string // A list of repositories that run will skip
RegExIncludeRepository *regexp.Regexp
RegExExcludeRepository *regexp.Regexp
Expand Down Expand Up @@ -319,7 +320,7 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu

// Determine if a branch already exists and (depending on the conflict strategy) skip making changes
featureBranchExist := false
if !r.SkipPullRequest {
if !r.SkipPullRequest && !r.PushOnly {
featureBranchExist, err = sourceController.BranchExist(remoteName, r.FeatureBranch)
if err != nil {
return nil, errors.Wrap(err, "could not verify if branch already exists")
Expand All @@ -340,6 +341,12 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu
return nil, errors.Wrap(err, "could not push changes")
}

if r.PushOnly {
return dryRunPullRequest{
Repository: repo,
}, nil
}

return r.ensurePullRequestExists(ctx, log, repo, prRepo, baseBranch, featureBranchExist)
}

Expand Down
70 changes: 70 additions & 0 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,76 @@ Repositories with a successful run:
assert.Equal(t, "Both the feature branch and base branch was named master, if you intended to push directly into the base branch, please use the `skip-pr` option:\n owner/should-not-change\n", runData.out)
},
},

{
name: "fork conflicts with pushOnly",
vcCreate: func(t *testing.T) *vcmock.VersionController {
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "owner", "example-repository", "i like apples"),
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "custom message",
"--fork", "--push-only",
changerBinaryPath,
},
expectErr: true,
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
assert.Contains(t, runData.cmdOut, "Error: --push-only and --fork can't be used at the same time")
},
},
{
name: "skipPr conflicts with pushOnly",
vcCreate: func(t *testing.T) *vcmock.VersionController {
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "owner", "example-repository", "i like apples"),
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "custom message",
"--skip-pr", "--push-only",
changerBinaryPath,
},
expectErr: true,
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
assert.Contains(t, runData.cmdOut, "Error: --push-only and --skip-pr can't be used at the same time")
},
},
{
name: "pushOnly success",
vcCreate: func(t *testing.T) *vcmock.VersionController {
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "owner", "example-repository", "i like apples"),
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "custom message",
"--push-only",
changerBinaryPath,
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
assert.Equal(t, runData.cmdOut, "")
assert.Equal(t, runData.out, "Repositories with a successful run:\n owner/example-repository #0\n")
},
},
}

for _, gitBackend := range gitBackends {
Expand Down

0 comments on commit 76dcabd

Please sign in to comment.