New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-60393] Ensure all plugins are loaded to configure the defaults #136
Changes from 4 commits
1949cf3
c672384
4a9a46a
b441c87
8db93bb
451e2f3
ae00f49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,15 @@ | |
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetric; | ||
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetricDescriptor; | ||
import hudson.Extension; | ||
import hudson.init.InitMilestone; | ||
import hudson.init.Initializer; | ||
import hudson.util.DescribableList; | ||
import jenkins.model.GlobalConfiguration; | ||
import net.sf.json.JSONObject; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
|
@@ -18,7 +22,7 @@ | |
public class AbstractFolderConfiguration extends GlobalConfiguration { | ||
|
||
private List<FolderHealthMetric> healthMetrics; | ||
|
||
@Nonnull | ||
public static AbstractFolderConfiguration get() { | ||
AbstractFolderConfiguration instance = GlobalConfiguration.all().get(AbstractFolderConfiguration.class); | ||
|
@@ -30,27 +34,47 @@ public static AbstractFolderConfiguration get() { | |
|
||
@DataBoundConstructor | ||
public AbstractFolderConfiguration() { | ||
load(); | ||
if (healthMetrics == null) { | ||
this.load(); | ||
} | ||
|
||
/** | ||
* Auto-configure the default metrics after all plugins have been loaded. | ||
*/ | ||
@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.JOB_LOADED) | ||
public static void autoConfigure() { | ||
AbstractFolderConfiguration abstractFolderConfiguration = AbstractFolderConfiguration.get(); | ||
if (abstractFolderConfiguration.healthMetrics == null) { | ||
List<FolderHealthMetric> metrics = new ArrayList<>(); | ||
for (FolderHealthMetricDescriptor d : FolderHealthMetricDescriptor.all()) { | ||
FolderHealthMetric metric = d.createDefault(); | ||
if (metric != null) { | ||
metrics.add(metric); | ||
} | ||
} | ||
healthMetrics = new DescribableList<FolderHealthMetric, FolderHealthMetricDescriptor>(this, metrics); | ||
abstractFolderConfiguration.setHealthMetrics(new DescribableList<FolderHealthMetric, | ||
FolderHealthMetricDescriptor>(abstractFolderConfiguration, metrics)); | ||
} | ||
} | ||
|
||
@Nonnull | ||
public List<FolderHealthMetric> getHealthMetrics() { | ||
return healthMetrics == null ? Collections.emptyList() : healthMetrics; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setHealthMetrics(List<FolderHealthMetric> healthMetrics) { | ||
this.healthMetrics = healthMetrics; | ||
save(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean configure(StaplerRequest req, JSONObject json) { | ||
if(json.containsKey("healthMetrics")) { | ||
req.bindJSON(this, json); | ||
this.save(); | ||
} else { | ||
this.setHealthMetrics(Collections.emptyList()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed Maybe simpler to this.setHealthMetrics(Collections.emptyList());
req.bindJSON(this, json);
this.save(); ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah I see now the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah this would |
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember the implications of having both
after
andbefore
rules in the same initializer. But I guess you've verified it works as intended?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested this yes. I am not sure how to create a test for the initialization though ?
The real fix is to perform the auto configuration outside the constructor and when plugins are loaded:
after = InitMilestone.EXTENSIONS_AUGMENTED
. I do believe that this is enough. I added thebefore = InitMilestone.JOB_LOADED
because I feel this is consistent (the global metrics will be used on Folder's creation)