Skip to content

Commit

Permalink
Support getting Git Parameter choices via extra call when opening Bui…
Browse files Browse the repository at this point in the history
…ldDialog
  • Loading branch information
chrisxiao committed Dec 31, 2021
1 parent 163d88d commit 83260d7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
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) {
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

0 comments on commit 83260d7

Please sign in to comment.