From 13c275f66b923b8594147125fce1ac6b4af8aa0b Mon Sep 17 00:00:00 2001 From: Micha Wiedenmann Date: Thu, 28 Jan 2016 13:12:03 +0100 Subject: [PATCH] Use the lastest changed tag as default --- ...ListSubversionTagsParameterDefinition.java | 50 +++++++++++++------ .../config.jelly | 4 ++ .../help-useLatestTag.html | 31 ++++++++++++ ...SubversionTagsParameterDefinitionTest.java | 2 +- 4 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/help-useLatestTag.html diff --git a/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java b/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java index 8ecfbf641..4fd213a75 100644 --- a/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java +++ b/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java @@ -91,29 +91,31 @@ public class ListSubversionTagsParameterDefinition extends ParameterDefinition { private final boolean reverseByDate; private final boolean reverseByName; private final String defaultValue; + private final boolean useLatestTag; private final String maxTags; private static final String SVN_BRANCHES = "branches"; private static final String SVN_TAGS = "tags"; private static final String SVN_TRUNK = "trunk"; @Deprecated - public ListSubversionTagsParameterDefinition(String name, String tagsDir, String tagsFilter, String defaultValue, String maxTags, boolean reverseByDate, boolean reverseByName, String uuid) { - this(name, tagsDir, null, tagsFilter, defaultValue, maxTags, reverseByDate, reverseByName); + public ListSubversionTagsParameterDefinition(String name, String tagsDir, String tagsFilter, String defaultValue, boolean useLatestTag, String maxTags, boolean reverseByDate, boolean reverseByName, String uuid) { + this(name, tagsDir, null, tagsFilter, defaultValue, useLatestTag, maxTags, reverseByDate, reverseByName); } @Deprecated - public ListSubversionTagsParameterDefinition(String name, String tagsDir, String tagsFilter, String defaultValue, String maxTags, boolean reverseByDate, boolean reverseByName, String uuid, String credentialsId) { - this(name, tagsDir, credentialsId, tagsFilter, defaultValue, maxTags, reverseByDate, reverseByName); + public ListSubversionTagsParameterDefinition(String name, String tagsDir, String tagsFilter, String defaultValue, boolean useLatestTag, String maxTags, boolean reverseByDate, boolean reverseByName, String uuid, String credentialsId) { + this(name, tagsDir, credentialsId, tagsFilter, defaultValue, useLatestTag, maxTags, reverseByDate, reverseByName); } @DataBoundConstructor - public ListSubversionTagsParameterDefinition(String name, String tagsDir, String credentialsId, String tagsFilter, String defaultValue, String maxTags, boolean reverseByDate, boolean reverseByName) { + public ListSubversionTagsParameterDefinition(String name, String tagsDir, String credentialsId, String tagsFilter, String defaultValue, boolean useLatestTag, String maxTags, boolean reverseByDate, boolean reverseByName) { super(name, ResourceBundleHolder.get(ListSubversionTagsParameterDefinition.class).format("TagDescription")); this.tagsDir = Util.removeTrailingSlash(tagsDir); this.tagsFilter = tagsFilter; this.reverseByDate = reverseByDate; this.reverseByName = reverseByName; this.defaultValue = defaultValue; + this.useLatestTag = useLatestTag; this.maxTags = maxTags; this.credentialsId = credentialsId; } @@ -143,7 +145,13 @@ public ParameterValue createValue(StaplerRequest req, JSONObject formData) { @Override public ParameterValue getDefaultParameterValue() { - if (StringUtils.isEmpty(this.defaultValue)) { + if (isUseLatestTag()) { + List tags = getTags(null, "tags", true); + if (tags.size() > 0) { + return new ListSubversionTagsParameterValue(getName(), getTagsDir() + "/tags", tags.get(0)); + } + return null; + } else if (StringUtils.isEmpty(this.defaultValue)) { List tags = getTags(null); if (tags.size() > 0) { return new ListSubversionTagsParameterValue(getName(), getTagsDir(), tags.get(0)); @@ -158,6 +166,10 @@ public DescriptorImpl getDescriptor() { return (DescriptorImpl) super.getDescriptor(); } + @Nonnull public List getTags(@Nullable Job context) { + return getTags(context, "", isReverseByDate()); + } + /** * Returns a list of Subversion dirs to be displayed in * {@code ListSubversionTagsParameterDefinition/index.jelly}. @@ -168,31 +180,33 @@ public DescriptorImpl getDescriptor() { *

This method never returns {@code null}. In case an error happens, the * returned list contains an error message prefixed by {@code !}.

*/ - @Nonnull public List getTags(@Nullable Job context) { + @Nonnull public List getTags(@Nullable Job context, String subDir, boolean localReverseByDate) { List dirs = new ArrayList(); + String tmpTagsDir = StringUtils.isEmpty(subDir) ? getTagsDir() : getTagsDir() + "/" + subDir; + try { ISVNAuthenticationProvider authProvider = CredentialsSVNAuthenticationProviderImpl.createAuthenticationProvider( - context, getTagsDir(), getCredentialsId(), null + context, tmpTagsDir, getCredentialsId(), null ); ISVNAuthenticationManager authManager = SubversionSCM.createSvnAuthenticationManager(authProvider); - SVNURL repoURL = SVNURL.parseURIDecoded(getTagsDir()); + SVNURL repoURL = SVNURL.parseURIDecoded(tmpTagsDir); SVNRepository repo = SVNRepositoryFactory.create(repoURL); repo.setAuthenticationManager(authManager); SVNLogClient logClient = new SVNLogClient(authManager, null); if (isSVNRepositoryProjectRoot(repo)) { - dirs = this.getSVNRootRepoDirectories(logClient, repoURL); + dirs = this.getSVNRootRepoDirectories(logClient, repoURL, localReverseByDate); } else { SimpleSVNDirEntryHandler dirEntryHandler = new SimpleSVNDirEntryHandler(tagsFilter); logClient.doList(repoURL, SVNRevision.HEAD, SVNRevision.HEAD, false, SVNDepth.IMMEDIATES, SVNDirEntry.DIRENT_TIME, dirEntryHandler); - dirs = dirEntryHandler.getDirs(isReverseByDate(), isReverseByName()); + dirs = dirEntryHandler.getDirs(localReverseByDate, isReverseByName()); } } catch(SVNException e) { // logs are not translated (IMO, this is a bad idea to translate logs) - LOGGER.log(Level.SEVERE, "An SVN exception occurred while listing the directory entries at " + getTagsDir(), e); + LOGGER.log(Level.SEVERE, "An SVN exception occurred while listing the directory entries at " + tmpTagsDir, e); return Collections.singletonList("!" + ResourceBundleHolder.get(ListSubversionTagsParameterDefinition.class).format("SVNException")); } @@ -201,7 +215,7 @@ context, getTagsDir(), getCredentialsId(), null removeParentDir(dirs); } else { - LOGGER.log(Level.INFO, "No directory entries were found for the following SVN repository: {0}", getTagsDir()); + LOGGER.log(Level.INFO, "No directory entries were found for the following SVN repository: {0}", tmpTagsDir); return Collections.singletonList("!" + ResourceBundleHolder.get(ListSubversionTagsParameterDefinition.class).format("NoDirectoryEntriesFound")); } @@ -242,6 +256,10 @@ public String getMaxTags() { return maxTags; } + public boolean isUseLatestTag() { + return useLatestTag; + } + /** * Checks to see if given repository contains a trunk, branches, and tags * directories. @@ -295,12 +313,12 @@ private boolean isInt(String value) { * @return List of directories. * @throws SVNException */ - private List getSVNRootRepoDirectories(SVNLogClient logClient, SVNURL repoURL) throws SVNException { + private List getSVNRootRepoDirectories(SVNLogClient logClient, SVNURL repoURL, boolean localReverseByDate) throws SVNException { // Get the branches repository contents SVNURL branchesRepo = repoURL.appendPath(SVN_BRANCHES, true); SimpleSVNDirEntryHandler branchesEntryHandler = new SimpleSVNDirEntryHandler(null); logClient.doList(branchesRepo, SVNRevision.HEAD, SVNRevision.HEAD, false, SVNDepth.IMMEDIATES, SVNDirEntry.DIRENT_ALL, branchesEntryHandler); - List branches = branchesEntryHandler.getDirs(isReverseByDate(), isReverseByName()); + List branches = branchesEntryHandler.getDirs(localReverseByDate, isReverseByName()); branches.remove(""); appendTargetDir(SVN_BRANCHES, branches); @@ -308,7 +326,7 @@ private List getSVNRootRepoDirectories(SVNLogClient logClient, SVNURL re SVNURL tagsRepo = repoURL.appendPath(SVN_TAGS, true); SimpleSVNDirEntryHandler tagsEntryHandler = new SimpleSVNDirEntryHandler(null); logClient.doList(tagsRepo, SVNRevision.HEAD, SVNRevision.HEAD, false, SVNDepth.IMMEDIATES, SVNDirEntry.DIRENT_ALL, tagsEntryHandler); - List tags = tagsEntryHandler.getDirs(isReverseByDate(), isReverseByName()); + List tags = tagsEntryHandler.getDirs(localReverseByDate, isReverseByName()); tags.remove(""); appendTargetDir(SVN_TAGS, tags); diff --git a/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly b/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly index bec932260..3789860fc 100644 --- a/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly +++ b/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly @@ -40,6 +40,10 @@ + + + + diff --git a/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/help-useLatestTag.html b/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/help-useLatestTag.html new file mode 100644 index 000000000..a7f5def04 --- /dev/null +++ b/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/help-useLatestTag.html @@ -0,0 +1,31 @@ + + +
+ Check this option so that the latest tag in the tags/ subdirectory is used as default value. + +

+ If this option is checked, the Default value one won't be taken into + account. +

diff --git a/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java b/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java index 125df3765..5419b082e 100644 --- a/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java +++ b/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java @@ -28,7 +28,7 @@ public class ListSubversionTagsParameterDefinitionTest extends AbstractSubversio public void testListTags() throws Exception { Proc p = runSvnServe(getClass().getResource("JENKINS-11933.zip")); try { - ListSubversionTagsParameterDefinition def = new ListSubversionTagsParameterDefinition("FOO", "svn://localhost/", null, "", "", "", false, false); + ListSubversionTagsParameterDefinition def = new ListSubversionTagsParameterDefinition("FOO", "svn://localhost/", null, "", "", false, "", false, false); List tags = def.getTags(null); List expected = Arrays.asList("trunk", "tags/a", "tags/b", "tags/c");