Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add merge-method option #105

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# changelog

HEAD

- Add option to set merge method (merge, squash or rebase). (Example: `mp merge --merge-method squash`)

---

2021-05-22 - v0.0.32

- Adds - `--diff` flag added to `mp plan` to show the diff of the changes made in each repo
Expand Down
25 changes: 25 additions & 0 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -19,10 +20,13 @@ import (
var mergeFlagThrottle string
var mergeFlagIgnoreReviewApproval bool
var mergeFlagIgnoreBuildStatus bool
var mergeMethod string

// rate limits the # of PR merges. used to prevent load on CI system
var mergeThrottle *time.Ticker

var supportedMergeMethods = []string{"merge", "squash", "rebase"}

var mergeCmd = &cobra.Command{
Use: "merge",
Short: "Merge pushed changes",
Expand All @@ -46,6 +50,10 @@ var mergeCmd = &cobra.Command{
mergeThrottle = time.NewTicker(dur)
}

if !contains(supportedMergeMethods, mergeMethod) {
log.Fatalf("Invalid --merge-method: %s", mergeMethod)
}

err = parallelize(repos, mergeOneRepo)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -92,6 +100,7 @@ func mergeOneRepo(r lib.Repo, ctx context.Context) error {
CommitSHA: pushOutput.CommitSHA,
RequireReviewApproval: !mergeFlagIgnoreReviewApproval,
RequireBuildSuccess: !mergeFlagIgnoreBuildStatus,
MergeMethod: mergeMethod,
}
var output merge.Output
if r.IsGitlab() {
Expand All @@ -113,3 +122,19 @@ func mergeOneRepo(r lib.Repo, ctx context.Context) error {
writeJSON(output, mergeOutputPath)
return nil
}

func init() {
mergeCmd.Flags().StringVarP(&mergeFlagThrottle, "throttle", "t", "30s", "Throttle number of merges, e.g. '30s' means 1 merge per 30 seconds")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreReviewApproval, "ignore-review-approval", false, "Ignore whether or not the review has been approved")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreBuildStatus, "ignore-build-status", false, "Ignore whether or not builds are passing")
mergeCmd.Flags().StringVarP(&mergeMethod, "merge-method", "m", "merge", fmt.Sprintf("Merge method to use. Possible values include: %s", strings.Join(supportedMergeMethods, ", ")))
}

func contains(list []string, item string) bool {
for _, each := range list {
if each == item {
return true
}
}
return false
}
3 changes: 0 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ func init() {
rootCmd.AddCommand(docsCmd)

rootCmd.AddCommand(mergeCmd)
mergeCmd.Flags().StringVarP(&mergeFlagThrottle, "throttle", "t", "30s", "Throttle number of merges, e.g. '30s' means 1 merge per 30 seconds")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreReviewApproval, "ignore-review-approval", false, "Ignore whether or not the review has been approved")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreBuildStatus, "ignore-build-status", false, "Ignore whether or not builds are passing")

rootCmd.AddCommand(planCmd)
planCmd.Flags().StringVarP(&planFlagBranch, "branch", "b", "", "Git branch to commit to")
Expand Down
6 changes: 5 additions & 1 deletion merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Input struct {
RequireReviewApproval bool
// RequireBuildSuccess specifies if the PR must have a successful build before merging
RequireBuildSuccess bool
// Merge method to use. Possible values include: "merge", "squash", and "rebase"
MergeMethod string
}

// Output from Push()
Expand Down Expand Up @@ -95,7 +97,9 @@ func GitHubMerge(ctx context.Context, input Input, repoLimiter *time.Ticker, mer
}

// Merge the PR
options := &github.PullRequestOptions{}
options := &github.PullRequestOptions{
MergeMethod: input.MergeMethod,
}
commitMsg := ""
<-mergeLimiter.C
<-repoLimiter.C
Expand Down