Permalink
Browse files

[FIXED JENKINS-44149] Probably clean up old props/triggers/params

Previously we weren't calling the properties step if there were no
properties (i.e., no job properties from options, no triggers, no
parameters), but that left lurking properties when you removed all
properties, triggers and parameters from the Jenkinsfile. So now let's
check the job config to see if there were *already* defined properties
before deciding not to call the properties step. Also make sure that
PipelineTriggersJobProperty and ParametersDefinitionProperty are not
empty, rather than just present.
  • Loading branch information...
abayer committed May 18, 2017
1 parent cde871f commit 71a6001202218266ea16c18f37a86b4af17d7459
@@ -33,6 +33,7 @@ import groovy.json.StringEscapeUtils
import hudson.ExtensionList
import hudson.model.Describable
import hudson.model.Descriptor
import hudson.model.ParametersDefinitionProperty
import hudson.model.Result
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.lang.StringUtils
@@ -71,7 +72,9 @@ import org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
import org.jenkinsci.plugins.workflow.graph.FlowNode
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
import org.jenkinsci.plugins.workflow.steps.StepDescriptor
import org.jenkinsci.plugins.workflow.support.steps.StageStep
@@ -212,6 +215,27 @@ public class Utils {
}
}

static boolean hasJobProperties(CpsScript script) {
WorkflowRun r = script.$build()

WorkflowJob j = r.getParent()

return j.getAllProperties().any { p ->
// We only consider PipelineTriggersJobProperty and ParametersDefinitionProperty if they're empty.
if (p instanceof PipelineTriggersJobProperty) {
if (!p.getTriggers().isEmpty()) {
return true
}
} else if (p instanceof ParametersDefinitionProperty) {
if (!p.getParameterDefinitions().isEmpty()) {
return true
}
} else {
return true
}
}
}

static Root attachDeclarativeActions(@Nonnull Root root, CpsScript script) {
WorkflowRun r = script.$build()
ModelASTPipelineDef model = Converter.parseFromWorkflowRun(r)
@@ -547,7 +547,7 @@ public class ModelInterpreter implements Serializable {
if (root.parameters != null) {
jobProps.add(script.parameters(root.parameters.parameters))
}
if (!jobProps.isEmpty()) {
if (!jobProps.isEmpty() || Utils.hasJobProperties(script)) {
script.properties(jobProps)
}
}
@@ -33,14 +33,17 @@
import hudson.triggers.Trigger;
import jenkins.model.BuildDiscarder;
import jenkins.model.BuildDiscarderProperty;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty;
import org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class OptionsTest extends AbstractModelDefTest {
@@ -146,4 +149,20 @@ public void multipleWrappers() throws Exception {
.go();
}

@Issue("JENKINS-44149")
@Test
public void propsRemoved() throws Exception {
WorkflowRun b = getAndStartNonRepoBuild("simpleJobProperties");
j.assertBuildStatusSuccess(j.waitForCompletion(b));

WorkflowJob job = b.getParent();
assertNotNull(job.getProperty(BuildDiscarderProperty.class));

job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true));
WorkflowRun b2 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatusSuccess(j.waitForCompletion(b2));

assertNull(job.getProperty(BuildDiscarderProperty.class));
}

}
@@ -26,12 +26,15 @@

import hudson.model.BooleanParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class ParametersTest extends AbstractModelDefTest {
@@ -54,4 +57,21 @@ public void simpleParameters() throws Exception {
assertTrue(bpd.isDefaultValue());
}

@Issue("JENKINS-44149")
@Test
public void paramsRemoved() throws Exception {
WorkflowRun b = getAndStartNonRepoBuild("simpleParameters");
j.assertBuildStatusSuccess(j.waitForCompletion(b));

WorkflowJob job = b.getParent();
ParametersDefinitionProperty paramProp = job.getProperty(ParametersDefinitionProperty.class);
assertNotNull(paramProp);
assertEquals(1, paramProp.getParameterDefinitions().size());

job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true));
WorkflowRun b2 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatusSuccess(j.waitForCompletion(b2));

assertNull(job.getProperty(ParametersDefinitionProperty.class));
}
}
@@ -26,13 +26,16 @@

import hudson.triggers.TimerTrigger;
import hudson.triggers.Trigger;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TriggersTest extends AbstractModelDefTest {
@@ -58,4 +61,21 @@ public void simpleTriggers() throws Exception {
assertEquals("@daily", timer.getSpec());
}

@Issue("JENKINS-44149")
@Test
public void triggersRemoved() throws Exception {
WorkflowRun b = getAndStartNonRepoBuild("simpleTriggers");
j.assertBuildStatusSuccess(j.waitForCompletion(b));

WorkflowJob job = b.getParent();
PipelineTriggersJobProperty triggersJobProperty = job.getProperty(PipelineTriggersJobProperty.class);
assertNotNull(triggersJobProperty);
assertEquals(1, triggersJobProperty.getTriggers().size());

job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true));
WorkflowRun b2 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatusSuccess(j.waitForCompletion(b2));

assertNull(job.getProperty(PipelineTriggersJobProperty.class));
}
}
@@ -0,0 +1,37 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent none
stages {
stage("foo") {
steps {
echo "hello"
}
}
}
}



0 comments on commit 71a6001

Please sign in to comment.