From 3de67a19459891ec5b0a44d4f3f3bb1f7561672f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renzo=20Cris=C3=B3stomo?= Date: Thu, 24 May 2018 11:41:15 +0200 Subject: [PATCH] Add failOnMissingPlugin option to dslGenerateXml task --- .../plugins/jobdsl/tasks/GenerateXmlTest.groovy | 15 +++++++++++++++ .../build-with-missing-plugins.gradle | 11 +++++++++++ .../jobdsl/FilteringJenkinsJobManagement.groovy | 4 +++- .../plugins/jobdsl/tasks/GenerateXmlTask.groovy | 17 ++++++++++++++++- .../tasks/runners/GenerateXmlRunner.groovy | 3 ++- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 plugin/src/functionalTest/resources/generateXml/build-with-missing-plugins.gradle diff --git a/plugin/src/functionalTest/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTest.groovy b/plugin/src/functionalTest/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTest.groovy index a3cba79..2dc67a8 100644 --- a/plugin/src/functionalTest/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTest.groovy +++ b/plugin/src/functionalTest/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTest.groovy @@ -68,6 +68,21 @@ class GenerateXmlTest extends AbstractTaskTest { XMLUnit.compareXML(expectedText, actualText).identical() } + def 'failOnMissingPlugin fails the task if a required plugin is missing'() { + given: + buildFile << readBuildGradle('generateXml/build-with-missing-plugins.gradle') + copyResourceToTestDir('generateXml/folder.groovy') + + when: + def result = gradleRunner + .withArguments('dslGenerateXml', '--failOnMissingPlugin') + .buildAndFail() + + then: + result.task(':dslGenerateXml').outcome == TaskOutcome.FAILED + result.output.contains("plugin 'cloudbees-folder' needs to be installed") + } + def 'job in folder is generated in the right subfolder'() { given: buildFile << readBuildGradle('generateXml/build.gradle') diff --git a/plugin/src/functionalTest/resources/generateXml/build-with-missing-plugins.gradle b/plugin/src/functionalTest/resources/generateXml/build-with-missing-plugins.gradle new file mode 100644 index 0000000..1ac8b3c --- /dev/null +++ b/plugin/src/functionalTest/resources/generateXml/build-with-missing-plugins.gradle @@ -0,0 +1,11 @@ +buildscript { + dependencies { + classpath files(CLASSPATH_STRING) + } +} + +apply plugin: 'com.here.jobdsl' + +dependencies { + compile localGroovy() +} diff --git a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/FilteringJenkinsJobManagement.groovy b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/FilteringJenkinsJobManagement.groovy index eb49d0d..132b5c0 100644 --- a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/FilteringJenkinsJobManagement.groovy +++ b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/FilteringJenkinsJobManagement.groovy @@ -11,9 +11,11 @@ class FilteringJenkinsJobManagement extends JenkinsJobManagement { private final ItemFilter filter - FilteringJenkinsJobManagement(ItemFilter filter, PrintStream outputLogger, Map envVars, File workspace) { + FilteringJenkinsJobManagement(ItemFilter filter, PrintStream outputLogger, Map envVars, + File workspace, boolean failOnMissingPlugin) { super(outputLogger, envVars, workspace) this.filter = filter + this.failOnMissingPlugin = failOnMissingPlugin } @Override diff --git a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTask.groovy b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTask.groovy index 94b6010..0259bed 100644 --- a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTask.groovy +++ b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/GenerateXmlTask.groovy @@ -1,14 +1,23 @@ package com.here.gradle.plugins.jobdsl.tasks +import org.gradle.api.internal.tasks.options.Option +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional + /** * Task that calls {@link com.here.gradle.plugins.jobdsl.tasks.runners.GenerateXmlRunner} to generate XML files for * all items and views configured in the project. */ class GenerateXmlTask extends AbstractDslTask { + @Input + @Optional + boolean failOnMissingPlugin + GenerateXmlTask() { super() description = 'Generate XML for all jobs.' + failOnMissingPlugin = false } @Override @@ -20,8 +29,14 @@ class GenerateXmlTask extends AbstractDslTask { @Override Map getProperties() { [ - outputDirectory: "${project.buildDir}/jobdsl/xml" + outputDirectory : "${project.buildDir}/jobdsl/xml", + failOnMissingPlugin: failOnMissingPlugin, ] } + @Option(option = 'failOnMissingPlugin', description = 'Fail the task if a required plugin is missing.') + void setFailOnMissingPlugin(boolean failOnMissingPlugin) { + this.failOnMissingPlugin = failOnMissingPlugin + } + } diff --git a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/runners/GenerateXmlRunner.groovy b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/runners/GenerateXmlRunner.groovy index cb93299..aa11bc5 100644 --- a/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/runners/GenerateXmlRunner.groovy +++ b/plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/tasks/runners/GenerateXmlRunner.groovy @@ -27,10 +27,11 @@ class GenerateXmlRunner extends AbstractTaskRunner { @Override JobManagement createJobManagement(Jenkins jenkins, ItemFilter filter) { + boolean failOnMissingPlugin = runProperties['failOnMissingPlugin'].toBoolean() def workspace = Files.createTempDirectory('jobdsl').toFile() workspace.deleteOnExit() - new FilteringJenkinsJobManagement(filter, System.out, [:], workspace) + new FilteringJenkinsJobManagement(filter, System.out, [:], workspace, failOnMissingPlugin) } @Override