From c22198da8b4937e976f5520e7da3057e12a3d33b Mon Sep 17 00:00:00 2001 From: gabemontero Date: Wed, 30 Aug 2017 14:55:46 -0400 Subject: [PATCH] fix sorting based on build id; incorporate start time wrt finding latest build --- .../model/IOpenShiftBuildVerifier.java | 73 ++++++++++++++----- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/openshift/jenkins/plugins/pipeline/model/IOpenShiftBuildVerifier.java b/src/main/java/com/openshift/jenkins/plugins/pipeline/model/IOpenShiftBuildVerifier.java index 6f37e9e..9193826 100644 --- a/src/main/java/com/openshift/jenkins/plugins/pipeline/model/IOpenShiftBuildVerifier.java +++ b/src/main/java/com/openshift/jenkins/plugins/pipeline/model/IOpenShiftBuildVerifier.java @@ -10,10 +10,14 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang.StringUtils; + public interface IOpenShiftBuildVerifier extends ITimedOpenShiftPlugin { String DISPLAY_NAME = "Verify OpenShift Build"; @@ -38,26 +42,35 @@ default String getCheckForTriggeredDeployments(Map overrides) { return getOverride(getCheckForTriggeredDeployments(), overrides); } - default List getBuildIDs(IClient client, + default String getLatestBuildID(IClient client, Map overrides) { + Map filter = new HashMap(); + filter.put("openshift.io/build-config.name", getBldCfg(overrides)); List blds = client.list(ResourceKind.BUILD, - getNamespace(overrides)); - List ids = new ArrayList(); + getNamespace(overrides), filter); + // we'll get a list of startimes that we'll sort on to get the latest; + // we'll also build a map of startimes to build ids, so that we return + // the build ID related to the latest starttime + List starttimes = new ArrayList(); + Map timeToID = new HashMap(); + /* + * in case multiple builds have the same start time (parallel builds), + * use our custom comparator to make sure build-4 comes before build-38; + * then, if consecutive builds have the same start time, that later id + * number will be later in this list, and replace earlier entries in the + * timeToID map + */ + Collections.sort(blds, new BuildNameComparator()); for (IBuild bld : blds) { - if (bld.getName().startsWith(getBldCfg(overrides))) { - ids.add(bld.getName()); - } + starttimes.add(bld.getBuildStatus().getStartTime()); + timeToID.put(bld.getBuildStatus().getStartTime(), bld.getName()); } - return ids; - } - - default String getLatestBuildID(List ids) { - String bldId = null; - if (ids.size() > 0) { - Collections.sort(ids); - bldId = ids.get(ids.size() - 1); + String starttime = null; + if (starttimes.size() > 0) { + Collections.sort(starttimes); + starttime = starttimes.get(starttimes.size() - 1); } - return bldId; + return timeToID.get(starttime); } default boolean coreLogic(Launcher launcher, TaskListener listener, @@ -74,9 +87,7 @@ DISPLAY_NAME, getBldCfg(overrides), IClient client = this.getClient(listener, DISPLAY_NAME, overrides); if (client != null) { - List ids = getBuildIDs(client, overrides); - - String bldId = getLatestBuildID(ids); + String bldId = getLatestBuildID(client, overrides); if (!checkDeps) { listener.getLogger() @@ -106,4 +117,30 @@ DISPLAY_NAME, getBldCfg(overrides), } + public class BuildNameComparator implements Comparator { + + // makes optimizing assumptions based on format of openshift build names + @Override + public int compare(IBuild o1, IBuild o2) { + String id1 = o1.getName(); + String id2 = o2.getName(); + int at = StringUtils.indexOfDifference(id1, id2); + // no index means strings equal + if (at == StringUtils.INDEX_NOT_FOUND) + return 0; + String rem0 = id1.substring(at); + String rem1 = id2.substring(at); + // means id2 is just longer, so id1 < id2, so return -1 + if (StringUtils.isBlank(rem0)) + return -1; + // means id1 is just longer, so id1 > id2, so return 1 + if (StringUtils.isBlank(rem1)) + return 1; + if (Integer.valueOf(rem0) < Integer.valueOf(rem1)) + return -1; + return 1; + } + + } + }