Skip to content

Commit

Permalink
change configuration (#8)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mawinter69 authored Dec 4, 2023
1 parent 2f2db10 commit e5db875
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 90 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<properties>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.426.1</jenkins.version>
<hpi.compatibleSinceVersion>17</hpi.compatibleSinceVersion>
</properties>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 44 in src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildLabelConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 44 is not covered by tests
}

public void setAlternateBuildButton(String alternateBuildButton) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -20,40 +18,37 @@ public class AlternateBuildNow extends AlternativeUiTextProvider {
@Override
public <T> String getText(Message<T> 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) {

Check warning on line 27 in src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 27 is only partially covered, one branch is missing
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) {

Check warning on line 38 in src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 38 is only partially covered, one branch is missing
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());

Check warning on line 50 in src/main/java/org/jenkinsci/plugins/customizebuildnow/AlternateBuildNow.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 43-50 are not covered by tests
}
return buttonText;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job<?, ?>> {
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;

Check warning on line 48 in src/main/java/org/jenkinsci/plugins/customizebuildnow/BuildNowTextProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 37-48 are not covered by tests
}

@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);
}
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/org/jenkinsci/plugins/customizebuildnow/Labels.java
Original file line number Diff line number Diff line change
@@ -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<Labels> {

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<Labels> {

@NonNull
@Override
public String getDisplayName() {
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="${%Alternative Labels for Build Links and Buttons}">
<f:entry title="${%New Label for Build Now}" field="alternateBuildNow">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build with Parameters}" field="alternateBuildWithParams">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build Button}" field="alternateBuildButton">
<f:textbox/>
</f:entry>
<f:property field="labels"/>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%New Label for Build Now}" field="alternateBuildNow">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build with Parameters}" field="alternateBuildWithParams">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build Button}" field="alternateBuildButton">
<f:textbox/>
</f:entry>
<f:property field="labels"/>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%New Label for Build Now}" field="alternateBuildNow">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build with Parameters}" field="alternateBuildWithParams">
<f:textbox/>
</f:entry>
<f:entry title="${%New Label for Build Button}" field="alternateBuildButton">
<f:textbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -45,11 +46,11 @@ public void testBuildWithParamsChange() throws Exception {
List<ParameterDefinition> 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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e5db875

Please sign in to comment.