I'm trying to create a merge hook which allows/forbids merge strategy (ff/squash/commit...) on a per-branch basis.
That is, after clicking the 'Merge' button on a PR, only selective types of merge types shall be available from the drop-down menu.
I have already created a skeleton hook from scratch using Atlassian SDK, but I can't figure out how to tweak it to control the merge menu (or any other field in the merge page).
Hello there
with your implementation of RepositoryMergeCheck comes
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
@Nonnull PullRequestMergeHookRequest mergeHookRequest) {
// get merge strategy by id
Optional<String> mergeStrategy = request.getStrategyId();
...
}
There you can check which strategy is selected to the PR and return
RepositoryHookResult.accepted();
or
RepositoryHookResult.rejected(message, description)
Let me know how you go
Best Ulrich
Izymes // Eliminate boring from work
Hi @Ulrich Kuhnhardt _IzymesCo_ ,
After some time off, I'm back with this issue (Thanks again, your prior answer got me significantly ahead).
The thing is:
The approach you suggested (If I got it right), accepts or rejects the option to proceed from the Pull Request page to the Merge page (by enabling/disabling the 'Merge' button):
I need something slightly different.
I would like to control the possible merge strategies in the drop-down menu once in the merge page.
Is there a way to achieve this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad you're making some progress. You can configure available repository merge strategies - and only those will be enabled for the merge commit dropdown.
If your hook enforces a certain merge strategy and other strategies are available in the dropdown - the PR will be merged by Bitbucket only if the right strategy is selected. Otherwise the hook will block the merge process.
If you want branch-specific merge strategies you may use a tool like Workzone^ to configure your merge process with more granular control.
Let me know how you go,
Best, Ulrich
^ Disclaimer: Workzone is one of our apps for Bitbucket (Cloud, DC and server)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks @Ulrich Kuhnhardt _IzymesCo_ ,
That's helpful, but how do I make my hook to "enforce a certain merge strategy" ?
BR, Tal.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good question! Your hook implementation (see above) either returns
RepositoryHookResult.rejected('reason', 'description')
and Bitbucket will not be able to merge.
Or if the right strategy is selected the hook returns
RepositoryHookResult.accepted()
Hope that helps you further.
Best, Ulrich
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks @Ulrich Kuhnhardt _IzymesCo_
Right, I got this.
The thing is "preUpdate" is executed when clicking the merge button on the PR page.
At this stage - there is no selected strategy at all, because we select it on the merge dialog box which pops up afterwards:
Returning:
RepositoryHookResult.rejected('reason', 'description')
by the 'preUpdate' method cannot rely on the merge strategy simply because there is none selected at this point (it is 'Optional.Empty' at this point).
Maybe I'm using the wrong method? Is there a method which is executed once actually triggering the merge in the merge dialog box?
BR, Tal
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
'preUpdate' is used on 2 occasions:
1. When rendering the merge button (disabling it, if rejected)
2. When actually merging the PR, the merge fails if rejected by hook
You can make merge strategies available in repository or project settings.
If more than one is available the user can't merge, I don't know though if the merge popup modal button will be disabled, i.e. if the change in the selected value would trigger a merge-check in the back-end to validate the method.
Can you just configure merge-strategies that are allowed on project or repo level?
Here's another thought: Have you tried for the hook to _update_ the PR merge strategy to the correct one by modifying the pull request? I would confirm with the Atlassian bitbucket team though if that's possible.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ulrich - thanks again!
I got it to work!
My hook now accepts the first occasion ("When rendering the merge button"), by simply checking if the optional StrategyId is empty (it is empty because I didn't have the chance to set it yet.)
Now, the second occasion ("When actually merging the PR") is triggered when clicking the merge button on the modal popup. Here, a strategy is already selected and I can work with it to run the logic I'm aiming for and reject forbidden workflows.
"Can you just configure merge-strategies that are allowed on project or repo level?"
-No... as the header of the discussion says, I need to apply a per-branch logic.
Here's a screenshot (ignore the logic it implements, it's just a POC):
Thanks so much for your valuable and persistent help,
BR, Tal.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm trying to do the same thing. @Julián Lastiri Did you ever get this implemented and if so, is the code available publicly?
@talg1978 Is your code available publicly by chance?
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.