+import com.atlassian.stash.event.pull.PullRequestOpenRequestedEvent
+PullRequestOpenRequestedEvent event = event
+def sourcePattern = ~/\bbugfix\/(.+?)\/.*/
+def targetPattern = ~/release\/(.+)/
+def sourceID = event.pullRequest.fromRef.displayId
+def targetID = event.pullRequest.toRef.displayId
+def sourceMatches = (sourceID=~sourcePattern)
+def targetMatches = (targetID=~targetPattern)
+if (! (sourceMatches.matches() && targetMatches.matches())) {
+ event.cancel("Unsupported merge... not sure what you want to happen here?")
+def sourceBranch = sourceMatches[0][1]
+def targetBranch = targetMatches[0][1]
+if (sourceBranch != targetBranch) {
+ event.cancel("Unsupported merge - can only create a pull request to release/$sourceBranch")