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

Support getting Git Parameter choices via extra call #349

Merged
merged 3 commits into from
Jan 6, 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
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 @@ -468,6 +468,19 @@ public String getServerUrl(String serverData) {
return getServerUrl(parseJson(serverData));
}

@Override
public List<String> getFillValueItems(String fillValueItemsData) {
checkJsonDataAndThrowExceptionIfNecessary(fillValueItemsData);
JsonObject fillValueJson = parseJson(fillValueItemsData);
JsonArray fillValueArray = (JsonArray) fillValueJson.get("values");
List<String> values = new ArrayList<>();
for (Object obj : fillValueArray) {
JsonObject valueJson = (JsonObject) obj;
values.add(valueJson.getString(createJsonKey("value")));
}
return values;
}

@NotNull
private String getServerUrl(JsonObject jsonObject) {
final Optional<View> primaryView = Optional.ofNullable((JsonObject) jsonObject.get(PRIMARY_VIEW)).map(this::getView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ public interface JenkinsParser {

@NotNull
String getServerUrl(String serverData);

List<String> getFillValueItems(String fillValueItemsData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public RequestManager(Project project) {
}

public static RequestManager getInstance(Project project) {
return project.getService(RequestManager.class);
return Optional.ofNullable(project.getService(RequestManager.class))
.orElseGet(() -> new RequestManager(project));
}

private static boolean canContainNestedJobs(@NotNull Job job) {
Expand Down Expand Up @@ -486,6 +487,21 @@ public List<Computer> loadComputer(JenkinsAppSettings settings) {
return jsonParser.createComputers(securityClient.execute(url));
}

@NotNull
@Override
public List<String> getGitParameterChoices(Job job, JobParameter jobParameter) {
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));
}

@NotNull
private HttpClientBuilder createHttpClientBuilder(String serverUrl, JenkinsSettings jenkinsSettings) {
final CredentialsProvider provider = new BasicCredentialsProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public interface RequestManagerInterface {

@NotNull
List<Computer> loadComputer(JenkinsAppSettings settings);

List<String> getGitParameterChoices(Job job, JobParameter jobParameter);
}
10 changes: 10 additions & 0 deletions src/main/java/org/codinjutsu/tools/jenkins/logic/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class UrlBuilder {
private static final String NESTED_JOBS_INFO = URL + "name,displayName,fullDisplayName,jobs[" + BASIC_JOB_INFO + "]";
private static final String COMPUTER = "/computer";
private static final String COMPUTER_INFO = "computer[displayName,description,offline,assignedLabels[name]]";
private static final String FILL_VALUE_ITEMS = "descriptorByName/%s/fillValueItems?param=%s";

public static UrlBuilder getInstance(Project project) {
return Optional.ofNullable(project.getService(UrlBuilder.class))
Expand Down Expand Up @@ -212,4 +213,13 @@ public String removeTrailingSlash(@NotNull String url) {
}
return withoutTrailingSlash;
}

public URL createFillValueItemsUrl(String jobUrl, String className, String param) {
try {
return new URL(jobUrl + URIUtil.encodePathQuery(String.format(FILL_VALUE_ITEMS, className, param)));
} catch (Exception ex) {
handleException(ex);
}
return null;
}
}
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,6 +31,7 @@
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.JobParameterComponent;
Expand Down Expand Up @@ -117,7 +118,9 @@ private void addParameterInputs() {
for (JobParameter jobParameter : parameters) {
final JobParameterRenderer jobParameterRenderer = JobParameterRenderer.findRenderer(jobParameter)
.orElseGet(ErrorRenderer::new);
final JobParameterComponent<?> 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();
jobParameterComponent.getViewElement().setName(jobParameter.getName());
Expand Down Expand Up @@ -187,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