Skip to content

Commit

Permalink
[FIXES JENKINS-47406] Milestone JOB_LOADED is now attained after clea…
Browse files Browse the repository at this point in the history
…ning up obsolete items (#3078)

* [FIXES JENKINS-47406] Milestone JOB_LOADED is now attained after cleaning up obsolete items

This allows @Initializer(after=InitMilestone.JOB_LOADED) implementations
to create items safely without risking them to be removed.

* [JENKINS-47406] Add test with an initializer creating a job

* Revert "[FIXES JENKINS-47406] Milestone JOB_LOADED is now attained after cleaning up obsolete items"

This reverts commit 79ce796.

* [JENKINS-47406] Reinstate the test with Jesse's solution

* Reverted this commit by mistake

Revert "Revert "[FIXES JENKINS-47406] Milestone JOB_LOADED is now attained after cleaning up obsolete items""

This reverts commit 36c499c.

* Add link to hpi sources

* Fix Mix-up after the previous merge
  • Loading branch information
Vlatombe authored and oleg-nenashev committed Jan 21, 2018
1 parent 53ac66e commit 0311b2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -3054,8 +3054,9 @@ public void run(Reactor session) throws Exception {
}
});

List<Handle> loadJobs = new ArrayList<>();
for (final File subdir : subdirs) {
g.requires(loadJenkins).attains(JOB_LOADED).notFatal().add("Loading item " + subdir.getName(), new Executable() {
loadJobs.add(g.requires(loadJenkins).attains(JOB_LOADED).notFatal().add("Loading item " + subdir.getName(), new Executable() {
public void run(Reactor session) throws Exception {
if(!Items.getConfigFile(subdir).exists()) {
//Does not have job config file, so it is not a jenkins job hence skip it
Expand All @@ -3065,10 +3066,10 @@ public void run(Reactor session) throws Exception {
items.put(item.getName(), item);
loadedNames.add(item.getName());
}
});
}));
}

g.requires(JOB_LOADED).attains(COMPLETED).add("Cleaning up obsolete items deleted from the disk", new Executable() {
g.requires(loadJobs.toArray(new Handle[loadJobs.size()])).attains(JOB_LOADED).add("Cleaning up obsolete items deleted from the disk", new Executable() {
public void run(Reactor reactor) throws Exception {
// anything we didn't load from disk, throw them away.
// doing this after loading from disk allows newly loaded items
Expand Down
11 changes: 11 additions & 0 deletions test/src/test/java/jenkins/model/JenkinsTest.java
Expand Up @@ -23,6 +23,7 @@
*/
package jenkins.model;

import static hudson.init.InitMilestone.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
Expand All @@ -44,6 +45,7 @@
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import hudson.init.Initializer;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Computer;
Expand Down Expand Up @@ -72,6 +74,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.recipes.WithPlugin;
import org.kohsuke.stapler.HttpResponse;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
Expand All @@ -83,6 +86,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -683,4 +687,11 @@ public void versionIsSavedInSave() throws Exception {
VersionNumber nullVersion = Jenkins.getStoredVersion();
assertNull(nullVersion);
}

@Issue("JENKINS-47406")
@Test
@WithPlugin("jenkins-47406.hpi") // Sources: https://github.com/Vlatombe/jenkins-47406
public void jobCreatedByInitializerIsRetained() {
assertNotNull("JENKINS-47406 should exist", j.jenkins.getItem("JENKINS-47406"));
}
}
Binary file not shown.

0 comments on commit 0311b2d

Please sign in to comment.