Skip to content
Permalink
Browse files
[JENKINS-30192] Added JobDSL support
Added plugin requirement for Job DSL

Removed shorthand integration strategies

Added some tests for the DSL
  • Loading branch information
praqma-thi authored and MadsNielsen committed Oct 12, 2015
1 parent 260a3bc commit ad7569e921fdcb9bc0234b5e66a5ca378e426d93
@@ -463,6 +463,12 @@
<version>0.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>1.37</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -0,0 +1,54 @@
package org.jenkinsci.plugins.pretestedintegration;

import hudson.Extension;
import java.util.Arrays;
import java.util.List;
import static javaposse.jobdsl.dsl.Preconditions.checkArgument;
import javaposse.jobdsl.dsl.RequiresPlugin;
import javaposse.jobdsl.dsl.helpers.wrapper.WrapperContext;
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import javaposse.jobdsl.plugin.DslExtensionMethod;
import org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy;
import org.jenkinsci.plugins.pretestedintegration.scm.git.GitBridge;
import org.jenkinsci.plugins.pretestedintegration.scm.git.SquashCommitStrategy;

/*
```
job{
wrappers{
pretestedIntegration(String integrationStrategy, String branch, String repository)
}
}
```
Valid values for `integrationStrategy` are 'ACCUMULATED' and 'SQUASHED'.
```
job("pi-job"){
wrappers{
pretestedIntegration("SQUASHED","master","origin")
}
}
```
*/

@Extension(optional = true)
public class PretestedIntegrationJobDslExtension extends ContextExtensionPoint {
final List<String> strategies = Arrays.asList("ACCUMULATED", "SQUASHED");

@RequiresPlugin(id = "pretested-integration", minimumVersion = "2.3.0")
@DslExtensionMethod(context = WrapperContext.class)
public Object pretestedIntegration(String strategy, String branch, String repo) {
checkArgument(strategies.contains(strategy), "Strategy must be one of " + strategies);
IntegrationStrategy integrationStrategy = null;
switch (strategy) {
case "ACCUMULATED":
integrationStrategy = new AccumulatedCommitStrategy();
break;
case "SQUASHED":
integrationStrategy = new SquashCommitStrategy();
break;
}
return new PretestedIntegrationBuildWrapper(new GitBridge(integrationStrategy, branch, repo));
}
}
@@ -0,0 +1,46 @@
package org.jenkinsci.plugins.pretestedintegration;

import javaposse.jobdsl.dsl.DslScriptException;
import static org.hamcrest.CoreMatchers.instanceOf;
import org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy;
import org.jenkinsci.plugins.pretestedintegration.scm.git.GitBridge;
import org.jenkinsci.plugins.pretestedintegration.scm.git.SquashCommitStrategy;
import org.junit.Test;

import static org.junit.Assert.*;

public class PretestedIntegrationJobDslExtensionTest {

@Test
public void testDslWithSquash() throws Exception {
Object node = new PretestedIntegrationJobDslExtension().pretestedIntegration("SQUASHED", "branch", "repo");
assertThat(node, instanceOf(PretestedIntegrationBuildWrapper.class));
PretestedIntegrationBuildWrapper wrapper = (PretestedIntegrationBuildWrapper)node;

assertThat(wrapper.scmBridge, instanceOf(GitBridge.class));
GitBridge bridge = (GitBridge)wrapper.scmBridge;

assertThat(bridge.integrationStrategy, instanceOf(SquashCommitStrategy.class));
assertEquals(bridge.getBranch(), "branch");
assertEquals(bridge.getRepoName(), "repo");
}

@Test
public void testDslWithAccumulated() throws Exception {
Object node = new PretestedIntegrationJobDslExtension().pretestedIntegration("ACCUMULATED", "branch", "repo");
assertThat(node, instanceOf(PretestedIntegrationBuildWrapper.class));
PretestedIntegrationBuildWrapper wrapper = (PretestedIntegrationBuildWrapper)node;

assertThat(wrapper.scmBridge, instanceOf(GitBridge.class));
GitBridge bridge = (GitBridge)wrapper.scmBridge;

assertThat(bridge.integrationStrategy, instanceOf(AccumulatedCommitStrategy.class));
assertEquals(bridge.getBranch(), "branch");
assertEquals(bridge.getRepoName(), "repo");
}

@Test(expected = DslScriptException.class)
public void testDslWithInvalidStrategy() throws Exception {
new PretestedIntegrationJobDslExtension().pretestedIntegration("b0rk", "branch", "repo");
}
}

0 comments on commit ad7569e

Please sign in to comment.