Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Validate PullRequest

Oleg Oleg February 6, 2017

Hi. I searched the plugin which will validate PullRequest. If a from branch is not rebased on to brach the plugin will disable "merge" button. But I did not found it sad

So I decided to make it. I started from scratch https://bitbucket.org/atlassian/stash-example-merge-check-config . but I cannot figure out how to check is from brach rebased on to branch. Can any body help?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 7, 2017

Hi Oleg,

I think what you really want to check is that the source branch has not fallen behind the target branch of the pull request. If it has then you should rebase otherwise there would be conflicts or the result of the merge will be unknown.

You can do this using ScriptRunner for Bitbucket Server. After installing you need to go to Admin -> ScriptRunner -> Script Merge Checks -> "Custom merge check" and for the inline script use the following:

import com.atlassian.bitbucket.scm.pull.MergeRequest
import com.onresolve.scriptrunner.canned.bitbucket.pullrequests.PullRequestPolicies

def mergeRequest = mergeRequest as MergeRequest
def pullRequest = mergeRequest.pullRequest

def pullRequestValidator = new PullRequestValidator()

def isUpToDate = pullRequestValidator.isPullRequestUpToDate(pullRequest)

if (! isUpToDate.shouldBeFFMerge) {
    mergeRequest.veto("Pull request is out of date!", "You must rebase this branch on the target branch first.")
}

class PullRequestValidator extends PullRequestPolicies {

    @Override
    String getName() {
        return null
    }

    @Override
    String getDescription() {
        return null
    }

    @Override
    List getParameters(Map params) {
        return null
    }

    @Override
    Map doScript(Map<String, Object> params) {
        return null
    }
}

That will check if the source branch of the pull request is out of date with the target branch and if it is the merge button will be disabled.

The advantage of writing it like above is that you can change the error message the user receives when the merge button is greyed out.

You can also bulk select what repositories/projects to apply the check to as in the screenshot below:

Screen Shot 2017-02-07 at 11.40.09.png

Let me know how you get on with that or if you need further help getting it setup.

Its also useful to point out we have a version of this which blocks the creation of an out of date pull request here. Maybe you would like to add that in to your workflow as well.

Thanks,
Adam 

dmitry June 3, 2019

why does it feel so overkill ?

TAGS
AUG Leaders

Atlassian Community Events