From fa3298d2931a24227d25cc4e7237002d81a51191 Mon Sep 17 00:00:00 2001 From: mcmics Date: Tue, 12 Apr 2022 22:04:48 +0200 Subject: [PATCH] add Active Choices Parameter (only rendered as text input fields) --- CHANGELOG.md | 1 + .../jenkins/model/BuildInJobParameter.java | 2 +- .../tools/jenkins/model/JobParameterType.java | 23 +++++++-- .../view/extension/JobParameterRenderers.java | 13 ++++- .../ActiveChoicesParameterRenderer.java | 48 +++++++++++++++++++ .../view/parameter/SeparatorRenderer.java | 2 +- ...tsu.tools.jenkins-JobParameterRenderer.xml | 1 + .../jenkins/model/JobParameterTypeTest.java | 12 +++++ .../jenkins/view/BuildParamDialogTest.java | 9 ++-- .../ActiveChoicesParameterRendererTest.java | 43 +++++++++++++++++ .../BuiltInJobParameterRendererTest.java | 8 ++-- .../parameter/GitParameterRendererTest.java | 2 +- 12 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRenderer.java create mode 100644 src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRendererTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 266f3856..dd9ccdb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - \#349 Extend support for Git Parameter. Thanks to @chrisxiao - 2022.1 API Compatibility - Change icons for show log actions +- \#251 Enable ActiveChoicesParameter. Rendered as Textfield because missing API. ## [0.13.13] - \#309 Exception in plugin Jenkins Control diff --git a/src/main/java/org/codinjutsu/tools/jenkins/model/BuildInJobParameter.java b/src/main/java/org/codinjutsu/tools/jenkins/model/BuildInJobParameter.java index 21095351..c7d8e2b0 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/model/BuildInJobParameter.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/model/BuildInJobParameter.java @@ -18,7 +18,7 @@ public final class BuildInJobParameter { @NotNull private static JobParameterType create(@NotNull String name) { - return new JobParameterType(name, "hudson.model." + name); + return JobParameterType.createTypeForClassPrefix(name, "hudson.model."); } @Nonnull diff --git a/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameterType.java b/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameterType.java index 19a9bdc9..bd30c37e 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameterType.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameterType.java @@ -2,6 +2,7 @@ import lombok.Value; import org.apache.commons.lang.StringUtils; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -11,17 +12,31 @@ public class JobParameterType { @NotNull - private String name; + private String type; @Nullable private String className; @NotNull - public static JobParameterType getType(@NotNull String parameterName, @Nullable String parameterClass) { + public static JobParameterType getType(@NotNull String parameterType, @Nullable String parameterClass) { Optional jobParameter = Optional.empty(); if (StringUtils.isEmpty(parameterClass)) { jobParameter = BuildInJobParameter.getBuiltInJobParameter() - .filter(parameter -> parameter.getName().equals(parameterName)).findFirst(); + .filter(parameter -> parameter.getType().equals(parameterType)).findFirst(); } - return jobParameter.orElseGet(() -> new JobParameterType(parameterName, parameterClass)); + return jobParameter.orElseGet(() -> new JobParameterType(parameterType, parameterClass)); + } + + @NotNull + public static JobParameterType createTypeForClassPrefix(@NonNls @NotNull String type, + @NonNls @NotNull String classPrefix) { + final StringBuilder classPrefixWithTrailingDot = new StringBuilder(classPrefix); + if (classPrefixWithTrailingDot.length() > 0) { + if (classPrefixWithTrailingDot.charAt(classPrefixWithTrailingDot.length() - 1) != '.') { + classPrefixWithTrailingDot.append("."); + } + classPrefixWithTrailingDot.append(type); + } + + return getType(type, classPrefixWithTrailingDot.toString()); } } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderers.java b/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderers.java index d3cc9c4b..bc557f6b 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderers.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderers.java @@ -7,6 +7,7 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBTextArea; import com.intellij.ui.components.JBTextField; import lombok.experimental.UtilityClass; @@ -93,13 +94,23 @@ public static JobParameterComponent createComboBox(@NotNull JobParameter return new JobParameterComponent<>(jobParameter, comboBox, asString(JComboBox::getSelectedItem)); } + @SuppressWarnings("unused") + @NotNull + public static JobParameterComponent createLabel(@NotNull JobParameter jobParameter, String defaultValue) { + final JBLabel label = new JBLabel(); + if (StringUtils.isNotEmpty(defaultValue)) { + label.setText(defaultValue); + } + return new JobParameterComponent<>(jobParameter, label, JLabel::getText); + } + @NotNull public static JLabel createErrorLabel(@Nullable JobParameterType jobParameterType) { final String text; if (jobParameterType == null) { text = "Unknown parameter type"; } else { - text = jobParameterType.getName() + " is unsupported."; + text = jobParameterType.getType() + " is unsupported."; } return createErrorLabel(text); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRenderer.java new file mode 100644 index 00000000..e46d4f15 --- /dev/null +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRenderer.java @@ -0,0 +1,48 @@ +package org.codinjutsu.tools.jenkins.view.parameter; + +import org.codinjutsu.tools.jenkins.model.JobParameter; +import org.codinjutsu.tools.jenkins.model.JobParameterType; +import org.codinjutsu.tools.jenkins.model.ProjectJob; +import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderer; +import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderers; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; + +import static org.codinjutsu.tools.jenkins.model.JobParameterType.createTypeForClassPrefix; + +public class ActiveChoicesParameterRenderer implements JobParameterRenderer { + + @NonNls + private static final String TYPE_CLASS_PREFIX = "org.biouno.unochoice."; + + static final JobParameterType CHOICE_PARAMETER = createTypeForClassPrefix("ChoiceParameter", TYPE_CLASS_PREFIX); + + static final JobParameterType CASCADE_CHOICE_PARAMETER = createTypeForClassPrefix("CascadeChoiceParameter", TYPE_CLASS_PREFIX); + + static final JobParameterType DYNAMIC_REFERENCE_PARAMETER = createTypeForClassPrefix("DynamicReferenceParameter", TYPE_CLASS_PREFIX); + + private final Map>> converter = + new HashMap<>(); + + public ActiveChoicesParameterRenderer() { + converter.put(CHOICE_PARAMETER, JobParameterRenderers::createComboBoxIfChoicesExists); + converter.put(CASCADE_CHOICE_PARAMETER, JobParameterRenderers::createComboBoxIfChoicesExists); + converter.put(DYNAMIC_REFERENCE_PARAMETER, JobParameterRenderers::createLabel); + } + + @Override + public @NotNull JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { + return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) + .apply(jobParameter, jobParameter.getDefaultValue()); + } + + @Override + public boolean isForJobParameter(@NotNull JobParameter jobParameter) { + return converter.containsKey(jobParameter.getJobParameterType()); + } +} diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRenderer.java index e6846939..bd7ab077 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRenderer.java @@ -31,7 +31,7 @@ public JobParameterComponent render(@NotNull JobParameter jobParameter, @Override public boolean isForJobParameter(@NotNull JobParameter jobParameter) { return Optional.of(jobParameter).map(JobParameter::getJobParameterType) - .map(JobParameterType::getName) + .map(JobParameterType::getType) .filter("ParameterSeparatorDefinition"::equals).isPresent(); } } diff --git a/src/main/resources/META-INF/org.codinjutsu.tools.jenkins-JobParameterRenderer.xml b/src/main/resources/META-INF/org.codinjutsu.tools.jenkins-JobParameterRenderer.xml index 069205ab..368ae5f4 100644 --- a/src/main/resources/META-INF/org.codinjutsu.tools.jenkins-JobParameterRenderer.xml +++ b/src/main/resources/META-INF/org.codinjutsu.tools.jenkins-JobParameterRenderer.xml @@ -14,5 +14,6 @@ + diff --git a/src/test/java/org/codinjutsu/tools/jenkins/model/JobParameterTypeTest.java b/src/test/java/org/codinjutsu/tools/jenkins/model/JobParameterTypeTest.java index edea18cb..8e918f67 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/model/JobParameterTypeTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/model/JobParameterTypeTest.java @@ -35,4 +35,16 @@ public void getCustomType() { .isEqualTo(new JobParameterType("ParameterSeparatorDefinition", "jenkins.plugins.parameter_separator.ParameterSeparatorDefinition")); } + + @Test + public void createTypeForClassPrefix() { + assertThat(JobParameterType.createTypeForClassPrefix("Test", "org.codinjutsu.tools.jenkins.")) + .isEqualTo(new JobParameterType("Test", "org.codinjutsu.tools.jenkins.Test")); + assertThat(JobParameterType.createTypeForClassPrefix("Test", "org.codinjutsu.tools.jenkins")) + .isEqualTo(new JobParameterType("Test", "org.codinjutsu.tools.jenkins.Test")); + assertThat(JobParameterType.createTypeForClassPrefix("Test", EMPTY_CLASS)) + .isEqualTo(new JobParameterType("Test", EMPTY_CLASS)); + assertThat(JobParameterType.createTypeForClassPrefix("StringParameterDefinition", EMPTY_CLASS)) + .isEqualTo(BuildInJobParameter.StringParameterDefinition); + } } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/BuildParamDialogTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/BuildParamDialogTest.java index 32043140..0a7caaa3 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/BuildParamDialogTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/BuildParamDialogTest.java @@ -59,18 +59,17 @@ public class BuildParamDialogTest { new JobBuilder() .job("myJob", "blue", "http://dummyserver/jobs/myJob", false, true) .health("health-80plus", "0 tests en échec sur un total de 24 tests") - .parameter("integrationTest", BuildInJobParameter.BooleanParameterDefinition.getName(), - "true") - .parameter("environment", BuildInJobParameter.ChoiceParameterDefinition.getName(), "development", + .parameter("integrationTest", BuildInJobParameter.BooleanParameterDefinition.getType(), "true") + .parameter("environment", BuildInJobParameter.ChoiceParameterDefinition.getType(), "development", "development", "integration", "acceptance", "production") - .parameter("message", BuildInJobParameter.StringParameterDefinition.getName(), "") + .parameter("message", BuildInJobParameter.StringParameterDefinition.getType(), "") .get(); private static final Job JOB_WITH_UNSUPPORTED_PARAMS = new JobBuilder() .job("myJob", "blue", "http://dummyserver/jobs/myJob", false, true) .health("health-80plus", "0 tests en échec sur un total de 24 tests") - .parameter("run", BuildInJobParameter.RunParameterDefinition.getName(), "blah") + .parameter("run", BuildInJobParameter.RunParameterDefinition.getType(), "blah") .get(); @NonNls diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRendererTest.java new file mode 100644 index 00000000..17989132 --- /dev/null +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ActiveChoicesParameterRendererTest.java @@ -0,0 +1,43 @@ +package org.codinjutsu.tools.jenkins.view.parameter; + +import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; +import org.codinjutsu.tools.jenkins.model.JobParameter; +import org.junit.Test; + +import javax.swing.*; + +import static org.assertj.core.api.Assertions.*; + +public class ActiveChoicesParameterRendererTest implements JobParameterTest { + + private final ActiveChoicesParameterRenderer jobParameterRenderer = new ActiveChoicesParameterRenderer(); + + @Test + public void renderAsInputField() { + JobParameter jobParameter = createJobParameter(ActiveChoicesParameterRenderer.CHOICE_PARAMETER); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); + assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); + assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); + + jobParameter = createJobParameter(ActiveChoicesParameterRenderer.CASCADE_CHOICE_PARAMETER); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); + assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); + assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); + } + + @Test + public void renderAsLabel() { + JobParameter jobParameter = createJobParameter(ActiveChoicesParameterRenderer.DYNAMIC_REFERENCE_PARAMETER); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); + assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); + } + + @Test + public void isForJobParameter() { + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(ActiveChoicesParameterRenderer.CHOICE_PARAMETER))).isTrue(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(ActiveChoicesParameterRenderer.CASCADE_CHOICE_PARAMETER))).isTrue(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(ActiveChoicesParameterRenderer.DYNAMIC_REFERENCE_PARAMETER))).isTrue(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition))).isFalse(); + } + +} diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRendererTest.java index 563a5de1..5b293aca 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRendererTest.java @@ -35,14 +35,14 @@ public void isForJobParameter() { assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(BuildInJobParameter.TextParameterDefinition))) .isTrue(); - assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType(BuildInJobParameter.ChoiceParameterDefinition.getName(), "otherClass")))) - .isFalse(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType( + BuildInJobParameter.ChoiceParameterDefinition.getType(), "otherClass")))).isFalse(); } @Test public void isForJobParameterForWrongBuiltInClass() { - assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType(BuildInJobParameter.ChoiceParameterDefinition.getName(), "otherClass")))) - .isFalse(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType( + BuildInJobParameter.ChoiceParameterDefinition.getType(), "otherClass")))).isFalse(); } @Test diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRendererTest.java index 35b20785..b471cb37 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRendererTest.java @@ -150,7 +150,7 @@ public void isForJobParameter() { assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(GitParameterRenderer.PT_PULL_REQUEST))).isTrue(); assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(GitParameterRenderer.GIT_PARAMETER_DEFINITION))).isTrue(); assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType(// - GitParameterRenderer.PT_TAG.getName(), "net.uaznia.lukanus.hudson.plugins.gitparameter.invalid.GitParameterDefinition")))) + GitParameterRenderer.PT_TAG.getType(), "net.uaznia.lukanus.hudson.plugins.gitparameter.invalid.GitParameterDefinition")))) .isFalse(); assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition))) .isFalse();