Skip to content

Commit

Permalink
Initialize default view earlier in the init process. (#8413)
Browse files Browse the repository at this point in the history
* Initialize default view earlier in the init process.

Perform the default view initialization next to loading the
configuration from disk.

This is more consistent as the primary view and views are cleared just
before loading from disk, which leads to a temporary inconsistent state while
Jenkins initializes.

* Update core/src/main/java/jenkins/model/Jenkins.java

Co-authored-by: Jesse Glick <jglick@cloudbees.com>

* View initialization should happen even when the config file doesn't exist.

* Add a test

---------

Co-authored-by: Jesse Glick <jglick@cloudbees.com>
  • Loading branch information
Vlatombe and jglick authored Aug 29, 2023
1 parent 8365d61 commit 7b1dfd8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
19 changes: 9 additions & 10 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -3378,6 +3378,15 @@ private void loadConfig() throws IOException {
// load from disk
cfg.unmarshal(Jenkins.this);
}
// initialize views by inserting the default view if necessary
// this is both for clean Jenkins and for backward compatibility.
if (views.isEmpty() || primaryView == null) {
View v = new AllView(AllView.DEFAULT_VIEW_NAME);
setViewOwner(v);
views.add(0, v);
primaryView = v.getViewName();
}
primaryView = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryView);
configLoaded = true;
try {
checkRawBuildsDir(buildsDir);
Expand Down Expand Up @@ -3536,16 +3545,6 @@ public void run(Reactor session) throws Exception {
getAssignedLabels();
}

// initialize views by inserting the default view if necessary
// this is both for clean Jenkins and for backward compatibility.
if (views.size() == 0 || primaryView == null) {
View v = new AllView(AllView.DEFAULT_VIEW_NAME);
setViewOwner(v);
views.add(0, v);
primaryView = v.getViewName();
}
primaryView = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryView);

if (useSecurity != null && !useSecurity) {
// forced reset to the unsecure mode.
// this works as an escape hatch for people who locked themselves out.
Expand Down
33 changes: 33 additions & 0 deletions test/src/test/java/jenkins/model/JenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.ExtensionList;
import hudson.XmlFile;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Computer;
import hudson.model.Failure;
import hudson.model.FreeStyleProject;
Expand Down Expand Up @@ -838,4 +840,35 @@ public String getUrlName() {
return "login123";
}
}

@Test
public void checkInitialView() {
assertTrue(CheckInitialViewExtension.hasPrimaryView);
}

@TestExtension(value = "checkInitialView")
public static class CheckInitialViewExtension implements RootAction {
private static boolean hasPrimaryView;

@Initializer(after = InitMilestone.SYSTEM_CONFIG_LOADED, before = InitMilestone.JOB_CONFIG_ADAPTED)
public static void checkViews() {
hasPrimaryView = Jenkins.get().getPrimaryView() != null;
}


@Override
public String getIconFileName() {
return null;
}

@Override
public String getDisplayName() {
return null;
}

@Override
public String getUrlName() {
return null;
}
}
}

0 comments on commit 7b1dfd8

Please sign in to comment.