Skip to content

Commit

Permalink
add ProjectJob to JobRenderer to allow retrieve project Services
Browse files Browse the repository at this point in the history
add Support for ListGitBranchesParameter
  • Loading branch information
MCMicS committed Jan 5, 2022
1 parent 83260d7 commit 6bd15c1
Show file tree
Hide file tree
Showing 32 changed files with 210 additions and 123 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,18 @@ public List<Computer> loadComputer(JenkinsAppSettings settings) {
return jsonParser.createComputers(securityClient.execute(url));
}

@NotNull
@Override
public List<String> 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<String> getFillValueItems(Job job, String parameterClassName, String parameterName) {
final URL url = urlBuilder.createFillValueItemsUrl(job.getUrl(), parameterClassName, parameterName);
return jsonParser.getFillValueItems(securityClient.execute(url));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/codinjutsu/tools/jenkins/model/ProjectJob.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -201,7 +190,7 @@ public class ErrorRenderer implements JobParameterRenderer {

@NotNull
@Override
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) {
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) {
return JobParameterRenderers.createErrorLabel(jobParameter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -39,7 +41,7 @@ default Optional<JLabel> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +132,31 @@ public static JobParameterComponent<String> createComboBoxIfChoicesExists(@NotNu
return renderer.apply(jobParameter, defaultValue);
}

@NotNull
public static BiFunction<JobParameter, String, JobParameterComponent<String>> createGitParameterChoices(
@NotNull ProjectJob projectJob) {
return (jobParameter, defaultValue) -> createGitParameterChoices(projectJob, jobParameter, defaultValue);
}

@NotNull
public static JobParameterComponent<String> 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 <T> Function<T, String> asString(Function<T, Object> provider) {
return c -> String.valueOf(provider.apply(c));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -18,7 +18,7 @@ public class DateParameterRenderer implements JobParameterRenderer {

@NotNull
@Override
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) {
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) {
return JobParameterRenderers.createTextField(jobParameter, jobParameter.getDefaultValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +45,7 @@ public ExtendedChoiceParameterRenderer() {

@NotNull
@Override
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) {
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) {
return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel)
.apply(jobParameter, jobParameter.getDefaultValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +29,7 @@ public ExtensibleChoiceParameterRenderer() {

@NotNull
@Override
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) {
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) {
return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel)
.apply(jobParameter, jobParameter.getDefaultValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<JobParameterType, BiFunction<JobParameter, String, JobParameterComponent<String>>> converter = new HashMap<>();
private final Set<JobParameterType> 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<String> render(@NotNull JobParameter jobParameter) {
return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel)
.apply(jobParameter, jobParameter.getDefaultValue());
public JobParameterComponent<String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -14,7 +16,7 @@ public class HiddenParameterRenderer implements JobParameterRenderer {

@NotNull
@Override
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) {
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter, @Nullable ProjectJob projectJob) {
return new JobParameterComponent<>(jobParameter, new JLabel(), false);
}

Expand Down
Loading

0 comments on commit 6bd15c1

Please sign in to comment.