Skip to content

Commit

Permalink
Merge pull request #6 from amuniz/JENKINS-38641
Browse files Browse the repository at this point in the history
[FIX JENKINS-38641] Clean up tracked jobs on deleted
  • Loading branch information
amuniz committed Dec 28, 2016
2 parents 5077e3a + 9826f7a commit a043481
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,5 +3,5 @@ target
.classpath
.settings
work
*.iml
.idea
*.iml
Expand Up @@ -26,7 +26,6 @@
import static java.util.logging.Level.WARNING;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -39,13 +38,10 @@
import javax.annotation.CheckForNull;

import com.google.common.base.Predicate;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
import hudson.model.Item;
import hudson.model.listeners.ItemListener;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
import org.jenkinsci.plugins.workflow.graph.BlockStartNode;
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graph.FlowStartNode;
import org.jenkinsci.plugins.workflow.graphanalysis.FlowScanningUtils;
import org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution;
Expand Down Expand Up @@ -339,6 +335,29 @@ public static final class Listener extends RunListener<Run<?,?>> {
}
}

/**
* Clean up tracked jobs on deleted.
*/
@Extension
public static class CleanupJobsOnDelete extends ItemListener {

@Override
public void onDeleted(Item item) {
if (item instanceof Job) {
String jobName = item.getFullName();
Map<Integer, Milestone> job = getMilestonesByOrdinalByJob().get(jobName);
if (job != null) {
remove(jobName);
}
}
}

private synchronized void remove(String jobName) {
getMilestonesByOrdinalByJob().remove(jobName);
save();
}
}

private static final long serialVersionUID = 1L;

}
@@ -0,0 +1,51 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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.
*/
package org.jenkinsci.plugins.pipeline.milestone;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class CleaupJobsTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void cleaupTrackedJobsOnDeleted() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("milestone 1;milestone 2"));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

p.delete();

p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("milestone 1;milestone 2"));
j.assertBuildStatusSuccess(p.scheduleBuild2(0)); // build #1 is not cancelled

}
}

0 comments on commit a043481

Please sign in to comment.