Skip to content

Commit

Permalink
Add verfion filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Dec 19, 2024
1 parent 57a2180 commit f960d88
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<changelist>-SNAPSHOT</changelist>

<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.version>2.387.3</jenkins.version>
<jenkins.version>2.414.3</jenkins.version>
<gitHubRepo>dumasd/${project.artifactId}-plugin</gitHubRepo>

<spotless.check.skip>false</spotless.check.skip>
Expand All @@ -46,7 +46,7 @@
<dependency>
<!-- Pick up common dependencies for the selected LTS line: https://github.com/jenkinsci/bom#usage -->
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.387.x</artifactId>
<artifactId>bom-2.414.x</artifactId>
<version>2543.vfb_1a_5fb_9496d</version>
<type>pom</type>
<scope>import</scope>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
Expand All @@ -41,6 +42,8 @@
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.verb.POST;

/**
* @author Bruce.Wu
Expand Down Expand Up @@ -72,7 +75,7 @@ public class NexusArtifactChoicesParameterDefinition extends ParameterDefinition
/**
* 最大的版本号查找数量
*/
private int maxVersionCount = 50;
private int maxVersionCount = 100;

@DataBoundConstructor
public NexusArtifactChoicesParameterDefinition(@NonNull String name, String serverId, String repository) {
Expand Down Expand Up @@ -111,18 +114,18 @@ public List<String> getGroupIdArtifactIdList() {

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
log.log(Level.INFO, "Create value with jo. {0}", jo);
JSONObject versionMap = jo.getJSONObject("value");
JSONArray gaIds = jo.getJSONArray("groupIdArtifactIds");
List<String> value = new ArrayList<>(versionMap.size());
for (Object obj : versionMap.values()) {
value.add(obj.toString());
for (int i = 0; i < gaIds.size(); i++) {
String key = gaIds.getString(i).replace('.', '-').replace(':', '-');
value.add(versionMap.getString(key));
}
return new NexusArtifactChoicesParameterValue(getName(), value);
}

@Override
public ParameterValue createValue(StaplerRequest req) {
log.log(Level.INFO, "Create value without jo.");
try {
JSONObject jo = req.getSubmittedForm();
return createValue(req, jo);
Expand All @@ -149,11 +152,14 @@ public ListBoxModel doFillServerIdItems() {
return items;
}

@POST
@JavaScriptMethod(name = "fillVersionOptionsItems")
public ListBoxModel doFillVersionOptionsItems(
@QueryParameter("serverId") String serverId,
@QueryParameter("repository") String repository,
@QueryParameter("option") String option,
@QueryParameter("limits") int limits)
@QueryParameter("limits") int limits,
@QueryParameter("keyword") String keyword)
throws Exception {
ListBoxModel items = new ListBoxModel();
NexusRepoServerConfig nxRepoCfg =
Expand Down Expand Up @@ -203,6 +209,12 @@ public ListBoxModel doFillVersionOptionsItems(
}
versionSet.forEach(e -> items.add(e, e));
}

if (Utils.isNotEmpty(keyword)) {
// 筛选
items.removeIf(e -> Utils.isNotContains(e.value, keyword) || Utils.isNotContains(e.name, keyword));
}

return items;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/jenkins/plugins/nexus/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ public static boolean isMatch(Pattern pattern, String content) {
}
return pattern.matcher(content).matches();
}

public static boolean isNotContains(String text, String search) {
if (text == null || search == null) {
return true;
}
return !text.contains(search);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<j:set var="escapeEntryTitleAndDescription" value="false"/>

<div style="display: none;">
<input id="nexusArtifactChoiceBaseUrl" type="hidden" disabled="disabled"
value="${rootURL}/descriptorByName/io.jenkins.plugins.nexus.NexusArtifactChoicesParameterDefinition"/>
<input id="nexusArtifactChoiceServerId" type="hidden" disabled="disabled" value="${it.serverId}"/>
<input id="nexusArtifactChoiceRepository" type="hidden" disabled="disabled" value="${it.repository}"/>
<input id="nexusArtifactChoiceMaxVersionCount" type="hidden" disabled="disabled"
value="${it.getMaxVersionCount()}"/>
</div>

<div name="parameter">
<f:entry title="${h.escape(it.name)}" description="${it.formattedDescription}">
<input type="hidden" name="name" value="${h.escape(it.name)}"/>
Expand All @@ -18,7 +28,7 @@
</div>
</div>

<script type="text/javascript">
<!--<script type="text/javascript">
<![CDATA[
function updateVersionOptions(select) {
var limits = '${it.getMaxVersionCount()}'
Expand Down Expand Up @@ -71,5 +81,7 @@
})
}
]]>
</script>
</script>-->

<script type="text/javascript" src="${rootURL}/plugin/jenkins-nexus-plugin/js/nexus-artifact-choices-parameter.js"/>
</j:jelly>
112 changes: 112 additions & 0 deletions src/main/webapp/js/nexus-artifact-choices-parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
function objectToUrlFormEncoded(parameters) {
// https://stackoverflow.com/a/37562814/4951015
// Code could be simplified if support for HTMLUnit is dropped
// body: new URLSearchParams(parameters) is enough then, but it doesn't work in HTMLUnit currently
let formBody = [];
for (const property in parameters) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(parameters[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
return formBody.join("&");
}

function filterVersionOptions(e) {
if (e.key === 'Enter') {
const searchInput = e.currentTarget;
const BASE_URL = document.getElementById("nexusArtifactChoiceBaseUrl").value;
let serverId = document.getElementById("nexusArtifactChoiceServerId").value;
let repository = document.getElementById("nexusArtifactChoiceRepository").value;
let limits = document.getElementById("nexusArtifactChoiceMaxVersionCount").value;
const selectElement = searchInput.previousElementSibling;
const option = selectElement.getAttribute("group-artifact-id");
selectElement.disabled = true
fetch(BASE_URL + "/fillVersionOptionsItems", {
method: "post",
headers: crumb.wrap({
"Content-Type": "application/x-www-form-urlencoded"
}),
body: objectToUrlFormEncoded({
serverId: serverId,
repository: repository,
option: option,
limits: limits,
keyword: searchInput.value,
})
}).then(response => response.json())
.then(resp => {
selectElement.innerHTML = ''
resp.values.forEach(subOption => {
let optionElement = document.createElement('option')
optionElement.value = subOption.value
optionElement.textContent = subOption.name
selectElement.appendChild(optionElement)
});
})
.finally(() => selectElement.disabled = false);
e.preventDefault();
}
}

function updateVersionOptions(select) {
const BASE_URL = document.getElementById("nexusArtifactChoiceBaseUrl").value;
let serverId = document.getElementById("nexusArtifactChoiceServerId").value;
let repository = document.getElementById("nexusArtifactChoiceRepository").value;
let limits = document.getElementById("nexusArtifactChoiceMaxVersionCount").value;

let selectedOptions = Array.from(select.selectedOptions).map(e => e.value)
let container = document.getElementById('versionsContainer')
container.innerHTML = ''
select.disabled = true
let promises = []
for (let i = 0; i < selectedOptions.length; i++) {
const option = selectedOptions.at(i)
const selectName = option.replaceAll(".", "-").replaceAll(":", "-")
const promise = fetch(BASE_URL + "/fillVersionOptionsItems", {
method: "post",
headers: crumb.wrap({
"Content-Type": "application/x-www-form-urlencoded"
}),
body: objectToUrlFormEncoded({
serverId: serverId,
repository: repository,
option: option,
limits: limits
})
}).then(response => response.json())
.then(resp => {
if (resp.values.length > 0) {
let selectSearchDiv = document.createElement('div')
selectSearchDiv.style.marginBottom = '5px'
selectSearchDiv.style.padding = '1px'

let selectElement = document.createElement('select')
resp.values.forEach(subOption => {
let optionElement = document.createElement('option')
optionElement.value = subOption.value
optionElement.textContent = subOption.name
selectElement.appendChild(optionElement)
});
selectElement.name = selectName
selectElement.style.marginBottom = '7px'
selectElement.style.padding = '1px'
selectElement.setAttribute("group-artifact-id", option)

let searchInput = document.createElement('input')
searchInput.type = 'text'
searchInput.style.marginLeft = '5px'
searchInput.addEventListener("keydown", filterVersionOptions)

selectSearchDiv.appendChild(selectElement)
selectSearchDiv.appendChild(searchInput)

container.appendChild(selectSearchDiv)
}
})
promises.push(promise)
}

Promise.all(promises).finally(() => {
select.disabled = false
})
}

0 comments on commit f960d88

Please sign in to comment.