Skip to main content
Example scripts
arrow icon
To homepage
Bitbucket
Data centre icon
Data Center

Pull changes in remote branch

Tags
Created 1 year ago, Updated 3 month(s) ago
App in script
ScriptRunner For Bitbucket
ScriptRunner For Bitbucket
by Adaptavist
Compatibility
compatibility bullet
Bitbucket (6.0 - 7.17)
compatibility bullet
ScriptRunner For Bitbucket (7.10.0)
Language |
groovy
import com.atlassian.bitbucket.io.SingleLineOutputHandler
import com.atlassian.bitbucket.scm.DefaultCommandExitHandler
import com.atlassian.bitbucket.user.ApplicationUser
import com.onresolve.scriptrunner.bitbucket.branch.BranchWithRepository
import com.onresolve.scriptrunner.parameters.annotation.BranchPicker
import com.atlassian.bitbucket.i18n.I18nService
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory
import com.atlassian.bitbucket.scm.git.command.fetch.GitFetchTagMode
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.parameters.annotation.UserPicker

def i18nService = ComponentLocator.getComponent(I18nService)

@BranchPicker(label = 'Source Branch', description = 'Select the branch to fetch from')
BranchWithRepository sourceBranch

@BranchPicker(label = 'Target Branch', description = 'Select a branch where you want to pull the changes into')
BranchWithRepository targetBranch

@UserPicker(label = "User", description = "Select the Bitbucket user that will be used as the author of the pull command")
ApplicationUser user

def gitCommandBuilderFactory = ComponentLocator.getComponent(GitCommandBuilderFactory)

def fetchCommand = gitCommandBuilderFactory.builder(targetBranch.repository)
    .fetch()
    .repository(sourceBranch.repository)
    .refspec(sourceBranch.id)
    .tags(GitFetchTagMode.NO_TAGS)
    .build(new SingleLineOutputHandler())

fetchCommand.setTimeout(60)
fetchCommand.call()

def updateRefCommand = gitCommandBuilderFactory.builder(targetBranch.repository)
    .updateRef()
    .set(targetBranch.id, sourceBranch.latestCommit)
    .author(user)
    .exitHandler(new DefaultCommandExitHandler(i18nService))
    .oldValue(targetBranch.latestCommit)
    .build(new SingleLineOutputHandler())

updateRefCommand.setTimeout(60)
updateRefCommand.call()

Having an issue with this script?
Report it here