diff --git a/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java b/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java index 117640e0a..5a0eaa3e1 100644 --- a/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java +++ b/src/main/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition.java @@ -98,29 +98,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; } @@ -158,7 +160,13 @@ public ParameterValue createValue(CLICommand command, String value) throws IOExc @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)); @@ -173,6 +181,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}. @@ -183,17 +195,18 @@ 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(); SVNRepository repo = null; SVNClientManager clientManager = null; + String tmpTagsDir = StringUtils.isEmpty(subDir) ? getTagsDir() : getTagsDir() + "/" + subDir; try { ISVNAuthenticationProvider authProvider = CredentialsSVNAuthenticationProviderImpl.createAuthenticationProvider( - context, getTagsDir(), getCredentialsId(), null, TaskListener.NULL + context, tmpTagsDir, getCredentialsId(), null, TaskListener.NULL ); ISVNAuthenticationManager authManager = SubversionSCM.createSvnAuthenticationManager(authProvider); - SVNURL repoURL = SVNURL.parseURIDecoded(getTagsDir()); + SVNURL repoURL = SVNURL.parseURIDecoded(tmpTagsDir); repo = SVNRepositoryFactory.create(repoURL); repo.setAuthenticationManager(authManager); @@ -201,16 +214,16 @@ context, getTagsDir(), getCredentialsId(), null, TaskListener.NULL SVNLogClient logClient = clientManager.getLogClient(); 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")); } finally { if (repo != null) { @@ -261,6 +274,10 @@ public String getMaxTags() { return maxTags; } + public boolean isUseLatestTag() { + return useLatestTag; + } + /** * Checks to see if given repository contains a trunk, branches, and tags * directories. @@ -314,12 +331,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); @@ -327,7 +344,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 de2ae4f84..a5457b463 100644 --- a/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly +++ b/src/main/resources/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinition/config.jelly @@ -41,6 +41,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 0275084b6..429156fbd 100644 --- a/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java +++ b/src/test/java/hudson/scm/listtagsparameter/ListSubversionTagsParameterDefinitionTest.java @@ -56,7 +56,7 @@ public class ListSubversionTagsParameterDefinitionTest { public void listTags() throws Exception { Proc p = AbstractSubversionTest.runSvnServe(tmp, 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");