public void setConcurrentBuild(boolean b) throws IOException { concurrentBuild = null; boolean propertyExists = getProperty(DisableConcurrentBuildsJobProperty.class) != null; // If the property exists, concurrent builds are disabled. So if the argument here is true and the // property exists, we need to remove the property, while if the argument is false and the property // does not exist, we need to add the property. Yay for flipping boolean values around! if (propertyExists == b) { BulkChange bc = new BulkChange(this); try { removeProperty(DisableConcurrentBuildsJobProperty.class); if (!b) { addProperty(new DisableConcurrentBuildsJobProperty()); } bc.commit(); } finally { bc.abort(); } } }
assertTrue(action2.getParameters().isEmpty()); job.addProperty(new DisableConcurrentBuildsJobProperty()); job.addProperty(new DisableConcurrentBuildsJobProperty()); job.addProperty(new DisableConcurrentBuildsJobProperty());
@LocalData @Test public void trackerPropertyUpgrade() throws Exception { WorkflowJob p = j.jenkins.getItemByFullName("trackerPropertyUpgrade", WorkflowJob.class); assertNotNull(p); WorkflowRun b1 = p.getLastBuild(); assertNotNull(b1); assertNotNull(b1.getAction(DeclarativeJobPropertyTrackerAction.class)); assertNull(p.getAction(DeclarativeJobPropertyTrackerAction.class)); p.addProperty(new DisableConcurrentBuildsJobProperty()); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("simpleParameters"), true)); WorkflowRun b2 = j.buildAndAssertSuccess(p); assertNull(b2.getAction(DeclarativeJobPropertyTrackerAction.class)); assertNull(p.getProperty(BuildDiscarderProperty.class)); ParametersDefinitionProperty parameters = p.getProperty(ParametersDefinitionProperty.class); assertNotNull(parameters); assertNotNull(parameters.getParameterDefinition("flag")); DeclarativeJobPropertyTrackerAction action2 = p.getAction(DeclarativeJobPropertyTrackerAction.class); assertNotNull(action2); assertFalse(action2.getParameters().isEmpty()); assertEquals("flag", action2.getParameters().iterator().next()); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); WorkflowRun b3 = j.buildAndAssertSuccess(p); assertNull(b3.getAction(DeclarativeJobPropertyTrackerAction.class)); assertNull(p.getProperty(BuildDiscarderProperty.class)); assertNull(p.getProperty(ParametersDefinitionProperty.class)); DeclarativeJobPropertyTrackerAction action3 = p.getAction(DeclarativeJobPropertyTrackerAction.class); assertNotNull(action3); assertTrue(action3.getParameters().isEmpty()); }
@Issue("JENKINS-44848") @Test public void onlyRemoveJenkinsfileProperties() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.addProperty(new DisableConcurrentBuildsJobProperty()); // Base case - not calling the properties step p.setDefinition(new CpsFlowDefinition("echo 'Not doing anything'", true)); r.buildAndAssertSuccess(p); assertNotNull(p.getProperty(DisableConcurrentBuildsJobProperty.class)); assertNull(p.getAction(JobPropertyTrackerAction.class)); // Adding a property, make sure the predefined one is still there. // TODO Jenkins 2.x use symbols p.setDefinition(new CpsFlowDefinition("properties([buildDiscarder(logRotator(numToKeepStr: '1'))])", true)); r.buildAndAssertSuccess(p); assertNotNull(p.getProperty(DisableConcurrentBuildsJobProperty.class)); assertNotNull(p.getProperty(BuildDiscarderProperty.class)); JobPropertyTrackerAction action2 = p.getAction(JobPropertyTrackerAction.class); assertNotNull(action2); assertEquals(1, action2.getJobPropertyDescriptors().size()); assertEquals(Collections.singleton(r.jenkins.getDescriptor(BuildDiscarderProperty.class).getId()), action2.getJobPropertyDescriptors()); // Make sure the predefined property is still there after we remove the properties-step-defined property. p.setDefinition(new CpsFlowDefinition("properties([])", true)); r.buildAndAssertSuccess(p); assertNotNull(p.getProperty(DisableConcurrentBuildsJobProperty.class)); assertNull(p.getProperty(BuildDiscarderProperty.class)); JobPropertyTrackerAction action3 = p.getAction(JobPropertyTrackerAction.class); assertNotNull(action3); assertTrue(action3.getJobPropertyDescriptors().isEmpty()); }
@Issue("JENKINS-44621") @Test public void externalPropsNotRemoved() throws Exception { WorkflowRun b = getAndStartNonRepoBuild("simpleJobProperties"); j.assertBuildStatusSuccess(j.waitForCompletion(b)); WorkflowJob job = b.getParent(); assertNotNull(job.getProperty(BuildDiscarderProperty.class)); job.addProperty(new DisableConcurrentBuildsJobProperty()); job.setQuietPeriod(15); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); assertNull(job.getProperty(BuildDiscarderProperty.class)); assertNotNull(job.getProperty(DisableConcurrentBuildsJobProperty.class)); int externalPropCount = 0; for (JobProperty p : job.getAllProperties()) { if (p instanceof DisableConcurrentBuildsJobProperty) { externalPropCount++; } } assertEquals(1, externalPropCount); assertEquals(15, job.getQuietPeriod()); }