Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #88 from Blackbaud-MikeLueders/demonstrate-copy-ar…
…tifacts-dsl-issue

 [FIXED JENKINS-34204] include node attributes when writing job dsl nodes
  • Loading branch information
oleg-nenashev committed Apr 19, 2016
2 parents 6936b07 + 0b86ed8 commit 30f9c6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
@@ -1,6 +1,7 @@
package hudson.plugins.promoted_builds.integrations.jobdsl;

import java.util.Collection;
import java.util.Map;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
Expand All @@ -15,6 +16,8 @@
import hudson.PluginManager;
import hudson.PluginWrapper;
import jenkins.model.Jenkins;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

/**
* XStream Converter for the PromotionProcess for the Job DSL Plugin
Expand Down Expand Up @@ -89,6 +92,7 @@ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingC

private void convertNode(Node node, HierarchicalStreamWriter writer) {
writer.startNode(node.name().toString());
writeNodeAttributes(node, writer);
if (node.value() instanceof Collection) {
for (Object subNode : (Collection) node.value()) {
convertNode((Node) subNode, writer);
Expand All @@ -99,6 +103,17 @@ private void convertNode(Node node, HierarchicalStreamWriter writer) {
writer.endNode();
}

private void writeNodeAttributes(Node node, HierarchicalStreamWriter writer) {
Map<?,?> attributes = node.attributes();
if (attributes != null) {
for (Map.Entry<?,?> entry : attributes.entrySet()) {
String key = ObjectUtils.toString(entry.getKey());
String value = ObjectUtils.toString(entry.getValue());
writer.addAttribute(key, value);
}
}
}

private String obtainClassOwnership() {
if (this.classOwnership != null) {
return this.classOwnership;
Expand Down
@@ -1,10 +1,14 @@
package hudson.plugins.promoted_builds.integrations.jobdsl;

import com.google.common.io.Files;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.TopLevelItem;
import hudson.model.queue.QueueTaskFuture;

import java.io.File;
import java.nio.charset.Charset;

import javaposse.jobdsl.plugin.RemovedJobAction;
import javaposse.jobdsl.plugin.ExecuteDslScripts;
Expand Down Expand Up @@ -41,4 +45,23 @@ public void testShouldGenerateTheDefindedComplexJob() throws Exception {
// Then
assertBuildStatusSuccess(scheduleBuild2);
}

@Test
public void testShouldGenerateTheCopyArtifactsJob() throws Exception {
// Given
String dsl = FileUtils.readFileToString(new File("src/test/resources/copyartifacts-example-dsl.groovy"));
FreeStyleProject seedJob = createFreeStyleProject();
seedJob.getBuildersList().add(
new ExecuteDslScripts(new ExecuteDslScripts.ScriptLocation(Boolean.TRUE.toString(), null, dsl), false, RemovedJobAction.DELETE));
// When
QueueTaskFuture<FreeStyleBuild> scheduleBuild2 = seedJob.scheduleBuild2(0);
// Then (unstable b/c we aren't including the CopyArtifacts dependency)
assertBuildStatus(Result.UNSTABLE, scheduleBuild2.get());

TopLevelItem item = jenkins.getItem("copy-artifacts-test");
File config = new File(item.getRootDir(), "promotions/Development/config.xml");
String content = Files.toString(config, Charset.forName("UTF-8"));
assert content.contains("<selector class=\"hudson.plugins.copyartifact.SpecificBuildSelector\">");
}

}
22 changes: 22 additions & 0 deletions src/test/resources/copyartifacts-example-dsl.groovy
@@ -0,0 +1,22 @@
freeStyleJob('copy-artifacts-test') {
properties {
promotions {
promotion {
name('Development')
conditions {
manual('tester')
}
actions {
actions {
copyArtifacts('source-job') {
includePatterns('lib/artifact.jar')
buildSelector {
buildNumber('${PROMOTED_NUMBER}')
}
}
}
}
}
}
}
}

0 comments on commit 30f9c6b

Please sign in to comment.