Skip to content

Commit

Permalink
[JENKINS-25163][JENKINS-25188] Take into account jobs with no labels
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiquebrice committed Oct 18, 2014
1 parent 804cf11 commit 9b99377
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Expand Up @@ -41,6 +41,7 @@
import hudson.model.AbstractProject;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.Node.Mode;
import hudson.model.labels.LabelAtom;
import hudson.model.RootAction;
import hudson.model.TopLevelItem;
Expand Down Expand Up @@ -73,6 +74,10 @@ public boolean getShowSingleNodeJobs() {
return GlobalSettings.get().getShowSingleNodeJobs();
}

public boolean getShowLabellessJobs() {
return GlobalSettings.get().getShowLabellessJobs();
}

/**
* This function scans all jobs and all nodes of this Jenkins instance
* to extract all LabelAtom defined. Goal is to list, per LabelAtom, all jobs
Expand Down Expand Up @@ -128,6 +133,28 @@ public List<LabelAtomData> getLabelsData() {
return result;
}

/**
* JENKINS-25188 - Orphaned jobs do not show jobs without label when all nodes set to Label restrictions
* This function scans all nodes to determine if at least one is in non-exclusive mode,
* meaning it can be used to run jobs with no labels.
* If there is no such node, jobs without labels can't be run at all
* @return true only if all nodes are set to Mode.EXCLUSIVE
*/
public boolean getOnlyExclusiveNodes() {
Jenkins jenkins = Jenkins.getInstance();
if (!Mode.EXCLUSIVE.equals(jenkins.getMode())) {
return false;
}
Iterator<Node> i = jenkins.getNodes().iterator();
while (i.hasNext()) {
if (!Mode.EXCLUSIVE.equals(i.next().getMode())) {
return false;
}
}
return true;
}

// JENKINS-25163 - Add list of jobs that do not have a label
// this function returns all jobs that have no associated label(s)
public ArrayList<AbstractProject<?, ?>> getJobsWithNoLabels() {
ArrayList<AbstractProject<?, ?>> noLabelsJobs = new ArrayList<AbstractProject<?, ?>>();
Expand All @@ -153,6 +180,8 @@ public int compare(AbstractProject<?, ?> o1, AbstractProject<?, ?> o2) {
// this function finds all jobs that can't run on any nodes
// because of labels (mis-)configuration
public ArrayList<AbstractProject<?, ?>> getOrphanedJobs() {
boolean bOnlyExclusiveNodes = getOnlyExclusiveNodes();

ArrayList<AbstractProject<?, ?>> orphanedJobs = new ArrayList<AbstractProject<?, ?>>();
for (AbstractProject<?, ?> job : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
if (!(job instanceof TopLevelItem)) {
Expand All @@ -162,7 +191,12 @@ public int compare(AbstractProject<?, ?> o1, AbstractProject<?, ?> o2) {
Label jobLabel = job.getAssignedLabel();
if (jobLabel == null) {
// if job.getAssignedLabel is null then the job can run
// anywhere, so we're fine
// anywhere, so we're fine...
if (bOnlyExclusiveNodes) {
// ... except if all nodes are in exclusive mode!
// JENKINS-25188 - Orphaned jobs do not show jobs without label when all nodes set to Label restrictions
orphanedJobs.add(job);
}
continue;
}
Jenkins jenkins = Jenkins.getInstance();
Expand Down
Expand Up @@ -58,6 +58,12 @@ public class GlobalSettings extends GlobalConfiguration {
*/
private boolean showSingleNodeJobs = true;

/**
* toggle to determine whether jobs with no labels should be shown
* in the dashboard
*/
private boolean showLabellessJobs = true;

public GlobalSettings() {
// this loads the settings from this plugin xml file
// into this instance's private members
Expand All @@ -83,6 +89,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
detailedView = formData.getBoolean("detailedView");
dashboardOrphanedJobsDetailedView = formData.getBoolean("dashboardOrphanedJobsDetailedView");
showSingleNodeJobs = formData.getBoolean("showSingleNodeJobs");
showLabellessJobs = formData.getBoolean("showLabellessJobs");

// save this instance members to the plugin configuration file
save();
Expand All @@ -100,4 +107,8 @@ public boolean getDashboardOrphanedJobsDetailedView() {
public boolean getShowSingleNodeJobs() {
return showSingleNodeJobs;
}

public boolean getShowLabellessJobs() {
return showLabellessJobs;
}
}
Expand Up @@ -55,7 +55,11 @@ THE SOFTWARE.
</j:forEach>
<br/><br/>

<j:set var="allExclusive" value="${it.onlyExclusiveNodes}" />
<h2>Nodes</h2>
<j:if test="${allExclusive}">
Note: all nodes are in exclusive mode.
</j:if>
<j:set var="nodes" value="${it.nodesData}" />
<j:forEach var="node" items="${nodes}">
<div>
Expand Down Expand Up @@ -107,6 +111,26 @@ THE SOFTWARE.
</j:otherwise>
</j:choose>
</j:if>
<br/><br/>

<j:if test="${it.showLabellessJobs}">
<j:set var="labellessJobs" value="${it.jobsWithNoLabels}" />
<h2>Label-less jobs</h2>
<j:choose>
<j:when test="${empty(labellessJobs)}">There are no jobs with no defined label.</j:when>
<j:otherwise>
The following job(s) have no assigned labels.<br/><br/>
<j:if test="${it.dashboardOrphanedJobsDetailedView}">
<t:projectView jobs="${labellessJobs}"/>
</j:if>
<j:if test="${!it.dashboardOrphanedJobsDetailedView}">
<j:forEach var="job" items="${labellessJobs}">
<a href="${job.absoluteUrl}">${job.name}</a>&#160;
</j:forEach>
</j:if>
</j:otherwise>
</j:choose>
</j:if>

</l:main-panel>

Expand Down
Expand Up @@ -36,6 +36,10 @@ THE SOFTWARE.
description="Uncheck if you want to see condensed lists of jobs on the Labels Dashboard">
<f:checkbox />
</f:entry>
<f:entry title="Labels Dashboard - Label-less jobs" field="showLabellessJobs"
description="Check if you want to see the list of jobs with no labels on the Labels Dashboard">
<f:checkbox />
</f:entry>
<f:entry title="List single-node/exclusive jobs" field="showSingleNodeJobs"
description="Check if you want to see the jobs that can run on only one node (Labels Dashboard, Linked Jobs for nodes)">
<f:checkbox />
Expand Down

0 comments on commit 9b99377

Please sign in to comment.