@Override public void removeProperty(JobProperty jobProperty) throws IOException { // Need to make sure we stop any triggers. if (jobProperty instanceof PipelineTriggersJobProperty) { ((PipelineTriggersJobProperty)jobProperty).stopTriggers(); } super.removeProperty(jobProperty); }
@Override public SCMTrigger getSCMTrigger() { for (Trigger t : getTriggersJobProperty().getTriggers()) { if (t instanceof SCMTrigger) { return (SCMTrigger) t; } } return null; }
@Override public Map<TriggerDescriptor, Trigger<?>> getTriggers() { return getTriggersJobProperty().getTriggersMap(); }
@Test public void serializationTriggersGString() throws Exception { WorkflowRun b = expect("serializationTriggersGString") .logContains("[Pipeline] { (foo)", "hello") .logNotContains("[Pipeline] { (Post Actions)") .go(); WorkflowJob p = b.getParent(); PipelineTriggersJobProperty triggersJobProperty = p.getTriggersJobProperty(); assertNotNull(triggersJobProperty); assertEquals(1, triggersJobProperty.getTriggers().size()); TimerTrigger.DescriptorImpl timerDesc = j.jenkins.getDescriptorByType(TimerTrigger.DescriptorImpl.class); Trigger trigger = triggersJobProperty.getTriggerForDescriptor(timerDesc); assertNotNull(trigger); assertTrue(trigger instanceof TimerTrigger); TimerTrigger timer = (TimerTrigger) trigger; assertEquals("@daily", timer.getSpec()); }
public void setTriggers(List<Trigger<?>> inputTriggers) throws IOException { triggers = null; BulkChange bc = new BulkChange(this); try { PipelineTriggersJobProperty originalProp = getTriggersJobProperty(); removeProperty(PipelineTriggersJobProperty.class); PipelineTriggersJobProperty triggerProp = new PipelineTriggersJobProperty(null); triggerProp.setTriggers(inputTriggers); addProperty(triggerProp); bc.commit(); originalProp.stopTriggers(); // No longer need to start triggers here - that's done by when we add the property. } finally { bc.abort(); } }
public PipelineTriggersJobProperty getTriggersJobProperty() { PipelineTriggersJobProperty triggerProp = getProperty(PipelineTriggersJobProperty.class); if (triggerProp == null) { triggerProp = new PipelineTriggersJobProperty(new ArrayList<Trigger>()); } return triggerProp; }
public void addTrigger(Trigger trigger) throws IOException { BulkChange bc = new BulkChange(this); try { PipelineTriggersJobProperty originalProp = getTriggersJobProperty(); Trigger old = originalProp.getTriggerForDescriptor(trigger.getDescriptor()); if (old != null) { originalProp.removeTrigger(old); old.stop(); } originalProp.addTrigger(trigger); removeProperty(PipelineTriggersJobProperty.class); addProperty(originalProp); bc.commit(); } finally { bc.abort(); } }
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(); } } }
@Override public void addProperty(JobProperty jobProp) throws IOException { super.addProperty(jobProp); getTriggersJobProperty().stopTriggers(); getTriggersJobProperty().startTriggers(Items.currentlyUpdatingByXml()); }
public void removeTrigger(Trigger t) { // TODO: Will we get equality for trigger instances of the same Descriptor? Trigger toRemove = getTriggerForDescriptor(t.getDescriptor()); if (toRemove != null) { triggers.remove(toRemove); } }
/** Create and run a basic build before we mangle its persisted contents. Stores job number to jobIdNumber index 0. */ private static WorkflowRun runBasicBuild(JenkinsRule j, String jobName, int[] jobIdNumber, FlowDurabilityHint hint) throws Exception { WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, jobName); job.setDefinition(new CpsFlowDefinition("echo 'doSomething'", true)); job.addProperty(new DurabilityHintJobProperty(hint)); WorkflowRun run = j.buildAndAssertSuccess(job); jobIdNumber[0] = run.getNumber(); assertCompletedCleanly(run); return run; }
@Override public @Nonnull Collection<? extends Action> createFor(@Nonnull WorkflowJob job) { return job.getTriggersJobProperty().getAllTriggerActions(); }
@Test public void simpleTriggers() throws Exception { WorkflowRun b = expect("simpleTriggers") .logContains("[Pipeline] { (foo)", "hello") .logNotContains("[Pipeline] { (Post Actions)") .go(); WorkflowJob p = b.getParent(); PipelineTriggersJobProperty triggersJobProperty = p.getTriggersJobProperty(); assertNotNull(triggersJobProperty); assertEquals(1, triggersJobProperty.getTriggers().size()); TimerTrigger.DescriptorImpl timerDesc = j.jenkins.getDescriptorByType(TimerTrigger.DescriptorImpl.class); Trigger trigger = triggersJobProperty.getTriggerForDescriptor(timerDesc); assertNotNull(trigger); assertTrue(trigger instanceof TimerTrigger); TimerTrigger timer = (TimerTrigger) trigger; assertEquals("@daily", timer.getSpec()); }
@Issue("JENKINS-37477") @Test public void configRoundTripTrigger() throws Exception { List<JobProperty> properties = Collections.<JobProperty>singletonList(new PipelineTriggersJobProperty(Collections.<Trigger>singletonList(new TimerTrigger("@daily")))); String snippetJson = "{'propertiesMap': {\n" + " 'stapler-class-bag': 'true',\n" + " 'org-jenkinsci-plugins-workflow-job-properties-PipelineTriggersJobProperty': {'triggers': {\n" + " 'stapler-class-bag': 'true',\n" + " 'hudson-triggers-TimerTrigger': {'spec': '@daily'}\n" + " }}},\n" + " 'stapler-class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep',\n" + " '$class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep'}"; new SnippetizerTester(r).assertGenerateSnippet(snippetJson, "properties([pipelineTriggers([cron('@daily')])])", null); new SnippetizerTester(r).assertRoundTrip(new JobPropertyStep(properties), "properties([pipelineTriggers([cron('@daily')])])"); }
public Map<TriggerDescriptor,Trigger<?>> getTriggersMap() { Map<TriggerDescriptor,Trigger<?>> triggerMap = new HashMap<>(); for (Trigger t : getTriggers()) { TriggerDescriptor td = t.getDescriptor(); triggerMap.put(td, t); } return triggerMap; }
@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()); }
@Override public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { super.onLoad(parent, name); if (buildMixIn == null) { buildMixIn = createBuildMixIn(); } buildMixIn.onLoad(parent, name); if (triggers != null && !triggers.isEmpty()) { setTriggers(triggers.toList()); } if (concurrentBuild != null) { setConcurrentBuild(concurrentBuild); } getTriggersJobProperty().stopTriggers(); getTriggersJobProperty().startTriggers(Items.currentlyUpdatingByXml()); }
@Issue("JENKINS-34464") @Test public void configRoundTripReverseBuildTrigger() throws Exception { List<JobProperty> properties = Collections.<JobProperty>singletonList(new PipelineTriggersJobProperty(Collections.<Trigger>singletonList(new ReverseBuildTrigger("some-job", Result.UNSTABLE)))); String snippetJson = "{'propertiesMap': {\n" + " 'stapler-class-bag': 'true',\n" + " 'org-jenkinsci-plugins-workflow-job-properties-PipelineTriggersJobProperty': {'triggers': {\n" + " 'stapler-class-bag': 'true',\n" + " 'jenkins-triggers-ReverseBuildTrigger': { 'threshold': 'UNSTABLE', 'upstreamProjects': 'some-job'}\n" + " }}},\n" + " 'stapler-class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep',\n" + " '$class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep'}"; new SnippetizerTester(r).assertGenerateSnippet(snippetJson, "properties([pipelineTriggers([upstream(threshold: 'UNSTABLE', upstreamProjects: 'some-job')])])", null); new SnippetizerTester(r).assertRoundTrip(new JobPropertyStep(properties), "properties([pipelineTriggers([upstream(threshold: 'UNSTABLE', upstreamProjects: 'some-job')])])"); }
@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)); j.buildAndAssertSuccess(job); assertNull(job.getProperty(PipelineTriggersJobProperty.class)); }
@Issue("JENKINS-37721") @Test public void configRoundTripSCMTrigger() throws Exception { List<JobProperty> properties = Collections.<JobProperty>singletonList(new PipelineTriggersJobProperty(Collections.<Trigger>singletonList(new SCMTrigger("@daily")))); String snippetJson = "{'propertiesMap': {\n" + " 'stapler-class-bag': 'true',\n" + " 'org-jenkinsci-plugins-workflow-job-properties-PipelineTriggersJobProperty': {'triggers': {\n" + " 'stapler-class-bag': 'true',\n" + " 'hudson-triggers-SCMTrigger': {'scmpoll_spec': '@daily', 'ignorePostCommitHooks': false }\n" + " }}},\n" + " 'stapler-class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep',\n" + " '$class': 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep'}"; new SnippetizerTester(r).assertGenerateSnippet(snippetJson, "properties([pipelineTriggers([pollSCM('@daily')])])", null); new SnippetizerTester(r).assertRoundTrip(new JobPropertyStep(properties), "properties([pipelineTriggers([pollSCM('@daily')])])"); }