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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

When creating a pull request, how do you ensure the source branch is branched from destination head?

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?

 

4 answers

@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.

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?

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.

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.

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.

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

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.

Like Erik Baty likes this

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...")
}

 

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>

@JamieA  I'm still looking for the answer too. Could you please suggest how to get this working.

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.)
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

2,575 views 1 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you