Skip to content
Permalink
Browse files
Merge branch 'pr/53'
JENKINS-26701
  • Loading branch information
Radek Antoniuk committed Oct 8, 2015
2 parents 15ee859 + a968e45 commit 06fe2a20ca6948a1d5bfa041ba8fbe33b6a7b901
Showing 2 changed files with 92 additions and 3 deletions.
@@ -16,10 +16,10 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;

import static hudson.Util.fixNull;

public class JiraVersionParameterDefinition extends ParameterDefinition {
private static final long serialVersionUID = 4232979892748310160L;

@@ -69,10 +69,12 @@ public List<JiraVersionParameterDefinition.Result> getVersions() throws IOExcept
if (session == null) throw new IllegalStateException("Remote access for JIRA isn't configured in Jenkins");

List<Version> versions = session.getVersions(projectKey);
SortedSet<Version> orderedVersions = new TreeSet<Version>(new VersionComparator());
orderedVersions.addAll(versions);

List<Result> projectVersions = new ArrayList<Result>();

for (Version version : fixNull(versions)) {
for (Version version : orderedVersions) {
if (match(version)) projectVersions.add(new Result(version));
}

@@ -0,0 +1,87 @@
package hudson.plugins.jira.versionparameter;

import com.atlassian.jira.rest.client.api.domain.Version;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
* This comparator can ordering the following formats versions:
* 9.9.9.9.9
* V-5.2.3
* PDFREPORT-2.3.4
* PDFREPORT-2.3
* 1.12.2.3.4
* 1.3.4
* 1.1.1.2
* 1.1.1.1
*/
public class VersionComparator implements Comparator<Version> {

public int compare(Version rev1, Version rev2) {
int result = 0;
boolean startWithoutLetters = true;

List<String> listRev1 = Arrays.asList(rev1.getName().split("\\."));
String oldRev1 = listRev1.get(0);

List<String> listRev2 = Arrays.asList(rev2.getName().split("\\."));
String oldRev2 = listRev2.get(0);

listRev1.set(0, getNumberVersion(listRev1.get(0)));
listRev2.set(0, getNumberVersion(listRev2.get(0)));

if (oldRev1.equals(listRev1.get(0)) && oldRev2.equals(listRev2.get(0))) {
startWithoutLetters = false;
}

int lenRev1 = listRev1.size();
int lenRev2 = listRev2.size();

Integer min = Math.min(lenRev1, lenRev2);

for (int i = 0; i < min; i++) {
String s1 = listRev1.get(i);
String s2 = listRev2.get(i);
try {
Integer coor1 = Integer.parseInt(s1);
Integer coor2 = Integer.parseInt(s2);
result = coor2.compareTo(coor1);
if (result != 0) {
break;
}
} catch (Exception e) {
}
}

if (result == 0) {
if (lenRev1 > lenRev2) {
result = -1;
} else if (lenRev2 > lenRev1) {
result = 1;
} else if (startWithoutLetters) {
result = 1;
}

}

return result;
}

/**
* For the cases like this:
* PDFREPORT-2.3.4
* return this
* 2.3.4
*/
private String getNumberVersion(String firstV) {
String res = firstV;
if (!firstV.matches("[0-9.]+") && firstV.contains("-")) {
res = firstV.split("-")[1];
}

return res;
}

}

0 comments on commit 06fe2a2

Please sign in to comment.