Our workflow requires us to rebase our branches, before we create a pull request, in order to preserve the linear merge graph. It would be useful to be able to automatically decline a pull request (or as a minimum present some sort of warning, or notification) if a pull request has been accepted to upstream, since your original branch or last rebase. Is there any built in feature or plugin via the marketplace that supports this?
Hi Mark... you want a mergecheck. I'm surprised this isn't already in the plugin, but it doesn't seem to be.
You can add a custom merge check with the following script:
import com.onresolve.scriptrunner.canned.stash.events.BlockOutOfDatePullRequests def pullRequest = mergeRequest.pullRequest def aheadBehindInfo = new BlockOutOfDatePullRequests().isPullRequestUpToDate(pullRequest) if (! aheadBehindInfo.alreadyRebased) { mergeRequest.veto("Can't merge because out of date", "Please rebase then have another go...") }
which looks like:
image2015-9-18 17:3:10.png
It can be difficult though to rebase if you've already merged to your feature branch from the target. But I guess once people have been bitten once they'll rebase rather than merge...
There is more chat about this kind of thing here: http://www.adaptavist.com/w/inside-adaptavists-stash-pull-request-workflow/
I think this is exactly what I'm after! I'll check it out and accept the answer if it works. Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No worries, get in touch with us via https://productsupport.adaptavist.com/servicedesk/customer/portal/3 if you need some hands-on help making it work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
I tried the above but the line :
import com.onresolve.scriptrunner.canned.stash.events.BlockOutOfDatePullRequests
Throws an error "unable to resolve class "com.onresolve.scriptrunner.canned.stash.events.BlockOutOfDatePullRequests"
I'm on Bitbucket server 5.12.1 and Scriptrunner 5.4.18
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm looking for something like this as well. We don't exactly force everyone to re-base / simply merge so I'm rather after a plain information if there are any commits in master that haven't landed in the feature branch - if yes - then merging should be blocked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@JamieA Can you help?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'd to change the request code to
import com.onresolve.scriptrunner.canned.bitbucket.events.BlockOutOfDatePullRequests
def pullRequest = mergeRequest.pullRequest
def aheadBehindInfo = new BlockOutOfDatePullRequests().isPullRequestUpToDate(pullRequest)
if (! aheadBehindInfo.alreadyRebased) {
mergeRequest.veto("Can't merge because needs rebase", "Please rebase then have another go...")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Fails with an error though:
<pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script117.groovy: 4: [Static type checking] - You tried to call a method which is not allowed: com.onresolve.scriptrunner.canned.bitbucket.events.BlockOutOfDatePullRequests#<init>() @ line 4, column 23. def aheadBehindInfo = new BlockOutOfDatePullRequests().isPullRequestUpToDate(pullRequest) ^ Script117.groovy: 6: [Static type checking] - Property is not allowed: com.onresolve.scriptrunner.canned.bitbucket.pullrequests.AheadBehindInfo#alreadyRebased @ line 6, column 7. if (! aheadBehindInfo.alreadyRebased) { ^ 2 errors </pre>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looks like
BlockOutOfDatePullRequests
class has been deprecated. https://docs.atlassian.com/bitbucket-server/javadoc/6.1.1/api/reference/classes.html#letter_B
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Ingram - there is a paid add-on in the Marketplace called ScriptRunner for Stash that supports this. The documentation outlines how to block the creation of out of date Pull Requests. I think that might meet your needs.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, that's nearly exactly what I need, except we would like to be able to add multiple pull requests, but just block the merging if the request is out of date. With the solution you provided, you can only create 1 pull request at once. Is this possible?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm not really sure what you mean... you can always only create one PR per source and target branch combination. I would recommend using this policy, along with my answer. It's better to get people to rebase at the time they try to create the PR, otherwise they may well not even notice it's out of date. But also you need the merge check in my answer because it can go out of date. Ideally you want to notify them when it becomes out of date, as the person merging is probably not the person that can rebase it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
here is what I came up with:
import com.atlassian.bitbucket.commit.CommitsBetweenRequest
import com.atlassian.bitbucket.commit.Commit
import com.atlassian.bitbucket.util.PageRequestImpl
import com.atlassian.bitbucket.util.Page
import com.atlassian.bitbucket.commit.CommitService
import com.atlassian.sal.api.component.ComponentLocator
def pageLimit = com.atlassian.bitbucket.util.PageRequest.MAX_PAGE_LIMIT
def CommitService commitService = ComponentLocator.getComponent(CommitService)
def pullRequest = mergeRequest.pullRequest
def toRefLatestCommit = pullRequest.toRef.getLatestCommit()
CommitsBetweenRequest.Builder builder = new CommitsBetweenRequest.Builder(mergeRequest.pullRequest);
CommitsBetweenRequest cbr = builder.build();
Page<Commit> cs = commitService.getCommitsBetween(cbr, new PageRequestImpl(0, pageLimit ))
def isUpToDate = cs.getValues().any{c -> c.getParents().any { p -> p.getId() == toRefLatestCommit} }
if (!isUpToDate) {
mergeRequest.veto("Can't merge because needs rebase", "Please rebase first.)
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.