Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-14155]Use the lastest changed tag as default #157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those additions will result in breaking the binary compatibility. Is it required to pass by the constructor ? I think a simple use of a setter / putting a default value would be sufficient.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno exactly the guideline for this plugin, but from my PoV it's better to use @DataBoundSetter when you add field instead of putting that in the constructor.

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;
}
Expand Down Expand Up @@ -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<String> 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<String> tags = getTags(null);
if (tags.size() > 0) {
return new ListSubversionTagsParameterValue(getName(), getTagsDir(), tags.get(0));
Expand All @@ -173,6 +181,10 @@ public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

@Nonnull public List<String> getTags(@Nullable Job context) {
return getTags(context, "", isReverseByDate());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to think about compatibility


/**
* Returns a list of Subversion dirs to be displayed in
* {@code ListSubversionTagsParameterDefinition/index.jelly}.
Expand All @@ -183,34 +195,35 @@ public DescriptorImpl getDescriptor() {
* <p>This method never returns {@code null}. In case an error happens, the
* returned list contains an error message prefixed by {@code !}.</p>
*/
@Nonnull public List<String> getTags(@Nullable Job context) {
@Nonnull public List<String> getTags(@Nullable Job context, String subDir, boolean localReverseByDate) {
List<String> dirs = new ArrayList<String>();

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);
clientManager = SVNClientManager.newInstance(null,authManager);
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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -314,20 +331,20 @@ private boolean isInt(String value) {
* @return List of directories.
* @throws SVNException
*/
private List<String> getSVNRootRepoDirectories(SVNLogClient logClient, SVNURL repoURL) throws SVNException {
private List<String> 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<String> branches = branchesEntryHandler.getDirs(isReverseByDate(), isReverseByName());
List<String> branches = branchesEntryHandler.getDirs(localReverseByDate, isReverseByName());
branches.remove("");
appendTargetDir(SVN_BRANCHES, branches);

// Get the tags repository contents
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<String> tags = tagsEntryHandler.getDirs(isReverseByDate(), isReverseByName());
List<String> tags = tagsEntryHandler.getDirs(localReverseByDate, isReverseByName());
tags.remove("");
appendTargetDir(SVN_TAGS, tags);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<f:entry title="${%Default value}" field="defaultValue">
<f:textbox/>
</f:entry>
<f:entry field="useLatestTag">
<f:checkbox/>
<label class="attach-previous">${%Use latest tag}</label>
</f:entry>
<f:entry title="${%Maximum tags to display}" field="maxTags">
<f:textbox/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
- The MIT License
-
- Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->

<div>
Check this option so that the latest tag in the tags/ subdirectory is used as default value.

<p>
If this option is checked, the <b>Default value</b> one won't be taken into
account.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> tags = def.getTags(null);
List<String> expected = Arrays.asList("trunk", "tags/a", "tags/b", "tags/c");

Expand Down