Skip to content

Commit

Permalink
[JENKINS-69135] Use DataboundSetter
Browse files Browse the repository at this point in the history
  • Loading branch information
rsandell committed Aug 29, 2023
1 parent 884f777 commit 4d79950
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import hudson.Extension;
import hudson.FilePath;
import hudson.RestrictedSince;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import java.io.IOException;
Expand Down Expand Up @@ -35,12 +39,21 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
public static final String GLOBAL_LIBRARIES_DIR = "global-libraries-cache";
public static final String LAST_READ_FILE = "last_read";

@DataBoundConstructor public LibraryCachingConfiguration(int refreshTimeMinutes, String excludedVersionsStr, String includedVersionsStr) {
@DataBoundConstructor public LibraryCachingConfiguration(int refreshTimeMinutes, String excludedVersionsStr) {
this.refreshTimeMinutes = refreshTimeMinutes;
this.excludedVersionsStr = excludedVersionsStr;
this.includedVersionsStr = includedVersionsStr;
this.includedVersionsStr = "";
}

/*
* Visible for testing ...
*/
@Restricted(NoExternalUse.class)
LibraryCachingConfiguration(int refreshTimeMinutes, String excludedVersionsStr, String includedVersionsStr) {
this.refreshTimeMinutes = refreshTimeMinutes;
this.excludedVersionsStr = excludedVersionsStr;
this.includedVersionsStr = includedVersionsStr;
}

public int getRefreshTimeMinutes() {
return refreshTimeMinutes;
Expand All @@ -64,6 +77,10 @@ public String getIncludedVersionsStr() {
return includedVersionsStr;
}

@DataBoundSetter
public void setIncludedVersionsStr(String includedVersionsStr) {
this.includedVersionsStr = includedVersionsStr;
}

private List<String> getExcludedVersions() {
if (excludedVersionsStr == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class GlobalLibrariesTest {
assertEquals(Collections.emptyList(), gl.getLibraries());
LibraryConfiguration foo = new LibraryConfiguration("foo", new SCMSourceRetriever(new SubversionSCMSource("foo", "https://phony.jenkins.io/foo/")));
LibraryConfiguration bar = new LibraryConfiguration("bar", new SCMSourceRetriever(new GitSCMSource(null, "https://phony.jenkins.io/bar.git", "", "origin", "+refs/heads/*:refs/remotes/origin/*", "*", "", true)));
LibraryCachingConfiguration cachingConfiguration = new LibraryCachingConfiguration(120, "develop", "master stable");
foo.setCachingConfiguration(cachingConfiguration);
bar.setDefaultVersion("master");
bar.setImplicit(true);
bar.setAllowVersionOverride(false);
Expand All @@ -72,6 +74,14 @@ public class GlobalLibrariesTest {
r.assertEqualDataBoundBeans(Arrays.asList(foo, bar), libs);
libs = gl.getLibraries();
r.assertEqualDataBoundBeans(Arrays.asList(foo, bar), libs);
boolean noFoo = true;
for (LibraryConfiguration lib : libs) {
if ("foo".equals(lib.getName())) {
noFoo = false;
r.assertEqualDataBoundBeans(lib.getCachingConfiguration(), cachingConfiguration);
}
}
assertFalse("Missing a library called foo (should not happen)", noFoo);
}

@Issue("SECURITY-1422")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public class LibraryAdderTest {
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
globalLib.setDefaultVersion("master");
globalLib.setImplicit(true);
globalLib.setCachingConfiguration(new LibraryCachingConfiguration(60, "", ""));
globalLib.setCachingConfiguration(new LibraryCachingConfiguration(60, ""));
GlobalLibraries.get().setLibraries(Collections.singletonList(globalLib));
// Create a folder library with the same name and which is also set up to enable caching.
sampleRepo2.write("vars/folderLibVar.groovy", "def call() { jenkins.model.Jenkins.get().setSystemMessage('folder library') }");
Expand All @@ -430,7 +430,7 @@ public class LibraryAdderTest {
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo2.toString(), "", "*", "", true)));
folderLib.setDefaultVersion("master");
folderLib.setImplicit(true);
folderLib.setCachingConfiguration(new LibraryCachingConfiguration(60, "", ""));
folderLib.setCachingConfiguration(new LibraryCachingConfiguration(60, ""));
Folder f = r.jenkins.createProject(Folder.class, "folder1");
f.getProperties().add(new FolderLibraries(Collections.singletonList(folderLib)));
// Create a job that uses the folder library, which will take precedence over the global library, since they have the same name.
Expand Down Expand Up @@ -493,7 +493,7 @@ public void parallelBuildsDontInterfereWithExpiredCache() throws Throwable {
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
config.setDefaultVersion("master");
config.setImplicit(true);
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null, null));
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null));
GlobalLibraries.get().getLibraries().add(config);
WorkflowJob p1 = r.createProject(WorkflowJob.class);
WorkflowJob p2 = r.createProject(WorkflowJob.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void smokes() throws Throwable {
new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true)));
config.setDefaultVersion("master");
config.setImplicit(true);
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null, null));
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null));
GlobalLibraries.get().getLibraries().add(config);
// Run build and check that cache gets created.
WorkflowJob p = r.createProject(WorkflowJob.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,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, null));
config.setCachingConfiguration(new LibraryCachingConfiguration(30, null));
GlobalLibraries.get().getLibraries().add(config);
// Run build and check that cache gets created.
WorkflowJob p = r.createProject(WorkflowJob.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class ResourceStepTest {
initFixedContentLibrary();

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

Expand All @@ -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", ""));
libraryConfig.setCachingConfiguration(new LibraryCachingConfiguration(0, "test_unused other"));
GlobalLibraries.get().setLibraries(Collections.singletonList(libraryConfig));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

Expand All @@ -114,7 +114,7 @@ public class ResourceStepTest {
initFixedContentLibrary();

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

Expand Down

0 comments on commit 4d79950

Please sign in to comment.