I’m using scriptrunner to write a Conditional Merge Check script on our BitBucket server. My goal is to block a merge request if the merge commit message does not match a regular expression. I’m struggling with a few different aspects of this task. (Aside: I think the biggest challenge I’m facing is I can find a wealth of special-case examples of scriptrunner scripts, but I haven’t found a good, general introduction to writing scriptrunner scripts. Maybe that would help?)
For context, the repo is configured with the Merge Strategy "Merge commit
--no-ff". So BitBucket tells git to always create a new merge commit and update the target branch to it, even if the source branch is already up to date with the target branch. With this strategy enabled, developers can edit the merge commit message when merging a pull request. It’s this merge commit message that I want to check with the script.
With some experimentation, I learned the script executes in two distinct contexts: (1) when bitbucket is trying to determine if it should enable/disable the Merge button on the Pull Request page, and (2) when someone clicks the Merge button on the Merge pull request dialog.
I started with a simple script that would reject the merge if the message was a specific string:
mergeRequest.message == "WRONG"
This worked in the sense that it does what it looks like it should: it rejects merge requests when I edited the merge commit message to be the exact string “WRONG”. But the requirement is to check for a regular expression.
Next I tried to reject the merge if the message did not match a regular expression. I read that the strings are Java String objects, so I tried this:
I found that this script caused the Merge button on the Pull Request page to be disabled. (Aside: that wasn’t what I expected. I really only want to check the message after the user edits it.) So I tried to only check the message when it was assigned, i.e. if its length was positive:
mergeRequest.message.length() > 0 && ! mergeRequest.message.matches("MY-REGEX")
But that also disabled the Merge button on the Pull Request page. At this point I tested the Java-String idea experimentally using a simplistic expression. First this:
mergeRequest.message.length() > 0
mergeRequest.message.length() <= 0
Both of the above scripts produced the same result. What is going on? Is mergeRequest.message a String?
More importantly, how do I check the merge commit message matches a regular expression?