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

Squash instead of merge commit #103

Open
simenandre opened this issue Aug 6, 2021 · 4 comments · Fixed by #105
Open

Squash instead of merge commit #103

simenandre opened this issue Aug 6, 2021 · 4 comments · Fixed by #105
Labels

Comments

@simenandre
Copy link
Contributor

Hello 👋

Thank you for this awesome project! I'd love to contribute to this project, and one thing that has bugged me a little is that I have no way to choose which kind of merge I'd like.

I would rather squash and merge than add a merge commit. I'm not sure if it's possible, but I'd love to have a go at it if that is a feature you'd like to see in Microplane!

Thanks again!

@nathanleiby
Copy link
Contributor

@cobraz

Thanks so much for the suggestion! Yes, I think that is possible, and it would be a nice feature if you'd like to add it.


I imagine usage would look something like

$ mp merge --merge-method squash ...

Here's how I'd go about adding it...

The backend logic for merging happens here

microplane/merge/merge.go

Lines 97 to 102 in 9487433

// Merge the PR
options := &github.PullRequestOptions{}
commitMsg := ""
<-mergeLimiter.C
<-repoLimiter.C
result, _, err := client.PullRequests.Merge(ctx, input.Repo.Owner, input.Repo.Name, input.PRNumber, commitMsg, options)

You'd want to set mergeMethod: "squash" within the PullRequestOptions (see `go-github: https://github.com/google/go-github/blob/a0bec87ef51b4d7f14371d355986c7ca8cd0d84c/github/pulls.go#L440-L441)

Beyond that, we'd want to make sure it's configurable from the CLI command. We can set the merge method via a flag, and default to the existing behavior.

microplane/cmd/root.go

Lines 33 to 36 in 9487433

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")

microplane/cmd/merge.go

Lines 18 to 21 in da8630b

// CLI flags
var mergeFlagThrottle string
var mergeFlagIgnoreReviewApproval bool
var mergeFlagIgnoreBuildStatus bool

@simenandre
Copy link
Contributor Author

@nathanleiby thank you for the quick reply. I've opened a pull request, hopefully, that works out? :)

See #105

@nathanleiby
Copy link
Contributor

@cobraz Thanks for adding support for this feature!

I realize that we could also add this feature for Gitlab. Sharing some relevant info below, in case you or someone else would like to add it.

During merge, it seems that Gitlab supports merge (default) or squash. rebase isn't handled as a 3rd merge option, but instead must be done separately from the merge.

Today in Microplane's Gitlab logic...

If a rebase is possible, we will always do it before merging:

// Try to rebase master if Diverged Commits greates that zero
if mr.DivergedCommitsCount > 0 {
_, err := client.MergeRequests.RebaseMergeRequest(pid, input.PRNumber, ctxFunc)
if err != nil {
return Output{Success: false}, fmt.Errorf("Failed to rebase from master")
}
}

We do the merge here (this is where we could pass squash: true as an option):

result, _, err := client.MergeRequests.AcceptMergeRequest(pid, input.PRNumber, &gitlab.AcceptMergeRequestOptions{
ShouldRemoveSourceBranch: &truePointer,
}, ctxFunc)


Suggested Gitlab approach, to give squash support without breaking anything existing:

  • always rebase for Gitlab (keep existing behavior)
  • if mergeOption == squash, then squash: true, otherwise squash: false

@nathanleiby
Copy link
Contributor

Re-opening to track the remaining Gitlab work

@nathanleiby nathanleiby reopened this Aug 12, 2021
@nathanleiby nathanleiby added the GitLab 🦊 Gitlab-specific issue label Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants