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 Map<TriggerDescriptor,Trigger<?>> getTriggersMap() { Map<TriggerDescriptor,Trigger<?>> triggerMap = new HashMap<>(); for (Trigger t : getTriggers()) { TriggerDescriptor td = t.getDescriptor(); triggerMap.put(td, t); } return triggerMap; }
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(); } }
@Override public void addProperty(JobProperty jobProp) throws IOException { super.addProperty(jobProp); getTriggersJobProperty().stopTriggers(); getTriggersJobProperty().startTriggers(Items.currentlyUpdatingByXml()); }
@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()); }
@Issue("JENKINS-44621") @Test public void externalTriggersNotRemoved() 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()); List<Trigger> newTriggers = new ArrayList<>(); newTriggers.addAll(triggersJobProperty.getTriggers()); newTriggers.add(new SCMTrigger("1 1 1 * *")); job.removeProperty(triggersJobProperty); job.addProperty(new PipelineTriggersJobProperty(newTriggers)); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); PipelineTriggersJobProperty newProp = job.getProperty(PipelineTriggersJobProperty.class); assertNotNull(newProp); assertEquals(1, newProp.getTriggers().size()); Trigger t = newProp.getTriggers().get(0); assertNotNull(t); assertTrue(t instanceof SCMTrigger); }
public PipelineTriggersJobProperty getTriggersJobProperty() { PipelineTriggersJobProperty triggerProp = getProperty(PipelineTriggersJobProperty.class); if (triggerProp == null) { triggerProp = new PipelineTriggersJobProperty(new ArrayList<Trigger>()); } return triggerProp; }
PipelineTriggersJobProperty thisProp = new PipelineTriggersJobProperty(new ArrayList<Trigger>(trigList.toList())); thisProp.startTriggers(true); return thisProp; } catch (Exception e) { try { owner.addTriggersJobPropertyWithoutStart(oldProp); oldProp.startTriggers(true); } catch (IOException e) { LOGGER.log(Level.WARNING, "could not revert to original configured triggers", e);
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); } }
@Override public Map<TriggerDescriptor, Trigger<?>> getTriggers() { return getTriggersJobProperty().getTriggersMap(); }
@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')])])"); }
@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()); }
@Ignore("Triggers are set before withEnv is called.") @Test public void envVarInTriggers() throws Exception { WorkflowRun b = expect("envVarInTriggers") .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()); }
@Override public SCMTrigger getSCMTrigger() { for (Trigger t : getTriggersJobProperty().getTriggers()) { if (t instanceof SCMTrigger) { return (SCMTrigger) t; } } return null; }
@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')])])"); }
assertNotNull(trigProp); assertEquals(1, trigProp.getTriggers().size()); TimerTrigger.DescriptorImpl timerDesc = j.jenkins.getDescriptorByType(TimerTrigger.DescriptorImpl.class); Trigger trigger = trigProp.getTriggerForDescriptor(timerDesc); assertNotNull(trigger);
@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')])])"); }