Skip to content

Commit

Permalink
Handling case for empty versions to include
Browse files Browse the repository at this point in the history
  • Loading branch information
KINAXIS\agharat committed Aug 18, 2022
1 parent a0d1dfe commit e5374aa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
import org.jenkinsci.plugins.workflow.cps.GlobalVariableSet;
Expand Down Expand Up @@ -210,7 +211,10 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
shouldCache = false;
}

if(shouldCache && cachingConfiguration.isIncluded(version)) {
//If the included versions is blank/null, cache irrespective
//else check if that version is included and then cache only that version

if((shouldCache && cachingConfiguration.isIncluded(version)) || (shouldCache && StringUtils.isBlank(cachingConfiguration.getIncludedVersionsStr()))) {
retrieveLock.readLock().lockInterruptibly();
try {
CacheStatus cacheStatus = getCacheStatus(cachingConfiguration, versionCacheDir);
Expand All @@ -220,8 +224,8 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
try {
boolean retrieve = false;
switch (getCacheStatus(cachingConfiguration, versionCacheDir)) {
case VALID:
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
case VALID:
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
break;
case DOES_NOT_EXIST:
retrieve = true;
Expand All @@ -236,20 +240,20 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
retrieve = true;
break;
}

if (retrieve) {
listener.getLogger().println("Caching library " + name + "@" + version);
listener.getLogger().println("Caching library " + name + "@" + version);
versionCacheDir.mkdirs();
retriever.retrieve(name, version, changelog, versionCacheDir, run, listener);
}
retrieveLock.readLock().lock();
} finally {
retrieveLock.writeLock().unlock();
retrieveLock.writeLock().unlock();
}
} else {
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
listener.getLogger().println("Library " + name + "@" + version + " is cached. Copying from home.");
}

lastReadFile.touch(System.currentTimeMillis());
versionCacheDir.withSuffix("-name.txt").write(name, "UTF-8");
versionCacheDir.copyRecursiveTo(libDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public Boolean isRefreshEnabled() {
public String getExcludedVersionsStr() {
return excludedVersionsStr;
}
public String getIncludedVersionsStr() { return includedVersionsStr; }
public String getIncludedVersionsStr() {
if(StringUtils.isBlank(includedVersionsStr)){
return null;
}
return includedVersionsStr;
}


private List<String> getExcludedVersions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,6 @@ public void isIncluded() {
assertTrue(substringVersionConfig.isIncluded(SUBSTRING_INCLUDED_VERSIONS_1));
assertTrue(substringVersionConfig.isIncluded(SUBSTRING_INCLUDED_VERSIONS_2));

assertFalse(nullVersionConfig.isIncluded(""));
assertFalse(oneVersionConfig.isIncluded(""));
assertFalse(multiVersionConfig.isIncluded(""));
assertFalse(substringVersionConfig.isIncluded(""));

assertFalse(nullVersionConfig.isIncluded(null));
assertFalse(oneVersionConfig.isIncluded(null));
assertFalse(multiVersionConfig.isIncluded(null));
assertFalse(substringVersionConfig.isIncluded(null));

}

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

@Issue("JENKINS-69135") //"Versions to include" feature for caching
@Test
public void clearCacheIncludedVersion() throws Exception {
sampleRepo.init();
sampleRepo.write("vars/foo.groovy", "def call() { echo 'foo' }");
sampleRepo.git("add", "vars");
sampleRepo.git("commit", "--message=init");
sampleRepo.git("branch", "test/include");
LibraryConfiguration config = new LibraryConfiguration("library",
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
config.setDefaultVersion("master");
config.setAllowVersionOverride(true);
config.setImplicit(false);
config.setCachingConfiguration(new LibraryCachingConfiguration(30, "", "test/include"));
GlobalLibraries.get().getLibraries().add(config);
// Run build and check that cache gets created.
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition("library identifier: 'library', changelog:false\n\nfoo()", true));
WorkflowRun b = r.buildAndAssertSuccess(p);
WorkflowJob p2 = r.createProject(WorkflowJob.class);
p2.setDefinition(new CpsFlowDefinition("library identifier: 'library@test/include', changelog:false\n\nfoo()", true));
WorkflowRun b2 = r.buildAndAssertSuccess(p2);
LibrariesAction action = b.getAction(LibrariesAction.class);
LibraryRecord record = action.getLibraries().get(0);
LibrariesAction action2 = b2.getAction(LibrariesAction.class);
LibraryRecord record2 = action2.getLibraries().get(0);
FilePath cache = LibraryCachingConfiguration.getGlobalLibrariesCacheDir().child(record.getDirectoryName());
FilePath cache2 = LibraryCachingConfiguration.getGlobalLibrariesCacheDir().child(record2.getDirectoryName());
assertThat(new File(cache.getRemote()), not(anExistingDirectory()));
assertThat(new File(cache.withSuffix("-name.txt").getRemote()), not(anExistingFile()));
assertThat(new File(cache2.getRemote()), anExistingDirectory());
assertThat(new File(cache2.withSuffix("-name.txt").getRemote()), anExistingFile());
// Clears cache for the entire library, until the "Delete specific cache version" feature in merged
// Clear the cache. TODO: Would be more realistic to set up security and use WebClient.
ExtensionList.lookupSingleton(LibraryCachingConfiguration.DescriptorImpl.class).doClearCache("library", false);
assertThat(new File(cache2.getRemote()), not(anExistingDirectory()));
assertThat(new File(cache2.withSuffix("-name.txt").getRemote()), not(anExistingFile()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ResourceStepTest {
initFixedContentLibrary();

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

Expand Down

0 comments on commit e5374aa

Please sign in to comment.