Skip to content

Commit

Permalink
add support for git fast-forward merge mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilszymanski committed Mar 16, 2016
1 parent 620cdc5 commit 21fc19e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
([JENKINS-33416](https://issues.jenkins-ci.org/browse/JENKINS-33416))
* Improved script execution performance by re-using script engines
([#782](https://github.com/jenkinsci/job-dsl-plugin/pull/782))
* Enhanced support for the [Git Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin)
* 1.44 (March 11 2016)
* Added support for the [Mattermost Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Mattermost+Plugin)
([JENKINS-32764](https://issues.jenkins-ci.org/browse/JENKINS-32764))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class GitExtensionContext extends AbstractContext {
mergeRemote(gitMergeOptionsContext.remote ?: '')
mergeTarget(gitMergeOptionsContext.branch ?: '')
mergeStrategy(gitMergeOptionsContext.strategy)
if (jobManagement.isMinimumPluginVersionInstalled('git', '2.3.5')) {
fastForwardMode(gitMergeOptionsContext.fastForwardMode.name())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package javaposse.jobdsl.dsl.helpers.scm
import javaposse.jobdsl.dsl.AbstractContext
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.dsl.Preconditions
import javaposse.jobdsl.dsl.RequiresPlugin

class GitMergeOptionsContext extends AbstractContext {
private static final Set<String> VALID_STRATEGIES = [
Expand All @@ -12,6 +13,7 @@ class GitMergeOptionsContext extends AbstractContext {
String remote
String branch
String strategy = 'default'
FastForwardMergeMode fastForwardMode = FastForwardMergeMode.FF

GitMergeOptionsContext(JobManagement jobManagement) {
super(jobManagement)
Expand Down Expand Up @@ -45,4 +47,33 @@ class GitMergeOptionsContext extends AbstractContext {

this.strategy = strategy
}

/**
* Sets fast-forward merge mode. Defaults to {@code FastForwardMergeMode.FF}
*
* @since 1.45
*/
@RequiresPlugin(id = 'git', minimumVersion = '2.3.5')
void fastForwardMode(FastForwardMergeMode fastForwardMode) {
this.fastForwardMode = fastForwardMode
}

enum FastForwardMergeMode {

/**
* When the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit.
*/
FF,

/**
* Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge
* can be resolved as a fast-forward.
*/
FF_ONLY,

/**
* Create a merge commit even when the merge resolves as a fast-forward.
*/
NO_FF
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import javaposse.jobdsl.dsl.jobs.FreeStyleJob
import spock.lang.Specification
import spock.lang.Unroll

import static javaposse.jobdsl.dsl.helpers.scm.GitMergeOptionsContext.FastForwardMergeMode.FF_ONLY

class ScmContextSpec extends Specification {
private static final String GIT_REPO_URL = 'git://github.com/Netflix/curator.git'

Expand Down Expand Up @@ -926,6 +928,40 @@ class ScmContextSpec extends Specification {
}

def 'call git scm with minimal mergeOptions'() {
setup:
mockJobManagement.isMinimumPluginVersionInstalled('git', '2.3.5') >> true

when:
context.git {
remote {
url('https://github.com/jenkinsci/job-dsl-plugin.git')
}
extensions {
mergeOptions {
}
}
}

then:
context.scmNodes[0] != null
context.scmNodes[0].extensions.size() == 1
context.scmNodes[0].extensions[0].'hudson.plugins.git.extensions.impl.PreBuildMerge'.size() == 1
with(context.scmNodes[0].extensions[0].'hudson.plugins.git.extensions.impl.PreBuildMerge'[0]) {
options.size() == 1
options[0].children().size() == 4
options[0].mergeRemote.size() == 1
options[0].mergeRemote[0].text() == ''
options[0].mergeTarget.size() == 1
options[0].mergeTarget[0].text() == ''
options[0].mergeStrategy.size() == 1
options[0].mergeStrategy[0].text() == 'default'
options[0].fastForwardMode.size() == 1
options[0].fastForwardMode[0].text() == 'FF'
}
1 * mockJobManagement.requireMinimumPluginVersion('git', '2.2.6')
}

def 'call git scm with minimal mergeOptions, plugin version older than 2.3.5'() {
when:
context.git {
remote {
Expand All @@ -952,9 +988,13 @@ class ScmContextSpec extends Specification {
options[0].mergeStrategy[0].text() == 'default'
}
1 * mockJobManagement.requireMinimumPluginVersion('git', '2.2.6')
1 * mockJobManagement.isMinimumPluginVersionInstalled('git', '2.3.5')
}

def 'call git scm with full mergeOptions'() {
setup:
mockJobManagement.isMinimumPluginVersionInstalled('git', '2.3.5') >> true

when:
context.git {
remote {
Expand All @@ -964,7 +1004,42 @@ class ScmContextSpec extends Specification {
mergeOptions {
remote('other')
branch('acme-plugin')
strategy(mergeStrategy)
strategy('default')
fastForwardMode(FF_ONLY)
}
}
}

then:
context.scmNodes[0] != null
context.scmNodes[0].extensions.size() == 1
context.scmNodes[0].extensions[0].'hudson.plugins.git.extensions.impl.PreBuildMerge'.size() == 1
with(context.scmNodes[0].extensions[0].'hudson.plugins.git.extensions.impl.PreBuildMerge'[0]) {
options.size() == 1
options[0].children().size() == 4
options[0].mergeRemote.size() == 1
options[0].mergeRemote[0].text() == 'other'
options[0].mergeTarget.size() == 1
options[0].mergeTarget[0].text() == 'acme-plugin'
options[0].mergeStrategy.size() == 1
options[0].mergeStrategy[0].text() == 'default'
options[0].fastForwardMode.size() == 1
options[0].fastForwardMode[0].text() == 'FF_ONLY'
}
1 * mockJobManagement.requireMinimumPluginVersion('git', '2.2.6')
}

def 'call git scm with full mergeOptions, plugin version older than 2.3.5'() {
when:
context.git {
remote {
url('https://github.com/jenkinsci/job-dsl-plugin.git')
}
extensions {
mergeOptions {
remote('other')
branch('acme-plugin')
strategy('default')
}
}
}
Expand All @@ -980,6 +1055,25 @@ class ScmContextSpec extends Specification {
options[0].mergeRemote[0].text() == 'other'
options[0].mergeTarget.size() == 1
options[0].mergeTarget[0].text() == 'acme-plugin'
options[0].mergeStrategy.size() == 1
options[0].mergeStrategy[0].text() == 'default'
}
1 * mockJobManagement.requireMinimumPluginVersion('git', '2.2.6')
1 * mockJobManagement.isMinimumPluginVersionInstalled('git', '2.3.5')
}

def 'call git scm with valid merge strategy'() {
when:
context.git {
extensions {
mergeOptions {
strategy(mergeStrategy)
}
}
}

then:
with(context.scmNodes[0].extensions[0].'hudson.plugins.git.extensions.impl.PreBuildMerge'[0]) {
options[0].mergeStrategy.size() == 1
options[0].mergeStrategy[0].text() == mergeStrategy
}
Expand Down

0 comments on commit 21fc19e

Please sign in to comment.