diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java index 8c2649ff..cab64a31 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java @@ -500,14 +500,26 @@ public List getTriggerDescriptors() { return result; } + /** + * Update an existing trigger or add a new one. + * @param trigger Target trigger instance. + */ public void addTrigger(Trigger trigger) { + removeTrigger(trigger); + triggers.add(trigger); + trigger.start(this, true); + } + + /** + * Remove an existing trigger. + * @param trigger Target trigger instance. + */ + public void removeTrigger(Trigger trigger) { Trigger old = triggers.get(trigger.getDescriptor()); if (old != null) { old.stop(); triggers.remove(old); } - triggers.add(trigger); - trigger.start(this, true); } /** diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java index 24faed41..2f35e182 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java @@ -443,6 +443,18 @@ public void triggersRoundtrip() throws Exception { } + @Test + public void cleanTriggers() throws Exception { + SampleComputedFolder s = r.jenkins.createProject(SampleComputedFolder.class, "s"); + s.addTrigger(new PeriodicFolderTrigger("30m")); + + assertEquals(1, s.getTriggers().size()); + + s.removeTrigger(new PeriodicFolderTrigger("30m")); + + assertEquals(0, s.getTriggers().size()); + } + @Test @Issue("JENKINS-42511") public void concurrentEvents() throws Exception {