Repository: lindell/multi-gitter
Pull request #80 (2021-03-02 11:32:26)

feat: added skip-pr flag

coveralls: ## Pull Request Test Coverage Report for [Build 613876567](https://coveralls.io/builds/37575682)

* **19** of **22**   **(86.36%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.3%**) to **47.908%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [internal/multigitter/run.go](https://coveralls.io/builds/37575682/source?filename=internal%2Fmultigitter%2Frun.go#L167) | 16 | 19 | 84.21%
<!-- | **Total:** | **19** | **22** | **86.36%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/37575682/badge)](https://coveralls.io/builds/37575682) |
| :-- | --: |
| Change from base [Build 584760677](https://coveralls.io/builds/37297641): |  0.3% |
| Covered Lines: | 813 |
| Relevant Lines: | 1697 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/cmd/run.go b/cmd/run.go
index 9b67ed9..57b0a99 100755
--- a/cmd/run.go
+++ b/cmd/run.go
@@ -43,6 +43,7 @@ func RunCmd() *cobra.Command {
 	cmd.Flags().IntP("max-reviewers", "M", 0, "If this value is set, reviewers will be randomized")
 	cmd.Flags().IntP("concurrent", "C", 1, "The maximum number of concurrent runs")
 	cmd.Flags().IntP("fetch-depth", "f", 1, "Limit fetching to the specified number of commits. Set to 0 for no limit")
+	cmd.Flags().BoolP("skip-pr", "", false, "Limit fetching to the specified number of commits. Set to 0 for no limit")
 	cmd.Flags().BoolP("dry-run", "d", false, "Run without pushing changes or creating pull requests")
 	cmd.Flags().StringP("author-name", "", "", "Name of the committer. If not set, the global git config setting will be used.")
 	cmd.Flags().StringP("author-email", "", "", "Email of the committer. If not set, the global git config setting will be used.")
@@ -65,6 +66,7 @@ func run(cmd *cobra.Command, args []string) error {
 	maxReviewers, _ := flag.GetInt("max-reviewers")
 	fetchDepth, _ := flag.GetInt("fetch-depth")
 	concurrent, _ := flag.GetInt("concurrent")
+	skipPullRequest, _ := flag.GetBool("skip-pr")
 	dryRun, _ := flag.GetBool("dry-run")
 	authorName, _ := flag.GetString("author-name")
 	authorEmail, _ := flag.GetString("author-email")
@@ -165,6 +167,7 @@ func run(cmd *cobra.Command, args []string) error {
 		Reviewers:        reviewers,
 		MaxReviewers:     maxReviewers,
 		DryRun:           dryRun,
+		SkipPullRequest:  skipPullRequest,
 		CommitAuthor:     commitAuthor,
 		BaseBranch:       baseBranchName,
 
diff --git a/internal/multigitter/run.go b/internal/multigitter/run.go
index 1bb4aad..ad82d7e 100755
--- a/internal/multigitter/run.go
+++ b/internal/multigitter/run.go
@@ -48,8 +48,9 @@ type Runner struct {
 	CommitAuthor     *domain.CommitAuthor
 	BaseBranch       string // The base branch of the PR, use default branch if not set
 
-	FetchDepth int // Limit fetching to the specified number of commits. Set to 0 for no limit
-	Concurrent int
+	FetchDepth      int // Limit fetching to the specified number of commits. Set to 0 for no limit
+	Concurrent      int
+	SkipPullRequest bool // If set, the script will run directly on the base-branch without creating any PR
 }
 
 var errAborted = errors.New("run was never started because of aborted execution")
@@ -94,7 +95,11 @@ func (r Runner) Run(ctx context.Context) error {
 			return
 		}
 
-		rc.AddSuccessPullRequest(pr)
+		if pr != nil {
+			rc.AddSuccessPullRequest(pr)
+		} else {
+			rc.AddSuccessRepositories(repos[i])
+		}
 	}, len(repos), r.Concurrent)
 
 	return nil
@@ -155,16 +160,19 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) (doma
 		return nil, err
 	}
 
-	featureBranchExist, err := sourceController.BranchExist(r.FeatureBranch)
-	if err != nil {
-		return nil, errors.Wrap(err, "could not verify if branch already exist")
-	} else if featureBranchExist {
-		return nil, domain.BranchExistError
-	}
+	// Change the branch to the feature branch
+	if !r.SkipPullRequest {
+		featureBranchExist, err := sourceController.BranchExist(r.FeatureBranch)
+		if err != nil {
+			return nil, errors.Wrap(err, "could not verify if branch already exist")
+		} else if featureBranchExist {
+			return nil, domain.BranchExistError
+		}
 
-	err = sourceController.ChangeBranch(r.FeatureBranch)
-	if err != nil {
-		return nil, err
+		err = sourceController.ChangeBranch(r.FeatureBranch)
+		if err != nil {
+			return nil, err
+		}
 	}
 
 	// Run the command that might or might not change the content of the repo
@@ -208,6 +216,10 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) (doma
 		return nil, errors.Wrap(err, "could not push changes")
 	}
 
+	if r.SkipPullRequest {
+		return nil, nil
+	}
+
 	log.Info("Change done, creating pull request")
 	pr, err := r.VersionController.CreatePullRequest(ctx, repo, domain.NewPullRequest{
 		Title:     r.PullRequestTitle,


Test patch:
diff --git a/tests/table_test.go b/tests/table_test.go
index 74d86b7..fc7982a 100644
--- a/tests/table_test.go
+++ b/tests/table_test.go
@@ -279,6 +279,50 @@ Repositories with a successful run:
 `, runData.out)
 			},
 		},
+
+		{
+			name: "skip-pr",
+			vcCreate: func(t *testing.T) *vcmock.VersionController {
+				repo := createRepo(t, "should-change", "i like apples")
+
+				// Change branch so that it's not the one we are expected to push to.
+				// If this can be avoided, it would be good.
+				changeBranch(t, repo.Path, "test", true)
+
+				return &vcmock.VersionController{
+					Repositories: []vcmock.Repository{
+						repo,
+					},
+				}
+			},
+			args: []string{
+				"run",
+				"--author-name", "Test Author",
+				"--author-email", "test@example.com",
+				"-B", "custom-branch-name",
+				"-m", "custom message",
+				"--skip-pr",
+				path.Join(workingDir, "scripts/changer/main"),
+			},
+			verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
+				fmt.Fprintln(os.Stderr, vcMock.Repositories[0].Path)
+
+				require.Len(t, vcMock.PullRequests, 0)
+
+				assert.Contains(t, runData.logOut, "Running on 1 repositories")
+				assert.Contains(t, runData.logOut, "Cloning and running script")
+
+				assert.Equal(t, `Repositories with a successful run:
+  should-change
+`, runData.out)
+
+				changeBranch(t, vcMock.Repositories[0].Path, "master", false)
+
+				assert.False(t, branchExist(t, vcMock.Repositories[0].Path, "custom-branch-name"))
+				assert.False(t, branchExist(t, vcMock.Repositories[0].Path, "multi-gitter-branch"))
+				assert.Equal(t, "i like bananas", readTestFile(t, vcMock.Repositories[0].Path))
+			},
+		},
 	}
 
 	for _, test := range tests {