Skip to content

Commit e5374aa

Browse files
author
KINAXIS\agharat
committed
Handling case for empty versions to include
1 parent a0d1dfe commit e5374aa

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import edu.umd.cs.findbugs.annotations.NonNull;
5353
import java.util.regex.Pattern;
5454
import org.apache.commons.io.IOUtils;
55+
import org.apache.commons.lang.StringUtils;
5556
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
5657
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
5758
import org.jenkinsci.plugins.workflow.cps.GlobalVariableSet;
@@ -210,7 +211,10 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
210211
shouldCache = false;
211212
}
212213

213-
if(shouldCache && cachingConfiguration.isIncluded(version)) {
214+
//If the included versions is blank/null, cache irrespective
215+
//else check if that version is included and then cache only that version
216+
217+
if((shouldCache && cachingConfiguration.isIncluded(version)) || (shouldCache && StringUtils.isBlank(cachingConfiguration.getIncludedVersionsStr()))) {
214218
retrieveLock.readLock().lockInterruptibly();
215219
try {
216220
CacheStatus cacheStatus = getCacheStatus(cachingConfiguration, versionCacheDir);
@@ -220,8 +224,8 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
220224
try {
221225
boolean retrieve = false;
222226
switch (getCacheStatus(cachingConfiguration, versionCacheDir)) {
223-
case VALID:
224-
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
227+
case VALID:
228+
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
225229
break;
226230
case DOES_NOT_EXIST:
227231
retrieve = true;
@@ -236,20 +240,20 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
236240
retrieve = true;
237241
break;
238242
}
239-
243+
240244
if (retrieve) {
241-
listener.getLogger().println("Caching library " + name + "@" + version);
245+
listener.getLogger().println("Caching library " + name + "@" + version);
242246
versionCacheDir.mkdirs();
243247
retriever.retrieve(name, version, changelog, versionCacheDir, run, listener);
244248
}
245249
retrieveLock.readLock().lock();
246250
} finally {
247-
retrieveLock.writeLock().unlock();
251+
retrieveLock.writeLock().unlock();
248252
}
249253
} else {
250-
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
254+
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
251255
}
252-
256+
253257
lastReadFile.touch(System.currentTimeMillis());
254258
versionCacheDir.withSuffix("-name.txt").write(name, "UTF-8");
255259
versionCacheDir.copyRecursiveTo(libDir);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public Boolean isRefreshEnabled() {
5757
public String getExcludedVersionsStr() {
5858
return excludedVersionsStr;
5959
}
60-
public String getIncludedVersionsStr() { return includedVersionsStr; }
60+
public String getIncludedVersionsStr() {
61+
if(StringUtils.isBlank(includedVersionsStr)){
62+
return null;
63+
}
64+
return includedVersionsStr;
65+
}
6166

6267

6368
private List<String> getExcludedVersions() {

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,6 @@ public void isIncluded() {
206206
assertTrue(substringVersionConfig.isIncluded(SUBSTRING_INCLUDED_VERSIONS_1));
207207
assertTrue(substringVersionConfig.isIncluded(SUBSTRING_INCLUDED_VERSIONS_2));
208208

209-
assertFalse(nullVersionConfig.isIncluded(""));
210-
assertFalse(oneVersionConfig.isIncluded(""));
211-
assertFalse(multiVersionConfig.isIncluded(""));
212-
assertFalse(substringVersionConfig.isIncluded(""));
213-
214-
assertFalse(nullVersionConfig.isIncluded(null));
215-
assertFalse(oneVersionConfig.isIncluded(null));
216-
assertFalse(multiVersionConfig.isIncluded(null));
217-
assertFalse(substringVersionConfig.isIncluded(null));
218-
219209
}
220210

221211
@Test
@@ -228,7 +218,7 @@ public void clearCache() throws Exception {
228218
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
229219
config.setDefaultVersion("master");
230220
config.setImplicit(true);
231-
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null, "master"));
221+
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null, null));
232222
GlobalLibraries.get().getLibraries().add(config);
233223
// Run build and check that cache gets created.
234224
WorkflowJob p = r.createProject(WorkflowJob.class);
@@ -274,4 +264,43 @@ public void clearCacheConflict() throws Exception {
274264
assertThat(new File(cache.withSuffix("-name.txt").getRemote()), not(anExistingFile()));
275265
}
276266

267+
@Issue("JENKINS-69135") //"Versions to include" feature for caching
268+
@Test
269+
public void clearCacheIncludedVersion() throws Exception {
270+
sampleRepo.init();
271+
sampleRepo.write("vars/foo.groovy", "def call() { echo 'foo' }");
272+
sampleRepo.git("add", "vars");
273+
sampleRepo.git("commit", "--message=init");
274+
sampleRepo.git("branch", "test/include");
275+
LibraryConfiguration config = new LibraryConfiguration("library",
276+
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
277+
config.setDefaultVersion("master");
278+
config.setAllowVersionOverride(true);
279+
config.setImplicit(false);
280+
config.setCachingConfiguration(new LibraryCachingConfiguration(30, "", "test/include"));
281+
GlobalLibraries.get().getLibraries().add(config);
282+
// Run build and check that cache gets created.
283+
WorkflowJob p = r.createProject(WorkflowJob.class);
284+
p.setDefinition(new CpsFlowDefinition("library identifier: 'library', changelog:false\n\nfoo()", true));
285+
WorkflowRun b = r.buildAndAssertSuccess(p);
286+
WorkflowJob p2 = r.createProject(WorkflowJob.class);
287+
p2.setDefinition(new CpsFlowDefinition("library identifier: 'library@test/include', changelog:false\n\nfoo()", true));
288+
WorkflowRun b2 = r.buildAndAssertSuccess(p2);
289+
LibrariesAction action = b.getAction(LibrariesAction.class);
290+
LibraryRecord record = action.getLibraries().get(0);
291+
LibrariesAction action2 = b2.getAction(LibrariesAction.class);
292+
LibraryRecord record2 = action2.getLibraries().get(0);
293+
FilePath cache = LibraryCachingConfiguration.getGlobalLibrariesCacheDir().child(record.getDirectoryName());
294+
FilePath cache2 = LibraryCachingConfiguration.getGlobalLibrariesCacheDir().child(record2.getDirectoryName());
295+
assertThat(new File(cache.getRemote()), not(anExistingDirectory()));
296+
assertThat(new File(cache.withSuffix("-name.txt").getRemote()), not(anExistingFile()));
297+
assertThat(new File(cache2.getRemote()), anExistingDirectory());
298+
assertThat(new File(cache2.withSuffix("-name.txt").getRemote()), anExistingFile());
299+
// Clears cache for the entire library, until the "Delete specific cache version" feature in merged
300+
// Clear the cache. TODO: Would be more realistic to set up security and use WebClient.
301+
ExtensionList.lookupSingleton(LibraryCachingConfiguration.DescriptorImpl.class).doClearCache("library", false);
302+
assertThat(new File(cache2.getRemote()), not(anExistingDirectory()));
303+
assertThat(new File(cache2.withSuffix("-name.txt").getRemote()), not(anExistingFile()));
304+
}
305+
277306
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class ResourceStepTest {
9292
initFixedContentLibrary();
9393

9494
LibraryConfiguration libraryConfig = new LibraryConfiguration("stuff", new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
95-
libraryConfig.setCachingConfiguration(new LibraryCachingConfiguration(0, "test_unused other", null));
95+
libraryConfig.setCachingConfiguration(new LibraryCachingConfiguration(0, "test_unused other", ""));
9696
GlobalLibraries.get().setLibraries(Collections.singletonList(libraryConfig));
9797
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
9898

0 commit comments

Comments
 (0)