Skip to content

Commit

Permalink
feat: add the ability to skip repos from the run command (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosCypher authored Oct 24, 2021
1 parent 7c0e870 commit d4de4dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ To use multi-gitter, a token that is allowed to list repositories and create pul

### Gitea

In Gitea, access tokens can be generated under Settings -> Applications -> Manage Access Tokens
In Gitea, access tokens can be generated under Settings -> Applications -> Manage Access Tokens

## Config file

Expand Down
3 changes: 3 additions & 0 deletions cmd/cmd-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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().BoolP("skip-pr", "", false, "Skip pull request and directly push to the 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.")
cmd.Flags().StringP("author-name", "", "", "Name of the committer. If not set, the global git config setting will be used.")
Expand All @@ -68,6 +69,7 @@ func run(cmd *cobra.Command, args []string) error {
maxReviewers, _ := flag.GetInt("max-reviewers")
concurrent, _ := flag.GetInt("concurrent")
skipPullRequest, _ := flag.GetBool("skip-pr")
skipRepository, _ := flag.GetStringSlice("skip-repo")
interactive, _ := flag.GetBool("interactive")
dryRun, _ := flag.GetBool("dry-run")
forkMode, _ := flag.GetBool("fork")
Expand Down Expand Up @@ -168,6 +170,7 @@ func run(cmd *cobra.Command, args []string) error {
Fork: forkMode,
ForkOwner: forkOwner,
SkipPullRequest: skipPullRequest,
SkipRepository: skipRepository,
CommitAuthor: commitAuthor,
BaseBranch: baseBranchName,
Assignees: assignees,
Expand Down
22 changes: 21 additions & 1 deletion internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type Runner struct {
Assignees []string

Concurrent int
SkipPullRequest bool // If set, the script will run directly on the base-branch without creating any PR
SkipPullRequest bool // If set, the script will run directly on the base-branch without creating any PR
SkipRepository []string // A list of repositories that run will skip

Fork bool // If set, create a fork and make the pull request from it
ForkOwner string // The owner of the new fork. If empty, the fork should happen on the logged in user
Expand Down Expand Up @@ -88,6 +89,8 @@ func (r *Runner) Run(ctx context.Context) error {
return errors.Wrap(err, "could not fetch repositories")
}

repos = filterRepositories(repos, r.SkipRepository)

// Setting up a "counter" that keeps track of successful and failed runs
rc := repocounter.NewCounter()
defer func() {
Expand Down Expand Up @@ -134,6 +137,23 @@ func (r *Runner) Run(ctx context.Context) error {
return nil
}

func filterRepositories(repos []git.Repository, skipRepositoryNames []string) []git.Repository {
skipReposMap := map[string]struct{}{}
for _, skipRepo := range skipRepositoryNames {
skipReposMap[skipRepo] = struct{}{}
}

filteredRepos := make([]git.Repository, 0, len(repos))
for _, r := range repos {
if _, shouldSkip := skipReposMap[r.FullName()]; !shouldSkip {
filteredRepos = append(filteredRepos, r)
} else {
log.Infof("Skipping %s", r.FullName())
}
}
return filteredRepos
}

func runInParallel(fun func(i int), total int, maxConcurrent int) {
concurrentGoroutines := make(chan struct{}, maxConcurrent)
var wg sync.WaitGroup
Expand Down
30 changes: 30 additions & 0 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,36 @@ Repositories with a successful run:
},
},

{
name: "skip-repo",
vcCreate: func(t *testing.T) *vcmock.VersionController {
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "owner", "should-skip", "i like my oranges"),
createRepo(t, "owner", "should-not-skip", "i like my apples"),
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "test",
"--skip-repo", "owner/should-skip",
changerBinaryPath,
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)

assert.Contains(t, runData.logOut, "Skipping owner/should-skip")
assert.Equal(t, `Repositories with a successful run:
owner/should-not-skip #1
`, runData.out)
assert.Equal(t, "i like my oranges", readTestFile(t, vcMock.Repositories[0].Path))
},
},

{
name: "skip-pr",
vcCreate: func(t *testing.T) *vcmock.VersionController {
Expand Down

0 comments on commit d4de4dc

Please sign in to comment.