From 5327dd7b97dc4000441b2772d31e6fcec205aef1 Mon Sep 17 00:00:00 2001 From: mcmics Date: Wed, 14 Jul 2021 18:17:31 +0200 Subject: [PATCH] add Renderer for ExtensibleChoiceParameter --- CHANGELOG.md | 2 + build.gradle | 6 +- .../ExtensibleChoiceParameterRenderer.java | 39 ++++++++++ .../META-INF/JobParameterRenderer.xml | 1 + .../jenkins/logic/JenkinsJsonParserTest.java | 14 ++++ ...ExtensibleChoiceParameterRendererTest.java | 39 ++++++++++ .../JobWithExtensibleChoiceParameter.json | 72 +++++++++++++++++++ 7 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java create mode 100644 src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java create mode 100644 src/test/resources/org/codinjutsu/tools/jenkins/logic/JobWithExtensibleChoiceParameter.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a19ea99..d3ecd89b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - \#297 NPE on Startup - \#298 Configure Action for Double Click Build - Expand Job Node after Load Build for first time +- \#301 ExtensibleChoiceParameterDefinition support request +- \#159 "Unknown parameter" for "Extensible Choice" parameters ## [0.13.11] - Rename Plugin to 'Jenkins Control' to respect Jetbrains naming convention diff --git a/build.gradle b/build.gradle index d5e88191..f1c8220c 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ def jenkinsPluginVersion = '0.13.12' def javaVersion = 11 group 'org.codinjutsu' -version "${jenkinsPluginVersion}-2021.2-eap4" +version "${jenkinsPluginVersion}-2021.2-eap5" apply plugin: 'org.jetbrains.intellij' apply plugin: 'org.jetbrains.changelog' apply plugin: 'java' @@ -23,11 +23,13 @@ apply plugin: "io.freefair.lombok" intellij { pluginName.set(jenkinsPluginName) version.set('LATEST-EAP-SNAPSHOT') + //version.set('2021.1') updateSinceUntilBuild.set(false) downloadSources.set(true) type.set('IC') - //type.set('RD') //Rider plugins.set(['com.intellij.java']) + //type.set('PC') + //type.set('RD') //Rider patchPluginXml { version.set("${project.version}") diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java new file mode 100644 index 00000000..5816bd02 --- /dev/null +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java @@ -0,0 +1,39 @@ +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.view.extension.JobParameterRenderer; +import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderers; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; + +public class ExtensibleChoiceParameterRenderer implements JobParameterRenderer { + + @NonNls + private static final String TYPE_CLASS = "jp.ikedam.jenkins.plugins.extensible_choice_parameter.ExtensibleChoiceParameterDefinition"; + + static final JobParameterType TYPE = new JobParameterType("ExtensibleChoiceParameterDefinition", TYPE_CLASS); + + private final Map>> converter = + new HashMap<>(); + + public ExtensibleChoiceParameterRenderer() { + converter.put(TYPE, JobParameterRenderers::createComboBoxIfChoicesExists); + } + + @NotNull + @Override + public JobParameterComponent render(@NotNull JobParameter jobParameter) { + 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/resources/META-INF/JobParameterRenderer.xml b/src/main/resources/META-INF/JobParameterRenderer.xml index 814b5890..848269be 100644 --- a/src/main/resources/META-INF/JobParameterRenderer.xml +++ b/src/main/resources/META-INF/JobParameterRenderer.xml @@ -14,5 +14,6 @@ + diff --git a/src/test/java/org/codinjutsu/tools/jenkins/logic/JenkinsJsonParserTest.java b/src/test/java/org/codinjutsu/tools/jenkins/logic/JenkinsJsonParserTest.java index 278219cf..c7010359 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/logic/JenkinsJsonParserTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/logic/JenkinsJsonParserTest.java @@ -370,6 +370,20 @@ public void testLoadJobWithNullAction() throws Exception { buildUrl)); } + @Test + public void testLoadJobExtensibleChoiceParameter() throws Exception { + Job actualJob = jsonParser.createJob(IOUtils.toString(getClass().getResourceAsStream( + "JobWithExtensibleChoiceParameter.json"))); + assertThat(actualJob).isNotNull(); + assertThat(actualJob.getParameters()).hasSize(2); + final JobParameter firstParameter = actualJob.getParameters().get(0); + final JobParameter secondParameter = actualJob.getParameters().get(1); + assertThat(firstParameter.getName()).isEqualTo("ExtensibleChoice"); + assertThat(firstParameter.getDefaultValue()).isEqualTo("Test"); + assertThat(secondParameter.getName()).isEqualTo("SimpleChoice"); + assertThat(secondParameter.getDefaultValue()).isEqualTo("Hello"); + } + @Before public void setUp() { MockitoAnnotations.initMocks(this); diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java new file mode 100644 index 00000000..2d8a2ccc --- /dev/null +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java @@ -0,0 +1,39 @@ +package org.codinjutsu.tools.jenkins.view.parameter; + +import com.intellij.openapi.ui.ComboBox; +import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; +import org.codinjutsu.tools.jenkins.model.JobParameter; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.*; + +public class ExtensibleChoiceParameterRendererTest implements JobParameterTest { + + private final ExtensibleChoiceParameterRenderer jobParameterRenderer = new ExtensibleChoiceParameterRenderer(); + + @Test + public void renderAsCombobox() { + JobParameter jobParameter = createJobParameterChoices(ExtensibleChoiceParameterRenderer.TYPE, + "Value1", "Selected", "Value3"); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); + assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); + + JobParameter jobParameterWithDefault = createJobParameterChoices(ExtensibleChoiceParameterRenderer.TYPE, + "Value1", "Selected", "Value3").toBuilder().defaultValue("Selected").build(); + jobParameterComponent = jobParameterRenderer.render(jobParameterWithDefault); + assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); + assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameterWithDefault); + final ComboBox comboBox = (ComboBox) jobParameterComponent.getViewElement(); + assertThat(comboBox.getSelectedItem()).isEqualTo("Selected"); + } + + @Test + public void isForJobParameter() { + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(ExtensibleChoiceParameterRenderer.TYPE))) + .isTrue(); + assertThat(jobParameterRenderer.isForJobParameter(createJobParameter( + BuildInJobParameter.ChoiceParameterDefinition))).isFalse(); + } + +} diff --git a/src/test/resources/org/codinjutsu/tools/jenkins/logic/JobWithExtensibleChoiceParameter.json b/src/test/resources/org/codinjutsu/tools/jenkins/logic/JobWithExtensibleChoiceParameter.json new file mode 100644 index 00000000..0b2e0f9d --- /dev/null +++ b/src/test/resources/org/codinjutsu/tools/jenkins/logic/JobWithExtensibleChoiceParameter.json @@ -0,0 +1,72 @@ +{ + "_class": "hudson.model.FreeStyleProject", + "description": "", + "displayName": "ExtensibleChoiceParameter", + "displayNameOrNull": null, + "fullDisplayName": "ExtensibleChoiceParameter", + "fullName": "ExtensibleChoiceParameter", + "name": "ExtensibleChoiceParameter", + "url": "https://localhost:8443/view/Parameter%20Builds/job/ExtensibleChoiceParameter/", + "buildable": true, + "builds": [], + "color": "notbuilt", + "firstBuild": null, + "healthReport": [], + "inQueue": false, + "keepDependencies": false, + "lastBuild": null, + "lastCompletedBuild": null, + "lastFailedBuild": null, + "lastStableBuild": null, + "lastSuccessfulBuild": null, + "lastUnstableBuild": null, + "lastUnsuccessfulBuild": null, + "nextBuildNumber": 1, + "property": [ + { + "_class": "hudson.model.ParametersDefinitionProperty", + "parameterDefinitions": [ + { + "_class": "jp.ikedam.jenkins.plugins.extensible_choice_parameter.ExtensibleChoiceParameterDefinition", + "defaultParameterValue": { + "_class": "hudson.model.StringParameterValue", + "name": "ExtensibleChoice", + "value": "Test" + }, + "description": null, + "name": "ExtensibleChoice", + "type": "ExtensibleChoiceParameterDefinition", + "choices": [ + "Test", + "Foo", + "Bar" + ] + }, + { + "_class": "hudson.model.ChoiceParameterDefinition", + "defaultParameterValue": { + "_class": "hudson.model.StringParameterValue", + "name": "SimpleChoice", + "value": "Hello" + }, + "description": null, + "name": "SimpleChoice", + "type": "ChoiceParameterDefinition", + "choices": [ + "Hello", + "World" + ] + } + ] + } + ], + "queueItem": null, + "concurrentBuild": false, + "disabled": false, + "downstreamProjects": [], + "labelExpression": null, + "scm": { + "_class": "hudson.scm.NullSCM" + }, + "upstreamProjects": [] +}