Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Active Choices Parameter (only rendered as text input fields) #370

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<JobParameterType> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,13 +94,23 @@ public static JobParameterComponent<String> createComboBox(@NotNull JobParameter
return new JobParameterComponent<>(jobParameter, comboBox, asString(JComboBox::getSelectedItem));
}

@SuppressWarnings("unused")
@NotNull
public static JobParameterComponent<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JobParameterType, BiFunction<JobParameter, String, JobParameterComponent<String>>> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public JobParameterComponent<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<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" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.ActiveChoicesParameterRenderer" />
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

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