Skip to content

Commit

Permalink
added option to mark build as unstable when using deprecated features
Browse files Browse the repository at this point in the history
[FIXES JENKINS-37418]
  • Loading branch information
daspilker committed Sep 22, 2016
1 parent baf15c1 commit b0c7569
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/Home.md
Expand Up @@ -33,6 +33,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
* Added an option to mark a seed job build as failed if a plugin must be installed or updated to support all feature
used in DSL scripts
([JENKINS-37417](https://issues.jenkins-ci.org/browse/JENKINS-37417))
* Added an option to mark a seed job build as unstable when using deprecated features
([JENKINS-37418](https://issues.jenkins-ci.org/browse/JENKINS-37418))
* Support for the older versions of the
[Exclusion Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Exclusion-Plugin) is deprecated, see
[Migration](Migration#migrating-to-152)
Expand Down
1 change: 1 addition & 0 deletions job-dsl-plugin/build.gradle
Expand Up @@ -67,5 +67,6 @@ dependencies {
optionalJenkinsPlugins 'org.jenkinsci.plugins:managed-scripts:1.2.1@jar'
jenkinsTest 'org.jenkins-ci.plugins:cloudbees-folder:5.0@jar'
jenkinsTest 'org.jenkins-ci.plugins:nested-view:1.14@jar'
jenkinsTest 'org.jenkins-ci.plugins:Exclusion:0.11@jar'
jenkinsTest 'org.jenkins-ci.main:jenkins-test-harness:2.7@jar'
}
Expand Up @@ -135,6 +135,8 @@ public void setIgnoreMissingFiles(boolean ignoreMissingFiles) {

private boolean failOnMissingPlugin;

private boolean unstableOnDeprecation;

private RemovedJobAction removedJobAction = RemovedJobAction.IGNORE;

private RemovedViewAction removedViewAction = RemovedViewAction.IGNORE;
Expand Down Expand Up @@ -256,6 +258,15 @@ public void setFailOnMissingPlugin(boolean failOnMissingPlugin) {
this.failOnMissingPlugin = failOnMissingPlugin;
}

public boolean isUnstableOnDeprecation() {
return unstableOnDeprecation;
}

@DataBoundSetter
public void setUnstableOnDeprecation(boolean unstableOnDeprecation) {
this.unstableOnDeprecation = unstableOnDeprecation;
}

public RemovedJobAction getRemovedJobAction() {
return removedJobAction;
}
Expand Down Expand Up @@ -328,6 +339,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
listener.getLogger(), env, run, workspace, getLookupStrategy()
);
jenkinsJobManagement.setFailOnMissingPlugin(failOnMissingPlugin);
jenkinsJobManagement.setUnstableOnDeprecation(unstableOnDeprecation);
JobManagement jobManagement = new InterruptibleJobManagement(jenkinsJobManagement);

ScriptRequestGenerator generator = new ScriptRequestGenerator(workspace, env);
Expand Down
Expand Up @@ -63,7 +63,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static hudson.model.Result.FAILURE;
import static hudson.model.Result.UNSTABLE;
import static hudson.model.View.createViewFromXML;
import static java.lang.String.format;
Expand All @@ -85,6 +84,7 @@ public class JenkinsJobManagement extends AbstractJobManagement {
private final Map<javaposse.jobdsl.dsl.Item, DslEnvironment> environments =
new HashMap<javaposse.jobdsl.dsl.Item, DslEnvironment>();
private boolean failOnMissingPlugin;
private boolean unstableOnDeprecation;

@Deprecated
public JenkinsJobManagement(PrintStream outputLogger, Map<String, ?> envVars, AbstractBuild<?, ?> build,
Expand Down Expand Up @@ -115,6 +115,10 @@ void setFailOnMissingPlugin(boolean failOnMissingPlugin) {
this.failOnMissingPlugin = failOnMissingPlugin;
}

void setUnstableOnDeprecation(boolean unstableOnDeprecation) {
this.unstableOnDeprecation = unstableOnDeprecation;
}

@Override
public String getConfig(String path) throws JobConfigurationNotFoundException {
LOGGER.log(Level.INFO, format("Getting config for Job %s", path));
Expand Down Expand Up @@ -306,6 +310,14 @@ public void logPluginDeprecationWarning(String pluginShortName, String minimumVe
}
}

@Override
protected void logDeprecationWarning(String subject, String details) {
super.logDeprecationWarning(subject, details);
if (unstableOnDeprecation && run != null) {
run.setResult(UNSTABLE);
}
}

@Override
public void requirePlugin(String pluginShortName) {
requirePlugin(pluginShortName, false);
Expand Down
Expand Up @@ -41,5 +41,9 @@
<f:entry title="Fail build if a plugin must be installed or updated:" field="failOnMissingPlugin">
<f:checkbox name="failOnMissingPlugin" checked="${instance.failOnMissingPlugin}"/>
</f:entry>

<f:entry title="Mark build as unstable when using deprecated features:" field="unstableOnDeprecation">
<f:checkbox name="unstableOnDeprecation" checked="${instance.unstableOnDeprecation}"/>
</f:entry>
</f:advanced>
</j:jelly>
@@ -0,0 +1,4 @@
<div>
If checked, marks the build as unstable when using deprecated features. If not checked, a warning will be
printed to the build log only.
</div>
Expand Up @@ -10,6 +10,7 @@ import hudson.model.FreeStyleProject
import hudson.model.Items
import hudson.model.Label
import hudson.model.ListView
import hudson.model.Result
import hudson.model.View
import hudson.slaves.DumbSlave
import javaposse.jobdsl.dsl.GeneratedJob
Expand All @@ -25,12 +26,14 @@ import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.WithoutJenkins
import spock.lang.Specification
import spock.lang.Unroll

import static hudson.model.Result.FAILURE
import static hudson.model.Result.SUCCESS
import static hudson.model.Result.UNSTABLE
import static org.junit.Assert.assertTrue

@Unroll
class ExecuteDslScriptsSpec extends Specification {
private static final String UTF_8 = 'UTF-8'

Expand Down Expand Up @@ -1201,17 +1204,27 @@ class ExecuteDslScriptsSpec extends Specification {
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').readToString().trim() == 'lorem ipsum'
}

def 'deprecation warning in DSL script'() {
def 'deprecation warning in DSL script with unstableOnDeprecation set to #{unstableOnDeprecation}'() {
setup:
FreeStyleProject job = jenkinsRule.createFreeStyleProject('seed')
job.buildersList.add(new ExecuteDslScripts(this.class.getResourceAsStream('deprecation.groovy').text))
job.buildersList.add(new ExecuteDslScripts(
scriptText: this.class.getResourceAsStream('deprecation.groovy').text,
unstableOnDeprecation: unstableOnDeprecation
))
job.onCreatedFromScratch()

when:
FreeStyleBuild freeStyleBuild = job.scheduleBuild2(0).get()
FreeStyleBuild build = job.scheduleBuild2(0).get()

then:
freeStyleBuild.getLog(25).join('\n') =~ /Warning: \(script, line 3\) mergePullRequest is deprecated/
build.getLog(25).join('\n') =~
/Warning: \(script, line 3\) support for Exclusion Plug-in versions older than 0.12 is deprecated/
build.result == result

where:
unstableOnDeprecation || result
true || Result.UNSTABLE
false || Result.SUCCESS
}

def 'unstable or failure on missing plugin'() {
Expand Down
@@ -1,6 +1,6 @@
multiJob('a') {
publishers {
mergePullRequest {
job('a') {
steps {
criticalBlock {
}
}
}

0 comments on commit b0c7569

Please sign in to comment.