Skip to content

Commit

Permalink
[FIXED JENKINS-25910] Sort nodes for label for UI (#1498)
Browse files Browse the repository at this point in the history
* [FIXED JENKINS-25910] Sort nodes for label for UI

* [JENKINS-25910] Sort Jenkins master at beginning

* Consistent sort order

* [JENKINS-25910] Instead use dedicated UI support method
  • Loading branch information
daniel-beck authored and oleg-nenashev committed Apr 7, 2018
1 parent b6dc503 commit 70c408c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion core/src/main/java/hudson/model/Label.java
Expand Up @@ -49,6 +49,8 @@
import jenkins.model.ModelObjectWithChildren;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
Expand All @@ -58,6 +60,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -196,6 +199,16 @@ public boolean isSelfLabel() {
return nodes.size() == 1 && nodes.iterator().next().getSelfLabel() == this;
}

private static class NodeSorter implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
if (o1 == o2) {
return 0;
}
return o1 instanceof Jenkins ? -1 : (o2 instanceof Jenkins ? 1 : o1.getNodeName().compareTo(o2.getNodeName()));
}
}

/**
* Gets all {@link Node}s that belong to this label.
*/
Expand All @@ -204,7 +217,7 @@ public Set<Node> getNodes() {
Set<Node> nodes = this.nodes;
if(nodes!=null) return nodes;

Set<Node> r = new HashSet<Node>();
Set<Node> r = new HashSet<>();
Jenkins h = Jenkins.getInstance();
if(this.matches(h))
r.add(h);
Expand All @@ -215,6 +228,13 @@ public Set<Node> getNodes() {
return this.nodes = Collections.unmodifiableSet(r);
}

@Restricted(DoNotUse.class) // Jelly
public Set<Node> getSortedNodes() {
Set<Node> r = new TreeSet<>(new NodeSorter());
r.addAll(getNodes());
return r;
}

/**
* Gets all {@link Cloud}s that can launch for this label.
*/
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/hudson/model/Label/index.jelly
Expand Up @@ -42,7 +42,7 @@ THE SOFTWARE.

<div>
<h2>${%Nodes}</h2>
<j:forEach var="n" items="${it.nodes}">
<j:forEach var="n" items="${it.sortedNodes}">
<j:set var="c" value="${app.getComputer(n.nodeName)}"/>
<j:set var="url" value="${rootURL}/${c.url}"/>
<nobr>
Expand Down

0 comments on commit 70c408c

Please sign in to comment.