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 1 commit
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
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 @@ -486,6 +486,12 @@ public List<Computer> loadComputer(JenkinsAppSettings settings) {
return jsonParser.createComputers(securityClient.execute(url));
}

@Override
public List<String> getGitParameterChoices(Job job, JobParameter jobParameter) {
final URL url = urlBuilder.createFillValueItemsUrl(job.getUrl(), jobParameter.getJobParameterType().getClassName(), jobParameter.getName());
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 + 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 @@ -33,6 +33,7 @@
import org.codinjutsu.tools.jenkins.model.JobParameter;
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 @@ -117,7 +118,20 @@ private void addParameterInputs() {
for (JobParameter jobParameter : parameters) {
final JobParameterRenderer jobParameterRenderer = JobParameterRenderer.findRenderer(jobParameter)
.orElseGet(ErrorRenderer::new);
final JobParameterComponent<?> jobParameterComponent = jobParameterRenderer.render(jobParameter);
JobParameterComponent<?> jobParameterComponent;
if (jobParameter.getChoices().isEmpty() && jobParameterRenderer instanceof GitParameterRenderer) {
MCMicS marked this conversation as resolved.
Show resolved Hide resolved
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);
}

if (jobParameterComponent.isVisible()) {
rows.incrementAndGet();
jobParameterComponent.getViewElement().setName(jobParameter.getName());
Expand Down