Skip to content

Commit

Permalink
add Renderer for ExtensibleChoiceParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
MCMicS committed Jul 16, 2021
1 parent dfc2759 commit 5327dd7
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JobParameterType, BiFunction<JobParameter, String, JobParameterComponent<String>>> converter =
new HashMap<>();

public ExtensibleChoiceParameterRenderer() {
converter.put(TYPE, JobParameterRenderers::createComboBoxIfChoicesExists);
}

@NotNull
@Override
public JobParameterComponent<String> 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());
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/JobParameterRenderer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.ListGitBranchesParameterRenderer" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.ExtendedChoiceParameterRenderer" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.DateParameterRenderer" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.ExtensibleChoiceParameterRenderer" />
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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": []
}

0 comments on commit 5327dd7

Please sign in to comment.