From cd9e806fa6098a8a541604329ca23eae09e40582 Mon Sep 17 00:00:00 2001 From: Denis Tikhomirov Date: Tue, 29 Jun 2021 19:08:06 +0300 Subject: [PATCH 1/3] Fix dynamic DSL plugins handling in job DSL This commit fixes working with dynamic DSL plugins in job DSL integration code, namely in promotion actions processing. --- .../JobDslPromotionProcessConverter.java | 18 +++++++----------- .../PromotionsDslContextExtensionTest.java | 16 ++++++++++++++++ .../resources/dynamic-dsl-example-dsl.groovy | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 src/test/resources/dynamic-dsl-example-dsl.groovy diff --git a/src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverter.java b/src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverter.java index 8155f425..080b4de1 100755 --- a/src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverter.java +++ b/src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverter.java @@ -87,24 +87,20 @@ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingC private void writeNodes(HierarchicalStreamWriter writer, List nodes) { for (Node node : nodes) { - writer.startNode(node.name().toString()); - if (node.value() instanceof Collection) { - for (Object subNode : (Collection) node.value()) { - convertNode((Node) subNode, writer); - } - } else { - writer.setValue(node.value().toString()); - } - writer.endNode(); + writeNode(node, writer); } } - private void convertNode(Node node, HierarchicalStreamWriter writer) { + private void writeNode(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); + if (subNode instanceof Node) { + writeNode((Node) subNode, writer); + } else { + writer.setValue(subNode.toString()); + } } } else { writer.setValue(node.value().toString()); diff --git a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java index 4d30d4b6..c927dd07 100644 --- a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java +++ b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java @@ -98,4 +98,20 @@ public void testShouldGenerateTheJobWithBuildWrappers() throws Exception { .matcher(content).find()); } + @Test + public void testShouldGenerateTheDynamicDslJob() throws Exception { + // Given + String dsl = FileUtils.readFileToString(new File("src/test/resources/dynamic-dsl-example-dsl.groovy")); + FreeStyleProject seedJob = j.createFreeStyleProject(); + seedJob.getBuildersList().add(createScript(dsl)); + // When + QueueTaskFuture scheduleBuild2 = seedJob.scheduleBuild2(0); + j.assertBuildStatusSuccess(scheduleBuild2.get()); + + TopLevelItem item = j.jenkins.getItem("dynamic-dsl-test"); + File config = new File(item.getRootDir(), "promotions/Development/config.xml"); + String content = Files.toString(config, Charset.forName("UTF-8")); + assert content.contains(""); + } + } diff --git a/src/test/resources/dynamic-dsl-example-dsl.groovy b/src/test/resources/dynamic-dsl-example-dsl.groovy new file mode 100644 index 00000000..d14f70ca --- /dev/null +++ b/src/test/resources/dynamic-dsl-example-dsl.groovy @@ -0,0 +1,17 @@ +freeStyleJob('dynamic-dsl-test') { + properties { + promotions { + promotion { + name('Development') + conditions { + manual('tester') + } + actions { + jobDsl { + scriptText('println test') + } + } + } + } + } +} From 0684ad74f3882bb3d21431d7ed92d2e016d1194d Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 5 May 2024 20:55:06 -0600 Subject: [PATCH 2/3] Fix compilation error --- .../integrations/jobdsl/PromotionsDslContextExtensionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java index aa8dc3e2..b23e038a 100644 --- a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java +++ b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java @@ -111,7 +111,7 @@ public void testShouldGenerateTheDynamicDslJob() throws Exception { TopLevelItem item = j.jenkins.getItem("dynamic-dsl-test"); File config = new File(item.getRootDir(), "promotions/Development/config.xml"); - String content = Files.toString(config, Charset.forName("UTF-8")); + String content = Files.readString(config.toPath()); assert content.contains(""); } From e9c59695a3a8ec01a4e7dfc1b9290551a16fdbb0 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 5 May 2024 21:00:46 -0600 Subject: [PATCH 3/3] Fix assertion for new data format --- .../integrations/jobdsl/PromotionsDslContextExtensionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java index b23e038a..e0b01d43 100644 --- a/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java +++ b/src/test/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java @@ -112,7 +112,7 @@ public void testShouldGenerateTheDynamicDslJob() throws Exception { TopLevelItem item = j.jenkins.getItem("dynamic-dsl-test"); File config = new File(item.getRootDir(), "promotions/Development/config.xml"); String content = Files.readString(config.toPath()); - assert content.contains(""); + assert content.contains("