Skip to content

Commit 18f30f9

Browse files
authored
convert library cache exclude to substring search (#147)
1 parent d0aa333 commit 18f30f9

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryCachingConfiguration.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Arrays;
1515
import java.util.Collections;
1616
import java.util.List;
17+
import org.apache.commons.lang.StringUtils;
1718

1819
public final class LibraryCachingConfiguration extends AbstractDescribableImpl<LibraryCachingConfiguration> {
1920
private int refreshTimeMinutes;
@@ -53,7 +54,19 @@ private List<String> getExcludedVersions() {
5354
}
5455

5556
public Boolean isExcluded(String version) {
56-
return getExcludedVersions().contains(version);
57+
// exit early if the version passed in is null or empty
58+
if (StringUtils.isBlank(version)) {
59+
return false;
60+
}
61+
for (String it : getExcludedVersions()) {
62+
// confirm that the excluded versions aren't null or empty
63+
// and if the version contains the exclusion thus it can be
64+
// anywhere in the string.
65+
if (StringUtils.isNotBlank(it) && version.contains(it)){
66+
return true;
67+
}
68+
}
69+
return false;
5770
}
5871

5972
@Override public String toString() {
@@ -82,4 +95,4 @@ public FormValidation doClearCache(@QueryParameter String name) throws Interrupt
8295
}
8396

8497
}
85-
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
Space separated list of versions excluded from caching. Ex: "master release10 release9"
2+
Space separated list of versions to exclude from caching via substring search using .contains() method. Ex: "release/ master release10 release9".
33
</div>

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryCachingConfigurationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class LibraryCachingConfigurationTest {
3737
private LibraryCachingConfiguration nullVersionConfig;
3838
private LibraryCachingConfiguration oneVersionConfig;
3939
private LibraryCachingConfiguration multiVersionConfig;
40+
private LibraryCachingConfiguration substringVersionConfig;
4041

4142
private static int REFRESH_TIME_MINUTES = 23;
4243
private static int NO_REFRESH_TIME_MINUTES = 0;
@@ -48,18 +49,25 @@ public class LibraryCachingConfigurationTest {
4849
private static String MULTIPLE_EXCLUDED_VERSIONS_2 = "branch-2";
4950
private static String MULTIPLE_EXCLUDED_VERSIONS_3 = "branch-3";
5051

52+
private static String SUBSTRING_EXCLUDED_VERSIONS_1 = "feature/test-substring-exclude";
53+
private static String SUBSTRING_EXCLUDED_VERSIONS_2 = "test-other-substring-exclude";
54+
5155
private static String MULTIPLE_EXCLUDED_VERSIONS =
5256
MULTIPLE_EXCLUDED_VERSIONS_1 + " " +
5357
MULTIPLE_EXCLUDED_VERSIONS_2 + " " +
5458
MULTIPLE_EXCLUDED_VERSIONS_3;
5559

60+
private static String SUBSTRING_EXCLUDED_VERSIONS =
61+
"feature/ other-substring";
62+
5663
private static String NEVER_EXCLUDED_VERSION = "never-excluded-version";
5764

5865
@Before
5966
public void createCachingConfiguration() {
6067
nullVersionConfig = new LibraryCachingConfiguration(REFRESH_TIME_MINUTES, NULL_EXCLUDED_VERSION);
6168
oneVersionConfig = new LibraryCachingConfiguration(NO_REFRESH_TIME_MINUTES, ONE_EXCLUDED_VERSION);
6269
multiVersionConfig = new LibraryCachingConfiguration(REFRESH_TIME_MINUTES, MULTIPLE_EXCLUDED_VERSIONS);
70+
substringVersionConfig = new LibraryCachingConfiguration(REFRESH_TIME_MINUTES, SUBSTRING_EXCLUDED_VERSIONS);
6371
}
6472

6573
@Issue("JENKINS-66045") // NPE getting excluded versions
@@ -91,6 +99,7 @@ public void getExcludedVersionsStr() {
9199
assertThat(nullVersionConfig.getExcludedVersionsStr(), is(NULL_EXCLUDED_VERSION));
92100
assertThat(oneVersionConfig.getExcludedVersionsStr(), is(ONE_EXCLUDED_VERSION));
93101
assertThat(multiVersionConfig.getExcludedVersionsStr(), is(MULTIPLE_EXCLUDED_VERSIONS));
102+
assertThat(substringVersionConfig.getExcludedVersionsStr(), is(SUBSTRING_EXCLUDED_VERSIONS));
94103
}
95104

96105
@Test
@@ -104,17 +113,22 @@ public void isExcluded() {
104113
assertTrue(multiVersionConfig.isExcluded(MULTIPLE_EXCLUDED_VERSIONS_2));
105114
assertTrue(multiVersionConfig.isExcluded(MULTIPLE_EXCLUDED_VERSIONS_3));
106115

116+
assertTrue(substringVersionConfig.isExcluded(SUBSTRING_EXCLUDED_VERSIONS_1));
117+
assertTrue(substringVersionConfig.isExcluded(SUBSTRING_EXCLUDED_VERSIONS_2));
118+
107119
assertFalse(nullVersionConfig.isExcluded(NEVER_EXCLUDED_VERSION));
108120
assertFalse(oneVersionConfig.isExcluded(NEVER_EXCLUDED_VERSION));
109121
assertFalse(multiVersionConfig.isExcluded(NEVER_EXCLUDED_VERSION));
110122

111123
assertFalse(nullVersionConfig.isExcluded(""));
112124
assertFalse(oneVersionConfig.isExcluded(""));
113125
assertFalse(multiVersionConfig.isExcluded(""));
126+
assertFalse(substringVersionConfig.isExcluded(""));
114127

115128
assertFalse(nullVersionConfig.isExcluded(null));
116129
assertFalse(oneVersionConfig.isExcluded(null));
117130
assertFalse(multiVersionConfig.isExcluded(null));
131+
assertFalse(substringVersionConfig.isExcluded(null));
118132
}
119133

120134
}

0 commit comments

Comments
 (0)