Skip to content

Commit

Permalink
Add default value
Browse files Browse the repository at this point in the history
  • Loading branch information
aendter committed Dec 30, 2023
1 parent 44af663 commit a3adfb7
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 51 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ For more information see the [homepage].

# Change Log

##### Version 0.0.9 (December 30, 2023)

- Added **includePathInValue** option by [allancth](https://github.com/jenkinsci/filesystem-list-parameter-plugin/commits?author=allancth).
- Added **default value**. Pre-select element if it is in the objects list.
- Security issue:
[JENKINS-69980 Plugin Homepage still shows security vulnerability warning for version 0.0.8](https://issues.jenkins.io/browse/JENKINS-69980)
- Update versions, formatting a.s.o.

##### Version 0.0.8 (July 06, 2022)

- Security issue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ private FormValidation checkRegex(String regex) {
private String regexIncludePattern;
private String regexExcludePattern;
private String value;
private String defaultValue;
private boolean includePathInValue;

/**
* @param name
* @param description
*/
@DataBoundConstructor
public FileSystemListParameterDefinition(String name, String description, String nodeName, String path, String selectedType,
public FileSystemListParameterDefinition(String name, String description, String nodeName, String path, String defaultValue, String selectedType,
String formSelectType, String regexIncludePattern, String regexExcludePattern, boolean sortByLastModified,
boolean sortReverseOrder, boolean includePathInValue) {

Expand All @@ -168,6 +169,7 @@ public FileSystemListParameterDefinition(String name, String description, String

this.nodeName = nodeName;
this.path = Util.fixNull(path);
this.defaultValue = defaultValue;
this.selectedType = selectedType;
this.formSelectType = formSelectType;
this.selectedEnumType = FsObjectTypes.valueOf(selectedType);
Expand Down Expand Up @@ -208,28 +210,30 @@ public ParameterValue createValue(StaplerRequest request, JSONObject jO) {

@Override
public ParameterValue getDefaultParameterValue() {
String defaultValue = "";
String localDefaultValue = "";

try {
defaultValue = getEffectiveDefaultValue();
localDefaultValue = getEffectiveDefaultValue();
} catch (Exception e) {
LOGGER.warning(
String.format(Messages.FileSystemListParameterDefinition_SymlinkDetectionError(), defaultValue));
String.format(Messages.FileSystemListParameterDefinition_SymlinkDetectionError(), localDefaultValue));

Check warning on line 219 in src/main/java/alex/jenkins/plugins/FileSystemListParameterDefinition.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 219 is not covered by tests
}
if (!StringUtils.isBlank(defaultValue)) {
if (!StringUtils.isBlank(localDefaultValue)) {

Check warning on line 221 in src/main/java/alex/jenkins/plugins/FileSystemListParameterDefinition.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 221 is only partially covered, one branch is missing
return new FileSystemListParameterValue(
getName(),
this.includePathInValue ? new File(this.path, defaultValue).getPath() : defaultValue
this.includePathInValue ? new File(this.path, localDefaultValue).getPath() : localDefaultValue

Check warning on line 224 in src/main/java/alex/jenkins/plugins/FileSystemListParameterDefinition.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 224 is only partially covered, one branch is missing
);
}
return super.getDefaultParameterValue();
}

private String getEffectiveDefaultValue() throws Exception {

List<String> defaultList = getFsObjectsList();
String defaultValue = defaultList.get(0);
return defaultValue;
if (defaultList.contains(getDefaultValue())) {
return getDefaultValue();
} else {
return defaultList.get(0);
}
}

public List<String> getFsObjectsList() throws Exception {
Expand Down Expand Up @@ -515,10 +519,6 @@ public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getRegexIncludePattern() {
return regexIncludePattern;
}
Expand All @@ -531,4 +531,8 @@ public String getNodeName() {
return nodeName;
}

public String getDefaultValue() {
return defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
</j:choose>
</f:entry>

<f:entry title="Default Value (Only one value supported, yet.)" field="defaultValue">
<f:textbox default="" />
</f:entry>

<f:entry title="Include Pattern" field="regexIncludePattern">
<f:textbox default="" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Only one value can be pre-selected as default when the value is contained in object list. (Multi select default is not supported for now!)
</div>
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<f:entry title="${it.name}" description="${it.description}" >
<div name="parameter" description="${it.description}" >
<j:choose>
<j:when test="${it.formSelectType eq 'SINGLE_SELECT'}">
<input type="hidden" name="name" value="${it.name}"/>
<select name="value">
<j:forEach var="fsObj" items="${it.fsObjectsList}">
<f:option value="${fsObj}">${fsObj}</f:option>
</j:forEach>
</select>
</j:when>
<j:when test="${it.formSelectType eq 'MULTI_SELECT'}">
<input type="hidden" name="name" value="${it.name}"/>
<select name="value" size="10" multiple="multiple" width="200px">
<j:forEach var="fsObj" items="${it.fsObjectsList}">
<f:option value="${fsObj}">${fsObj}</f:option>
</j:forEach>
</select>
</j:when>
</j:choose>
</div>
</f:entry>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler"
xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson"
xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<f:entry title="${it.name}" description="${it.description}">
<div name="parameter" description="${it.description}">
<j:choose>
<j:when test="${it.formSelectType eq 'SINGLE_SELECT'}">
<input type="hidden" name="name" value="${it.name}" />
<select name="value">
<j:forEach var="fsObj" items="${it.fsObjectsList}">
<f:option value="${fsObj}"
selected="${fsObj.equals(it.defaultValue)}">
${fsObj}
</f:option>
</j:forEach>
</select>
</j:when>
<j:when test="${it.formSelectType eq 'MULTI_SELECT'}">
<input type="hidden" name="name" value="${it.name}" />
<select name="value" size="10" multiple="multiple"
width="200px">
<j:forEach var="fsObj" items="${it.fsObjectsList}">
<f:option value="${fsObj}"
selected="${fsObj.equals(it.defaultValue)}">
${fsObj}
</f:option>
</j:forEach>
</select>
</j:when>
</j:choose>
</div>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
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"/>
<f:entry name="value" title="${h.escape(it.name)}" description="${it.formattedDescription}">
${it.value}
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler"
xmlns:d="jelly:define" 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" />
<f:entry name="value" title="${h.escape(it.name)}"
description="${it.formattedDescription}">
${it.value}
</f:entry>
</j:jelly>
49 changes: 47 additions & 2 deletions src/test/java/alex/jenkins/plugins/ChangeSequenceTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package alex.jenkins.plugins;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.TreeMap;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import alex.jenkins.plugins.FileSystemListParameterValue;

public class ChangeSequenceTest {

String path;

@Before
public void setup(){
URL resource = getClass().getResource("/1");
Assert.assertNotNull("Test test directory missing", resource);
path = resource.getPath();
}

@Test
public void testSorting() {
Expand Down Expand Up @@ -36,7 +49,7 @@ public void testReverseOrder() {
boolean sortByLastModified = true;
boolean sortReverseOrder = true;
boolean includePathInValue = false;
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", "path", "FILE","SINGLE_SELECT", "", "", sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", "path", "", "FILE","SINGLE_SELECT", "", "", sortByLastModified, sortReverseOrder, includePathInValue);

TreeMap<String, Long> map = new TreeMap<>();
String test1 = "test1";
Expand All @@ -60,7 +73,7 @@ public void testAlphabeticOrder() {
boolean sortByLastModified = false;
boolean sortReverseOrder = false;
boolean includePathInValue = false;
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", "path", "FILE","SINGLE_SELECT", "", "", sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", "path", "", "FILE","SINGLE_SELECT", "", "", sortByLastModified, sortReverseOrder, includePathInValue);

TreeMap<String, Long> map = new TreeMap<>();
String test1 = "test1";
Expand All @@ -83,5 +96,37 @@ public void testAlphabeticOrder() {

}

@Test
public void testGetExistingDefaultValue() throws Exception {

boolean sortByLastModified = false;
boolean sortReverseOrder = false;
boolean includePathInValue = false;
String includePattern = "";
String excludePattern = "";
String definition_default = "test2.txt";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, definition_default, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

String result_default = (String) pd.getDefaultParameterValue().getValue();
Assert.assertEquals(definition_default, result_default);

}

@Test
public void testGetNonExistingDefaultValue() throws Exception {

boolean sortByLastModified = false;
boolean sortReverseOrder = false;
boolean includePathInValue = false;
String includePattern = "";
String excludePattern = "";
String definition_default = "test4.txt";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, definition_default, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

String result_default = (String) pd.getDefaultParameterValue().getValue();
Assert.assertNotEquals(definition_default, result_default);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FileSystemListParameterDefinitionTest {
@Issue("JENKINS-49649")
public void smokeRoundtrip() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
FileSystemListParameterDefinition d = new FileSystemListParameterDefinition("name", "description", "master", "path", "FILE","SINGLE_SELECT", "", "", true, false, false);
FileSystemListParameterDefinition d = new FileSystemListParameterDefinition("name", "description", "master", "path", "", "FILE","SINGLE_SELECT", "", "", true, false, false);
ParametersDefinitionProperty params = new ParametersDefinitionProperty(d);
p.addProperty(params);

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/alex/jenkins/plugins/RegexFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testRegexEmptyFilter() throws Exception {
boolean includePathInValue = false;
String includePattern = "";
String excludePattern = "";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "", "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

List<String> list = pd.getFsObjectsList();

Expand All @@ -42,7 +42,7 @@ public void testRegexFilterNotfound() throws Exception {
boolean includePathInValue = false;
String includePattern = "notFound";
String excludePattern = "";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "", "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

List<String> list = pd.getFsObjectsList();

Expand All @@ -59,7 +59,7 @@ public void testRegexIncludeFilter() throws Exception {
boolean includePathInValue = false;
String includePattern = "[\\w]*3[.]txt";
String excludePattern = "";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "", "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

List<String> list = pd.getFsObjectsList();

Expand All @@ -77,7 +77,7 @@ public void testRegexExcludeFilter() throws Exception {
boolean includePathInValue = false;
String includePattern = "";
String excludePattern = "[\\w]*3[.]txt";
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);
FileSystemListParameterDefinition pd = new FileSystemListParameterDefinition("name", "description", "master", path, "", "FILE","SINGLE_SELECT", includePattern, excludePattern, sortByLastModified, sortReverseOrder, includePathInValue);

List<String> list = pd.getFsObjectsList();

Expand Down

0 comments on commit a3adfb7

Please sign in to comment.