From 7b1dfd8d838b9938346bafa1c3fde363cd5e8b9a Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Tue, 29 Aug 2023 18:59:14 +0200 Subject: [PATCH] Initialize default view earlier in the init process. (#8413) * 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 * View initialization should happen even when the config file doesn't exist. * Add a test --------- Co-authored-by: Jesse Glick --- core/src/main/java/jenkins/model/Jenkins.java | 19 +++++------ .../test/java/jenkins/model/JenkinsTest.java | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 29477e36d9..d021e5975e 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3378,6 +3378,15 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve // 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); @@ -3536,16 +3545,6 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve 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. diff --git a/test/src/test/java/jenkins/model/JenkinsTest.java b/test/src/test/java/jenkins/model/JenkinsTest.java index 479837a4ca..5c0c9acedb 100644 --- a/test/src/test/java/jenkins/model/JenkinsTest.java +++ b/test/src/test/java/jenkins/model/JenkinsTest.java @@ -42,6 +42,8 @@ import static org.junit.Assert.assertTrue; 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; @@ -838,4 +840,35 @@ public class JenkinsTest { 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; + } + } }