From ee4c85172f97d7bd3376a5775cc2e67fb168ff72 Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Mon, 4 Dec 2023 10:19:35 +0100 Subject: [PATCH] change configuration To avoid having to change code at 2 places when new labels are introduced, add a class for just labels and use it in global config and in job property. This is a breaking change that once applied a rollback requires manual intervention. --- README.md | 11 ++++ pom.xml | 1 + .../AlternateBuildLabelConfiguration.java | 37 ++++++------ .../customizebuildnow/AlternateBuildNow.java | 35 +++++------ .../BuildNowTextProperty.java | 51 ++++++++-------- .../plugins/customizebuildnow/Labels.java | 58 +++++++++++++++++++ .../config.jelly | 10 +--- .../BuildNowTextProperty/config-details.jelly | 10 +--- .../customizebuildnow/Labels/config.jelly | 12 ++++ .../help-alternateBuildButton.html | 0 .../help-alternateBuildNow.html | 0 .../help-alternateBuildWithParams.html | 0 .../AlternateBuildNowTest.java | 22 +++---- 13 files changed, 157 insertions(+), 90 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/customizebuildnow/Labels.java create mode 100644 src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/config.jelly rename src/main/resources/org/jenkinsci/plugins/customizebuildnow/{BuildNowTextProperty => Labels}/help-alternateBuildButton.html (100%) rename src/main/resources/org/jenkinsci/plugins/customizebuildnow/{BuildNowTextProperty => Labels}/help-alternateBuildNow.html (100%) rename src/main/resources/org/jenkinsci/plugins/customizebuildnow/{BuildNowTextProperty => Labels}/help-alternateBuildWithParams.html (100%) diff --git a/README.md b/README.md index 240668f..3382dbd 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,14 @@ The labels can be configured globally in the `Appearance` section and/or individ ![Sidepanel](/docs/sidepanel.png) ![Sidepanel with Parameters](/docs/sidepanel-parameters.png) ![Parameters](/docs/parameters.png) + + +### Configuration as Code +```yaml +appearance: + customizeBuildNow: + labels: + alternateBuildButton: "Execute" + alternateBuildNow: "Execute Now" + alternateBuildWithParams: "Execute with Parameters" +``` diff --git a/pom.xml b/pom.xml index 51a94ad..de9eef2 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,7 @@ 999999-SNAPSHOT 2.426.1 + 17 diff --git a/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration.java b/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration.java index d80fde9..aa83a19 100644 --- a/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration.java +++ b/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration.java @@ -13,34 +13,35 @@ @Extension public class AlternateBuildLabelConfiguration extends GlobalConfiguration { - public AlternateBuildLabelConfiguration() { - load(); - } - - private String alternateBuildNow; + private transient String alternateBuildNow; - private String alternateBuildWithParams; + private transient String alternateBuildWithParams; - private String alternateBuildButton; + private transient String alternateBuildButton; - public String getAlternateBuildNow() { - return alternateBuildNow; - } + private Labels labels; - public void setAlternateBuildNow(String alternateBuildNow) { - this.alternateBuildNow = alternateBuildNow; + public AlternateBuildLabelConfiguration() { + load(); } - public String getAlternateBuildWithParams() { - return alternateBuildWithParams; + @Override + public void load() { + super.load(); + if (labels == null) { + labels = new Labels(); + labels.setAlternateBuildButton(alternateBuildButton); + labels.setAlternateBuildNow(alternateBuildNow); + labels.setAlternateBuildWithParams(alternateBuildWithParams); + } } - public void setAlternateBuildWithParams(String alternateBuildWithParams) { - this.alternateBuildWithParams = alternateBuildWithParams; + public Labels getLabels() { + return labels; } - public String getAlternateBuildButton() { - return alternateBuildButton; + public void setLabels(Labels labels) { + this.labels = labels; } public void setAlternateBuildButton(String alternateBuildButton) { diff --git a/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java b/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java index 58f4181..0237c9f 100644 --- a/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java +++ b/src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java @@ -2,11 +2,9 @@ import hudson.Extension; import hudson.Util; -import hudson.model.Descriptor; import hudson.model.Job; import hudson.model.ParametersDefinitionProperty; import hudson.util.AlternativeUiTextProvider; -import jenkins.model.Jenkins; import jenkins.model.ParameterizedJobMixIn; /** @@ -20,40 +18,37 @@ public class AlternateBuildNow extends AlternativeUiTextProvider { @Override public String getText(Message message, T t) { if (message == ParameterizedJobMixIn.BUILD_NOW_TEXT) { - Job p = (Job) t; + Job p = (Job) t; BuildNowTextProperty bt = p.getProperty(BuildNowTextProperty.class); - String linkText = null; + Labels labels = AlternateBuildLabelConfiguration.get().getLabels(); if (bt != null) { - linkText = Util.fixEmptyAndTrim(bt.getAlternateBuildNow()); + labels = bt.getLabels(); } - if (linkText == null) { - linkText = Util.fixEmptyAndTrim(AlternateBuildLabelConfiguration.get().getAlternateBuildNow()); + if (labels != null) { + return Util.fixEmptyAndTrim(labels.getAlternateBuildNow()); } - return linkText; } if (message == ParameterizedJobMixIn.BUILD_WITH_PARAMETERS_TEXT) { - Job p = (Job) t; + Job p = (Job) t; BuildNowTextProperty bt = p.getProperty(BuildNowTextProperty.class); - String linkText = null; + Labels labels = AlternateBuildLabelConfiguration.get().getLabels(); if (bt != null) { - linkText = Util.fixEmptyAndTrim(bt.getAlternateBuildWithParams()); + labels = bt.getLabels(); } - if (linkText == null) { - linkText = Util.fixEmptyAndTrim(AlternateBuildLabelConfiguration.get().getAlternateBuildWithParams()); + if (labels != null) { + return Util.fixEmptyAndTrim(labels.getAlternateBuildWithParams()); } - return linkText; } if (message == ParametersDefinitionProperty.BUILD_BUTTON_TEXT) { - Job p = (Job) t; + Job p = (Job) t; BuildNowTextProperty bt = p.getProperty(BuildNowTextProperty.class); - String buttonText = null; + Labels labels = AlternateBuildLabelConfiguration.get().getLabels(); if (bt != null) { - buttonText = Util.fixEmptyAndTrim(bt.getAlternateBuildButton()); + labels = bt.getLabels(); } - if (buttonText == null) { - buttonText = Util.fixEmptyAndTrim(AlternateBuildLabelConfiguration.get().getAlternateBuildButton()); + if (labels != null) { + return Util.fixEmptyAndTrim(labels.getAlternateBuildButton()); } - return buttonText; } return null; } diff --git a/src/main/java/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty.java b/src/main/java/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty.java index d694f51..8c6d4ba 100644 --- a/src/main/java/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty.java +++ b/src/main/java/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty.java @@ -2,57 +2,60 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; -import hudson.model.*; +import hudson.model.Job; +import hudson.model.JobProperty; import jenkins.model.OptionalJobProperty; import jenkins.model.ParameterizedJobMixIn; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.StaplerRequest; /** * Allow jobs to provide alternate label to "Build Now" */ public class BuildNowTextProperty extends OptionalJobProperty> { - private final String alternateBuildNow; + private transient String alternateBuildNow; - private String alternateBuildWithParams; + private transient String alternateBuildWithParams; - private String alternateBuildButton; + private transient String alternateBuildButton; - @DataBoundConstructor - public BuildNowTextProperty(String alternateBuildNow) { - this.alternateBuildNow = alternateBuildNow; - } - - public String getAlternateBuildNow() { - return alternateBuildNow; - } + private Labels labels; - public String getAlternateBuildWithParams() { - return alternateBuildWithParams; + @DataBoundConstructor + public BuildNowTextProperty(Labels labels) { + this.labels = labels; } - @DataBoundSetter - public void setAlternateBuildWithParams(String alternateBuildWithParams) { - this.alternateBuildWithParams = alternateBuildWithParams; + /** + * @param alternateBuildNow new build now text + * @deprecated use {@link BuildNowTextProperty(Labels)} + */ + @Deprecated + public BuildNowTextProperty(String alternateBuildNow) { + labels = new Labels(); + labels.setAlternateBuildNow(alternateBuildNow); } - public String getAlternateBuildButton() { - return alternateBuildButton; + public Object readResolve() { + if (labels == null) { + labels = new Labels(); + labels.setAlternateBuildButton(alternateBuildButton); + labels.setAlternateBuildNow(alternateBuildNow); + labels.setAlternateBuildWithParams(alternateBuildWithParams); + } + return this; } - @DataBoundSetter - public void setAlternateBuildButton(String alternateBuildButton) { - this.alternateBuildButton = alternateBuildButton; + public Labels getLabels() { + return labels; } @Extension @Symbol("customizeBuildNow") public static final class DescriptorImpl extends OptionalJobPropertyDescriptor { - public DescriptorImpl() { super(BuildNowTextProperty.class); } diff --git a/src/main/java/org/jenkinsci/plugins/customizebuildnow/Labels.java b/src/main/java/org/jenkinsci/plugins/customizebuildnow/Labels.java new file mode 100644 index 0000000..daeb41d --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/customizebuildnow/Labels.java @@ -0,0 +1,58 @@ +package org.jenkinsci.plugins.customizebuildnow; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.Extension; +import hudson.model.AbstractDescribableImpl; +import hudson.model.Descriptor; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; + +public class Labels extends AbstractDescribableImpl { + + private String alternateBuildNow; + + private String alternateBuildWithParams; + + private String alternateBuildButton; + + @DataBoundConstructor + public Labels() { + } + + public String getAlternateBuildNow() { + return alternateBuildNow; + } + + @DataBoundSetter + public void setAlternateBuildNow(String alternateBuildNow) { + this.alternateBuildNow = alternateBuildNow; + } + + public String getAlternateBuildWithParams() { + return alternateBuildWithParams; + } + + @DataBoundSetter + public void setAlternateBuildWithParams(String alternateBuildWithParams) { + this.alternateBuildWithParams = alternateBuildWithParams; + } + + public String getAlternateBuildButton() { + return alternateBuildButton; + } + + @DataBoundSetter + public void setAlternateBuildButton(String alternateBuildButton) { + this.alternateBuildButton = alternateBuildButton; + } + + @Extension + public static class DescriptorImpl extends Descriptor { + + @NonNull + @Override + public String getDisplayName() { + return ""; + } + } +} diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration/config.jelly b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration/config.jelly index eba6b45..f2cca1a 100644 --- a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration/config.jelly @@ -1,14 +1,6 @@ - - - - - - - - - + diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/config-details.jelly b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/config-details.jelly index 4fdde93..8bb969f 100644 --- a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/config-details.jelly +++ b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/config-details.jelly @@ -1,12 +1,4 @@ - - - - - - - - - + diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/config.jelly b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/config.jelly new file mode 100644 index 0000000..4c76464 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/config.jelly @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildButton.html b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildButton.html similarity index 100% rename from src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildButton.html rename to src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildButton.html diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildNow.html b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildNow.html similarity index 100% rename from src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildNow.html rename to src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildNow.html diff --git a/src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildWithParams.html b/src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildWithParams.html similarity index 100% rename from src/main/resources/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty/help-alternateBuildWithParams.html rename to src/main/resources/org/jenkinsci/plugins/customizebuildnow/Labels/help-alternateBuildWithParams.html diff --git a/src/test/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNowTest.java b/src/test/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNowTest.java index 06982fe..1b59d5b 100644 --- a/src/test/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNowTest.java +++ b/src/test/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNowTest.java @@ -25,8 +25,9 @@ public class AlternateBuildNowTest { @Test public void testBuildNowChange() throws Exception { FreeStyleProject p = rule.createFreeStyleProject(); - p.addProperty(new BuildNowTextProperty("Deploy Now")); - //rule.interactiveBreak(); + Labels labels = new Labels(); + labels.setAlternateBuildNow("Deploy Now"); + p.addProperty(new BuildNowTextProperty(labels)); JenkinsRule.WebClient wc = rule.createWebClient(); HtmlPage html = wc.getPage(p); @@ -36,7 +37,7 @@ public void testBuildNowChange() throws Exception { BuildNowTextProperty textProperty = p2.getProperty(BuildNowTextProperty.class); assertNotNull(textProperty); - assertEquals(textProperty.getAlternateBuildNow(), "Deploy Now"); + assertEquals(textProperty.getLabels().getAlternateBuildNow(), "Deploy Now"); } @Test @@ -45,11 +46,11 @@ public void testBuildWithParamsChange() throws Exception { List parameterDefinitions = new ArrayList<>(); parameterDefinitions.add(new StringParameterDefinition("test", "")); ParametersDefinitionProperty paramsProp = new ParametersDefinitionProperty(parameterDefinitions); - BuildNowTextProperty prop = new BuildNowTextProperty("Run Now"); - prop.setAlternateBuildWithParams("Run with Parameters"); - p.addProperty(prop); + Labels labels = new Labels(); + labels.setAlternateBuildNow("Run Now"); + labels.setAlternateBuildWithParams("Run with Parameters"); + p.addProperty(new BuildNowTextProperty(labels)); p.addProperty(paramsProp); - //rule.interactiveBreak(); JenkinsRule.WebClient wc = rule.createWebClient(); HtmlPage html = wc.getPage(p); @@ -59,13 +60,12 @@ public void testBuildWithParamsChange() throws Exception { BuildNowTextProperty textProperty = p2.getProperty(BuildNowTextProperty.class); assertNotNull(textProperty); - assertEquals(textProperty.getAlternateBuildWithParams(), "Run with Parameters"); + assertEquals(textProperty.getLabels().getAlternateBuildWithParams(), "Run with Parameters"); } @Test public void testBuildNowUnChanged() throws Exception { FreeStyleProject p = rule.createFreeStyleProject(); - //rule.interactiveBreak(); JenkinsRule.WebClient wc = rule.createWebClient(); HtmlPage html = wc.getPage(p); @@ -80,7 +80,9 @@ public void testBuildNowUnChanged() throws Exception { public void workflow() throws Exception { WorkflowJob p = rule.jenkins.createProject(WorkflowJob.class, "p"); assertEquals(Messages.ParameterizedJobMixIn_build_now(), p.getBuildNowText()); - p.addProperty(new BuildNowTextProperty("Deploy Now")); + Labels labels = new Labels(); + labels.setAlternateBuildNow("Deploy Now"); + p.addProperty(new BuildNowTextProperty(labels)); assertEquals("Deploy Now", p.getBuildNowText()); JenkinsRule.WebClient wc = rule.createWebClient(); HtmlPage html = wc.getPage(p);