From 6bd15c11852b3d1be330c685f94113024b8edf98 Mon Sep 17 00:00:00 2001 From: mcmics Date: Wed, 5 Jan 2022 21:40:38 +0100 Subject: [PATCH] add ProjectJob to JobRenderer to allow retrieve project Services add Support for ListGitBranchesParameter --- CHANGELOG.md | 3 ++ build.gradle | 2 +- .../tools/jenkins/logic/RequestManager.java | 11 ++++++- .../tools/jenkins/logic/UrlBuilder.java | 2 +- .../tools/jenkins/model/JobParameter.java | 1 - .../tools/jenkins/model/ProjectJob.java | 17 ++++++++++ .../tools/jenkins/view/BuildParamDialog.java | 19 +++-------- .../view/extension/JobParameterRenderer.java | 4 ++- .../view/extension/JobParameterRenderers.java | 28 ++++++++++++++++ .../BuiltInJobParameterRenderer.java | 4 ++- .../view/parameter/DateParameterRenderer.java | 6 ++-- .../ExtendedChoiceParameterRenderer.java | 4 ++- .../ExtensibleChoiceParameterRenderer.java | 4 ++- .../view/parameter/GitParameterRenderer.java | 33 +++++++++++-------- .../parameter/HiddenParameterRenderer.java | 4 ++- .../ListGitBranchesParameterRenderer.java | 29 +++++++++------- .../view/parameter/NodeParameterRenderer.java | 4 ++- .../parameter/PatchParameterRenderer.java | 4 ++- .../PersistentParameterRenderer.java | 4 ++- .../view/parameter/SeparatorRenderer.java | 4 ++- .../BuiltInJobParameterRendererTest.java | 23 ++++++++----- .../parameter/DateParameterRendererTest.java | 4 +-- .../ExtendedChoiceParameterRendererTest.java | 16 ++++----- ...ExtensibleChoiceParameterRendererTest.java | 4 +-- .../parameter/GitParameterRendererTest.java | 30 ++++++++--------- .../HiddenParameterRendererTest.java | 4 +-- .../view/parameter/JobParameterTest.java | 13 ++++++-- .../ListGitBranchesParameterRendererTest.java | 18 +++++----- .../parameter/NodeParameterRendererTest.java | 11 +++---- .../parameter/PatchParameterRendererTest.java | 8 ++--- .../PersistentParameterRendererTest.java | 10 +++--- .../view/parameter/SeparatorRendererTest.java | 5 +-- 32 files changed, 210 insertions(+), 123 deletions(-) create mode 100644 src/main/java/org/codinjutsu/tools/jenkins/model/ProjectJob.java diff --git a/CHANGELOG.md b/CHANGELOG.md index d15fa524..a9401abf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## [Unreleased] +- \#349 Extend support for Git Parameter. Thanks to @chrisxiao + ## [0.13.13] - \#309 Exception in plugin Jenkins Control - \#322 Nullpointer if Jenkins if not running diff --git a/build.gradle b/build.gradle index d4435250..f4e26588 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { id 'jacoco' } def jenkinsPluginName = 'jenkins-control-plugin' -def jenkinsPluginVersion = '0.13.13' +def jenkinsPluginVersion = '0.13.14' group 'org.codinjutsu' version "${jenkinsPluginVersion}-2021.2" diff --git a/src/main/java/org/codinjutsu/tools/jenkins/logic/RequestManager.java b/src/main/java/org/codinjutsu/tools/jenkins/logic/RequestManager.java index f17c85e9..b1649bda 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/logic/RequestManager.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/logic/RequestManager.java @@ -486,9 +486,18 @@ public List loadComputer(JenkinsAppSettings settings) { return jsonParser.createComputers(securityClient.execute(url)); } + @NotNull @Override public List getGitParameterChoices(Job job, JobParameter jobParameter) { - final URL url = urlBuilder.createFillValueItemsUrl(job.getUrl(), jobParameter.getJobParameterType().getClassName(), jobParameter.getName()); + return Optional.ofNullable(jobParameter.getJobParameterType()) + .map(JobParameterType::getClassName) + .map(jobClassName -> getFillValueItems(job, jobClassName, jobParameter.getName())) + .orElse(Collections.emptyList()); + } + + @NotNull + private List getFillValueItems(Job job, String parameterClassName, String parameterName) { + final URL url = urlBuilder.createFillValueItemsUrl(job.getUrl(), parameterClassName, parameterName); return jsonParser.getFillValueItems(securityClient.execute(url)); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/logic/UrlBuilder.java b/src/main/java/org/codinjutsu/tools/jenkins/logic/UrlBuilder.java index c2399464..36a186a9 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/logic/UrlBuilder.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/logic/UrlBuilder.java @@ -216,7 +216,7 @@ public String removeTrailingSlash(@NotNull String url) { public URL createFillValueItemsUrl(String jobUrl, String className, String param) { try { - return new URL(jobUrl + String.format(FILL_VALUE_ITEMS, className, param)); + return new URL(jobUrl + URIUtil.encodePathQuery(String.format(FILL_VALUE_ITEMS, className, param))); } catch (Exception ex) { handleException(ex); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameter.java b/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameter.java index a344d31a..fbb4f1e0 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameter.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/model/JobParameter.java @@ -16,7 +16,6 @@ package org.codinjutsu.tools.jenkins.model; -import com.intellij.openapi.vfs.VirtualFile; import lombok.Builder; import lombok.Singular; import lombok.Value; diff --git a/src/main/java/org/codinjutsu/tools/jenkins/model/ProjectJob.java b/src/main/java/org/codinjutsu/tools/jenkins/model/ProjectJob.java new file mode 100644 index 00000000..41a32aee --- /dev/null +++ b/src/main/java/org/codinjutsu/tools/jenkins/model/ProjectJob.java @@ -0,0 +1,17 @@ +package org.codinjutsu.tools.jenkins.model; + +import com.intellij.openapi.project.Project; +import lombok.Builder; +import lombok.Value; +import org.jetbrains.annotations.NotNull; + +@Value +@Builder(toBuilder = true) +public class ProjectJob { + + @NotNull + private Project project; + + @NotNull + private Job job; +} diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/BuildParamDialog.java b/src/main/java/org/codinjutsu/tools/jenkins/view/BuildParamDialog.java index fbfc9eca..c1da9aac 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/BuildParamDialog.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/BuildParamDialog.java @@ -31,9 +31,9 @@ import org.codinjutsu.tools.jenkins.logic.RequestManagerInterface; import org.codinjutsu.tools.jenkins.model.Job; import org.codinjutsu.tools.jenkins.model.JobParameter; +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.codinjutsu.tools.jenkins.view.parameter.GitParameterRenderer; import org.codinjutsu.tools.jenkins.view.parameter.JobParameterComponent; import org.codinjutsu.tools.jenkins.view.util.SpringUtilities; import org.jetbrains.annotations.NotNull; @@ -118,19 +118,8 @@ private void addParameterInputs() { for (JobParameter jobParameter : parameters) { final JobParameterRenderer jobParameterRenderer = JobParameterRenderer.findRenderer(jobParameter) .orElseGet(ErrorRenderer::new); - JobParameterComponent jobParameterComponent; - if (jobParameter.getChoices().isEmpty() && jobParameterRenderer instanceof GitParameterRenderer) { - JobParameter gitParameter = JobParameter.builder() - .name(jobParameter.getName()) - .description(jobParameter.getDescription()) - .jobParameterType(jobParameter.getJobParameterType()) - .defaultValue(jobParameter.getDefaultValue()) - .choices(requestManager.getGitParameterChoices(job, jobParameter)) - .build(); - jobParameterComponent = jobParameterRenderer.render(gitParameter); - } else { - jobParameterComponent = jobParameterRenderer.render(jobParameter); - } + final ProjectJob projectJob = ProjectJob.builder().project(project).job(job).build(); + final JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, projectJob);; if (jobParameterComponent.isVisible()) { rows.incrementAndGet(); @@ -201,7 +190,7 @@ public class ErrorRenderer implements JobParameterRenderer { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return JobParameterRenderers.createErrorLabel(jobParameter); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderer.java index d9726473..c1e75f60 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/extension/JobParameterRenderer.java @@ -3,8 +3,10 @@ import com.intellij.openapi.extensions.ExtensionPointName; import org.apache.commons.lang.StringUtils; import org.codinjutsu.tools.jenkins.model.JobParameter; +import org.codinjutsu.tools.jenkins.model.ProjectJob; import org.codinjutsu.tools.jenkins.view.parameter.JobParameterComponent; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; import javax.swing.*; @@ -39,7 +41,7 @@ default Optional createLabel(@NotNull JobParameter jobParameter) { @SuppressWarnings({"rawtypes", "java:S3740"}) @NotNull - JobParameterComponent render(@NotNull JobParameter jobParameter); + JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob); boolean isForJobParameter(@NotNull JobParameter jobParameter); 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 3d09b485..cc3d9a1c 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 @@ -11,8 +11,11 @@ import com.intellij.ui.components.JBTextField; import lombok.experimental.UtilityClass; import org.apache.commons.lang.StringUtils; +import org.codinjutsu.tools.jenkins.logic.RequestManager; +import org.codinjutsu.tools.jenkins.logic.RequestManagerInterface; 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.parameter.JobParameterComponent; import org.codinjutsu.tools.jenkins.view.parameter.PasswordComponent; import org.jetbrains.annotations.NotNull; @@ -129,6 +132,31 @@ public static JobParameterComponent createComboBoxIfChoicesExists(@NotNu return renderer.apply(jobParameter, defaultValue); } + @NotNull + public static BiFunction> createGitParameterChoices( + @NotNull ProjectJob projectJob) { + return (jobParameter, defaultValue) -> createGitParameterChoices(projectJob, jobParameter, defaultValue); + } + + @NotNull + public static JobParameterComponent createGitParameterChoices(@NotNull ProjectJob projectJob, + @NotNull JobParameter jobParameter, + String defaultValue) { + if (jobParameter.getChoices().isEmpty()) { + final RequestManagerInterface requestManager = RequestManager.getInstance(projectJob.getProject()); + JobParameter gitParameter = JobParameter.builder() + .name(jobParameter.getName()) + .description(jobParameter.getDescription()) + .jobParameterType(jobParameter.getJobParameterType()) + .defaultValue(jobParameter.getDefaultValue()) + .choices(requestManager.getGitParameterChoices(projectJob.getJob(), jobParameter)) + .build(); + return createComboBoxIfChoicesExists(gitParameter, defaultValue); + } else { + return createComboBoxIfChoicesExists(jobParameter, defaultValue); + } + } + @NotNull private static Function asString(Function provider) { return c -> String.valueOf(provider.apply(c)); diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRenderer.java index 2cd9cccc..c5f15646 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/BuiltInJobParameterRenderer.java @@ -3,9 +3,11 @@ import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; 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.NotNull; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; import javax.swing.*; @@ -29,7 +31,7 @@ public BuiltInJobParameterRenderer() { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { final JobParameterType jobParameterType = jobParameter.getJobParameterType(); final String defaultValue = jobParameter.getDefaultValue(); return converter.getOrDefault(jobParameterType, JobParameterRenderers::createErrorLabel).apply(jobParameter, defaultValue); diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRenderer.java index 3c83b2b1..275c25ba 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRenderer.java @@ -2,12 +2,12 @@ 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 javax.swing.*; +import org.jetbrains.annotations.Nullable; public class DateParameterRenderer implements JobParameterRenderer { @@ -18,7 +18,7 @@ public class DateParameterRenderer implements JobParameterRenderer { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return JobParameterRenderers.createTextField(jobParameter, jobParameter.getDefaultValue()); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRenderer.java index 71a17308..febce368 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRenderer.java @@ -2,10 +2,12 @@ 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 javax.swing.*; import java.util.HashMap; @@ -43,7 +45,7 @@ public ExtendedChoiceParameterRenderer() { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) .apply(jobParameter, jobParameter.getDefaultValue()); } 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 index 5816bd02..cf6eafb0 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java @@ -2,10 +2,12 @@ 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; @@ -27,7 +29,7 @@ public ExtensibleChoiceParameterRenderer() { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) .apply(jobParameter, jobParameter.getDefaultValue()); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRenderer.java index addad781..084d2692 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/GitParameterRenderer.java @@ -2,14 +2,15 @@ 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 java.util.HashSet; +import java.util.Set; public class GitParameterRenderer implements JobParameterRenderer { @@ -26,25 +27,31 @@ public class GitParameterRenderer implements JobParameterRenderer { static final JobParameterType PT_PULL_REQUEST = new JobParameterType("PT_PULL_REQUEST", TYPE_CLASS); - private final Map>> converter = new HashMap<>(); + private final Set validTypes = new HashSet<>(); public GitParameterRenderer() { - converter.put(PT_TAG, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_BRANCH, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_BRANCH_TAG, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_REVISION, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_PULL_REQUEST, JobParameterRenderers::createComboBoxIfChoicesExists); + validTypes.add(PT_TAG); + validTypes.add(PT_BRANCH); + validTypes.add(PT_BRANCH_TAG); + validTypes.add(PT_REVISION); + validTypes.add(PT_PULL_REQUEST); } @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { - return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) - .apply(jobParameter, jobParameter.getDefaultValue()); + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { + if (!validTypes.contains(jobParameter.getJobParameterType())) { + return JobParameterRenderers.createErrorLabel(jobParameter); + } + if (projectJob != null) { + return JobParameterRenderers.createGitParameterChoices(projectJob, jobParameter, jobParameter.getDefaultValue()); + } else { + return JobParameterRenderers.createComboBoxIfChoicesExists(jobParameter, jobParameter.getDefaultValue()); + } } @Override public boolean isForJobParameter(@NotNull JobParameter jobParameter) { - return converter.containsKey(jobParameter.getJobParameterType()); + return validTypes.contains(jobParameter.getJobParameterType()); } } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRenderer.java index 8542f295..8ca681e8 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRenderer.java @@ -2,8 +2,10 @@ 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.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -14,7 +16,7 @@ public class HiddenParameterRenderer implements JobParameterRenderer { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return new JobParameterComponent<>(jobParameter, new JLabel(), false); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRenderer.java index 420b600e..5daca7bd 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRenderer.java @@ -2,14 +2,15 @@ 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 java.util.HashSet; +import java.util.Set; public class ListGitBranchesParameterRenderer implements JobParameterRenderer { @@ -22,23 +23,29 @@ public class ListGitBranchesParameterRenderer implements JobParameterRenderer { static final JobParameterType PT_BRANCH_TAG = new JobParameterType("PT_BRANCH_TAG", TYPE_CLASS); - private final Map>> converter = new HashMap<>(); + private final Set validTypes = new HashSet<>(); public ListGitBranchesParameterRenderer() { - converter.put(PT_TAG, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_BRANCH, JobParameterRenderers::createComboBoxIfChoicesExists); - converter.put(PT_BRANCH_TAG, JobParameterRenderers::createComboBoxIfChoicesExists); + validTypes.add(PT_TAG); + validTypes.add(PT_BRANCH); + validTypes.add(PT_BRANCH_TAG); } @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { - return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) - .apply(jobParameter, jobParameter.getDefaultValue()); + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { + if (!validTypes.contains(jobParameter.getJobParameterType())) { + return JobParameterRenderers.createErrorLabel(jobParameter); + } + if (projectJob != null) { + return JobParameterRenderers.createGitParameterChoices(projectJob, jobParameter, jobParameter.getDefaultValue()); + } else { + return JobParameterRenderers.createComboBoxIfChoicesExists(jobParameter, jobParameter.getDefaultValue()); + } } @Override public boolean isForJobParameter(@NotNull JobParameter jobParameter) { - return converter.containsKey(jobParameter.getJobParameterType()); + return validTypes.contains(jobParameter.getJobParameterType()); } } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRenderer.java index fa7d328a..73fd77be 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRenderer.java @@ -2,9 +2,11 @@ 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.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -28,7 +30,7 @@ public NodeParameterRenderer() { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) .apply(jobParameter, jobParameter.getDefaultValue()); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRenderer.java index 0780d2f2..a9178309 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRenderer.java @@ -3,9 +3,11 @@ import com.intellij.openapi.vfs.VirtualFile; 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.NotNull; +import org.jetbrains.annotations.Nullable; public class PatchParameterRenderer implements JobParameterRenderer { @@ -14,7 +16,7 @@ public class PatchParameterRenderer implements JobParameterRenderer { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return JobParameterRenderers.createFileUpload(jobParameter, jobParameter.getDefaultValue()); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRenderer.java b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRenderer.java index 1860556a..2de151b2 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRenderer.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRenderer.java @@ -2,9 +2,11 @@ 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.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -35,7 +37,7 @@ public PersistentParameterRenderer() { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { final JobParameterType jobParameterType = jobParameter.getJobParameterType(); final String defaultValue = jobParameter.getDefaultValue(); return converter.getOrDefault(jobParameterType, JobParameterRenderers::createErrorLabel).apply(jobParameter, defaultValue); 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 cf73f5af..e6846939 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 @@ -2,8 +2,10 @@ 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.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; import javax.swing.*; @@ -22,7 +24,7 @@ public Optional createLabel(@NotNull JobParameter jobParameter) { @NotNull @Override - public JobParameterComponent render(@NotNull JobParameter jobParameter) { + public JobParameterComponent render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) { return new JobParameterComponent<>(jobParameter, new JSeparator()); } 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 d23a84cd..563a5de1 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 @@ -48,24 +48,31 @@ public void isForJobParameterForWrongBuiltInClass() { @Test public void render() { final JobParameter jobParameter = createJobParameter(BuildInJobParameter.ChoiceParameterDefinition); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.BooleanParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.BooleanParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JCheckBox.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.StringParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.StringParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); final PasswordComponent passwordComponent = PasswordComponent.create(); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.PasswordParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.PasswordParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(passwordComponent.asComponent().getClass()); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.CredentialsParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.CredentialsParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JobParameterRenderers.ErrorLabel.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.FileParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.FileParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(TextFieldWithBrowseButton.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.RunParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.RunParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JobParameterRenderers.ErrorLabel.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.TextParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.TextParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextArea.class); } } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRendererTest.java index c07c91f9..3672009e 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/DateParameterRendererTest.java @@ -15,7 +15,7 @@ public class DateParameterRendererTest implements JobParameterTest { @Test public void renderAsInputField() { JobParameter jobParameter = createJobParameter(DateParameterRenderer.DATE_PARAMETER); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); } @@ -24,7 +24,7 @@ public void renderAsInputField() { public void renderWithDefaultValue() { final String defaultValue = "2021-05-15"; JobParameter jobParameter = createJobParameter(DateParameterRenderer.DATE_PARAMETER, defaultValue); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); final JTextField textField = (JTextField) jobParameterComponent.getViewElement(); assertThat(textField.getText()).isEqualTo(defaultValue); diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRendererTest.java index 05594c05..80a1c3fb 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtendedChoiceParameterRendererTest.java @@ -17,17 +17,17 @@ public class ExtendedChoiceParameterRendererTest implements JobParameterTest { @Test public void renderAsInputField() { JobParameter jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_SINGLE_SELECT); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_MULTI_SELECT); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_TEXTBOX); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); } @@ -36,13 +36,13 @@ public void renderAsInputField() { public void renderAsComboboxIfApiValuesAreAvailable() { JobParameter jobParameter = createJobParameterChoices(ExtendedChoiceParameterRenderer.PT_SINGLE_SELECT, "Value1", "Selected", "Value3"); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(ExtendedChoiceParameterRenderer.PT_MULTI_SELECT, "Value1", "Selected", "Value3"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); } @@ -50,11 +50,11 @@ public void renderAsComboboxIfApiValuesAreAvailable() { @Test public void renderAsUnsupported() { JobParameter jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_CHECKBOX); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JobParameterRenderers.ErrorLabel.class); jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_RADIO); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JobParameterRenderers.ErrorLabel.class); } @@ -62,7 +62,7 @@ public void renderAsUnsupported() { public void renderAsHidden() { JobParameter jobParameter = createJobParameter(ExtendedChoiceParameterRenderer.PT_HIDDEN); JobParameterComponent jobParameterComponent = jobParameterComponent = jobParameterRenderer.render( - jobParameter); + jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); assertThat(jobParameterComponent.isVisible()).isFalse(); } 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 index 2d8a2ccc..849a4b5c 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java @@ -15,13 +15,13 @@ public class ExtensibleChoiceParameterRendererTest implements JobParameterTest { public void renderAsCombobox() { JobParameter jobParameter = createJobParameterChoices(ExtensibleChoiceParameterRenderer.TYPE, "Value1", "Selected", "Value3"); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); 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); + jobParameterComponent = jobParameterRenderer.render(jobParameterWithDefault, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameterWithDefault); final ComboBox comboBox = (ComboBox) jobParameterComponent.getViewElement(); 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 e57a667d..cb34dcb0 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 @@ -1,8 +1,6 @@ package org.codinjutsu.tools.jenkins.view.parameter; import com.intellij.openapi.ui.ComboBox; -import com.intellij.openapi.ui.TextFieldWithBrowseButton; -import com.intellij.ui.components.JBTextField; import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; import org.codinjutsu.tools.jenkins.model.JobParameter; import org.codinjutsu.tools.jenkins.model.JobParameterType; @@ -11,73 +9,71 @@ import javax.swing.*; import static org.assertj.core.api.Assertions.*; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.*; public class GitParameterRendererTest implements JobParameterTest { - private final GitParameterRenderer jobParameterRenderer = new GitParameterRenderer(); @Test public void renderAsInputField() { JobParameter jobParameter = createJobParameter(GitParameterRenderer.PT_TAG); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); // JBTextField ? assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(GitParameterRenderer.PT_BRANCH); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(GitParameterRenderer.PT_BRANCH_TAG); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(GitParameterRenderer.PT_REVISION); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(GitParameterRenderer.PT_PULL_REQUEST); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } @Test public void renderAsComboBox() { JobParameter jobParameter = createJobParameterChoices(GitParameterRenderer.PT_TAG, "master", "tag/v0.13.6"); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(GitParameterRenderer.PT_BRANCH, "master", "bug/225"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(GitParameterRenderer.PT_BRANCH_TAG, "master", "tag/v0.13.6"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(GitParameterRenderer.PT_REVISION, "abcf12345 2020-04-14 21:36 user sample Revision"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(GitParameterRenderer.PT_PULL_REQUEST, "master", "pr/226"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRendererTest.java index 9f7f5785..fcfd8a5b 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/HiddenParameterRendererTest.java @@ -18,11 +18,11 @@ public class HiddenParameterRendererTest implements JobParameterTest { @Test public void render() { final JobParameter jobParameter = createJobParameter(hiddenParameter); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); assertThat(jobParameterComponent.isVisible()).isFalse(); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); assertThat(jobParameterComponent.isVisible()).isFalse(); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/JobParameterTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/JobParameterTest.java index 1be1a32f..832d08bf 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/JobParameterTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/JobParameterTest.java @@ -1,13 +1,20 @@ package org.codinjutsu.tools.jenkins.view.parameter; +import com.intellij.openapi.project.Project; +import org.codinjutsu.tools.jenkins.model.Job; import org.codinjutsu.tools.jenkins.model.JobParameter; import org.codinjutsu.tools.jenkins.model.JobParameterType; +import org.codinjutsu.tools.jenkins.model.ProjectJob; import org.jetbrains.annotations.NotNull; import java.util.Arrays; +import static org.mockito.Mockito.mock; + public interface JobParameterTest { + ProjectJob PROJECT_JOB = ProjectJob.builder().project(mock(Project.class)).job(mock(Job.class)).build(); + @NotNull default JobParameter createJobParameter(JobParameterType jobParameterType) { return createJobParameter(jobParameterType, ""); @@ -33,7 +40,9 @@ default JobParameter createJobParameter(JobParameterType jobParameterType, Strin @NotNull static JobParameter.JobParameterBuilder createJobParameterBuilder(JobParameterType jobParameterType) { - return JobParameter.builder().jobParameterType(jobParameterType) - .name("Test").description("Test Parameter"); + return JobParameter.builder() + .jobParameterType(jobParameterType) + .name("Test") + .description("Test Parameter"); } } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRendererTest.java index a4103af4..b14b5393 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/ListGitBranchesParameterRendererTest.java @@ -16,42 +16,44 @@ public class ListGitBranchesParameterRendererTest implements JobParameterTest { @Test public void renderAsInputField() { JobParameter jobParameter = createJobParameter(ListGitBranchesParameterRenderer.PT_TAG); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); // JBTextField ? assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(ListGitBranchesParameterRenderer.PT_BRANCH); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameter(ListGitBranchesParameterRenderer.PT_BRANCH_TAG); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } @Test public void renderAsComboBox() { JobParameter jobParameter = createJobParameterChoices(ListGitBranchesParameterRenderer.PT_TAG, "master", "tag/v0.13.6"); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(ListGitBranchesParameterRenderer.PT_BRANCH, "master", "bug/225"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); jobParameter = createJobParameterChoices(ListGitBranchesParameterRenderer.PT_BRANCH_TAG, "master", "tag/v0.13.6"); - jobParameterComponent = jobParameterRenderer.render(jobParameter); + jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRendererTest.java index 64caf0d9..c708cfa6 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/NodeParameterRendererTest.java @@ -3,11 +3,9 @@ import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; import org.codinjutsu.tools.jenkins.model.JobParameter; import org.codinjutsu.tools.jenkins.model.JobParameterType; -import org.jetbrains.annotations.NotNull; import org.junit.Test; import javax.swing.*; -import java.util.Arrays; import static org.assertj.core.api.Assertions.*; import static org.codinjutsu.tools.jenkins.view.parameter.NodeParameterRenderer.NODE_PARAMETER; @@ -22,18 +20,19 @@ public class NodeParameterRendererTest implements JobParameterTest { @Test public void renderLabel() { final JobParameter jobParameter = createJobParameter(labelParameter); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } @Test public void renderNode() { final JobParameter jobParameter = createJobParameter(nodeParameter, "default", "Test", "default", "master"); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, null); final JComponent viewElement = jobParameterComponent.getViewElement(); assertThat(viewElement).isInstanceOf(JComboBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); @@ -42,7 +41,7 @@ public void renderNode() { assertThat(nodes.getSelectedItem()).isEqualTo("default"); assertThat(nodes.getItemCount()).isEqualTo(3); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), null); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRendererTest.java index a5615e6a..ad517064 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PatchParameterRendererTest.java @@ -6,11 +6,7 @@ import org.codinjutsu.tools.jenkins.model.JobParameterType; import org.junit.Test; -import javax.swing.*; - import static org.assertj.core.api.Assertions.*; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.*; public class PatchParameterRendererTest implements JobParameterTest { @@ -19,11 +15,11 @@ public class PatchParameterRendererTest implements JobParameterTest { @Test public void render() { final JobParameter jobParameter = createJobParameter(PatchParameterRenderer.TYPE); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(TextFieldWithBrowseButton.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(TextFieldWithBrowseButton.class); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRendererTest.java index 407f94f8..a5cb5c32 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/PersistentParameterRendererTest.java @@ -17,20 +17,20 @@ public class PersistentParameterRendererTest implements JobParameterTest { @Test public void render() { final JobParameter jobParameter = createJobParameter(PersistentParameterRenderer.BOOLEAN); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JCheckBox.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.STRING)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.STRING), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextField.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.TEXT)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.TEXT), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextArea.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.CHOICE)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(PersistentParameterRenderer.CHOICE), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JComboBox.class); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JobParameterRenderers.ErrorLabel.class); } diff --git a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRendererTest.java b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRendererTest.java index 37ee95da..e07302e2 100644 --- a/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRendererTest.java +++ b/src/test/java/org/codinjutsu/tools/jenkins/view/parameter/SeparatorRendererTest.java @@ -18,11 +18,12 @@ public class SeparatorRendererTest implements JobParameterTest { @Test public void render() { final JobParameter jobParameter = createJobParameter(parameterSeparatorDefinition); - JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter); + JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter, PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JSeparator.class); assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); - jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)); + jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition), + PROJECT_JOB); assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JSeparator.class); }