Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
JENKINS-51031 Support sorting of Jenkins pipeline based views
Browse files Browse the repository at this point in the history
* Introduced generic core classes for specific implementations to extend, allowing comparators to operate on the same data model regardless which view implementation is used
  • Loading branch information
tommysdk committed Jun 2, 2018
1 parent 7ec06b5 commit 00ab61d
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
along with Delivery Pipeline Plugin.
If not, see <http://www.gnu.org/licenses/>.
*/
package se.diabol.jenkins.pipeline.domain;
package se.diabol.jenkins.core;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/se/diabol/jenkins/core/GenericComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This file is part of Delivery Pipeline Plugin.
Delivery Pipeline Plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Delivery Pipeline Plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Delivery Pipeline Plugin.
If not, see <http://www.gnu.org/licenses/>.
*/
package se.diabol.jenkins.core;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.util.List;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public abstract class GenericComponent extends AbstractItem {

public GenericComponent(String name) {
super(name);
}

@Exported
public long getLastActivity() {
long result = 0;
if (getPipelines() != null) {
for (GenericPipeline pipeline : getPipelines()) {
long lastActivity = pipeline.getLastActivity();
if (lastActivity > result) {
result = lastActivity;
}
}
}
return result;
}

@Exported
public abstract List<? extends GenericPipeline> getPipelines();
}
30 changes: 30 additions & 0 deletions src/main/java/se/diabol/jenkins/core/GenericPipeline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
This file is part of Delivery Pipeline Plugin.
Delivery Pipeline Plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Delivery Pipeline Plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Delivery Pipeline Plugin.
If not, see <http://www.gnu.org/licenses/>.
*/
package se.diabol.jenkins.core;

import org.kohsuke.stapler.export.ExportedBean;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public abstract class GenericPipeline extends AbstractItem {

public GenericPipeline(String name) {
super(name);
}

public abstract long getLastActivity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
import se.diabol.jenkins.pipeline.domain.Component;
import se.diabol.jenkins.pipeline.domain.Pipeline;
import se.diabol.jenkins.pipeline.domain.PipelineException;
import se.diabol.jenkins.pipeline.sort.ComponentComparator;
import se.diabol.jenkins.pipeline.sort.ComponentComparatorDescriptor;
import se.diabol.jenkins.pipeline.sort.GenericComponentComparator;
import se.diabol.jenkins.pipeline.trigger.ManualTrigger;
import se.diabol.jenkins.pipeline.trigger.ManualTriggerFactory;
import se.diabol.jenkins.pipeline.trigger.TriggerException;
Expand Down Expand Up @@ -525,7 +525,7 @@ public List<Component> getPipelines() {
}
}
if (getSorting() != null && !getSorting().equals(NONE_SORTER)) {
ComponentComparatorDescriptor comparatorDescriptor = ComponentComparator.all().find(sorting);
ComponentComparatorDescriptor comparatorDescriptor = GenericComponentComparator.all().find(sorting);
if (comparatorDescriptor != null) {
components.sort(comparatorDescriptor.createInstance());
}
Expand Down Expand Up @@ -639,8 +639,8 @@ public ListBoxModel doFillNoOfPipelinesItems(@AncestorInPath ItemGroup<?> contex
}

public ListBoxModel doFillSortingItems() {
DescriptorExtensionList<ComponentComparator, ComponentComparatorDescriptor> descriptors =
ComponentComparator.all();
DescriptorExtensionList<GenericComponentComparator, ComponentComparatorDescriptor> descriptors =
GenericComponentComparator.all();
ListBoxModel options = new ListBoxModel();
options.add("None", NONE_SORTER);
for (ComponentComparatorDescriptor descriptor : descriptors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hudson.scm.RepositoryBrowser;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;

import java.io.IOException;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.core.GenericComponent;
import se.diabol.jenkins.pipeline.PipelinePagination;
import se.diabol.jenkins.pipeline.util.FullScreen;

import java.util.List;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public class Component extends AbstractItem {
public class Component extends GenericComponent {

private List<Pipeline> pipelines;
private final String firstJob;
private final String firstJobUrl;
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/se/diabol/jenkins/pipeline/domain/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@
import hudson.model.ItemGroup;
import hudson.model.Result;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.GenericPipeline;
import se.diabol.jenkins.pipeline.domain.task.Task;
import se.diabol.jenkins.pipeline.sort.BuildStartTimeComparator;
import se.diabol.jenkins.pipeline.util.PipelineUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public class Pipeline extends AbstractItem {
public class Pipeline extends GenericPipeline {

private final AbstractProject firstProject;
private final AbstractProject lastProject;
Expand Down Expand Up @@ -170,6 +168,18 @@ public void calculateTotalBuildTime() {
}
}

public long getLastActivity() {
long result = 0;
for (Stage stage : getStages()) {
for (Task task : stage.getTasks()) {
if (task.getStatus().getLastActivity() > result) {
result = task.getStatus().getLastActivity();
}
}
}
return result;
}

private Route createRouteAndCopyTasks(final Route route, Task task) {
Route currentRoute = new Route();
if (route != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/diabol/jenkins/pipeline/domain/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jgrapht.graph.SimpleDirectedGraph;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.pipeline.PipelineProperty;
import se.diabol.jenkins.pipeline.domain.task.Task;
import se.diabol.jenkins.pipeline.util.BuildUtil;
Expand All @@ -47,7 +48,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hudson.model.Run;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.pipeline.CauseResolver;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import hudson.model.User;
import org.kohsuke.stapler.export.Exported;
import se.diabol.jenkins.core.AbstractItem;

import java.util.HashSet;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.core.AbstractItem;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public class Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.core.AbstractItem;

@ExportedBean(defaultVisibility = AbstractItem.VISIBILITY)
public class Running extends SimpleStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.pipeline.domain.status.promotion.AbstractPromotionStatusProvider;
import se.diabol.jenkins.pipeline.domain.status.promotion.PromotionStatus;
import se.diabol.jenkins.pipeline.util.PipelineUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.core.AbstractItem;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import hudson.model.Result;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.pipeline.trigger.ManualTriggerResolver;
import se.diabol.jenkins.pipeline.util.BuildUtil;
import se.diabol.jenkins.pipeline.util.JenkinsUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import jenkins.model.Jenkins;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import se.diabol.jenkins.core.AbstractItem;
import se.diabol.jenkins.pipeline.PipelineProperty;
import se.diabol.jenkins.pipeline.domain.AbstractItem;
import se.diabol.jenkins.pipeline.domain.results.StaticAnalysisResult;
import se.diabol.jenkins.pipeline.domain.results.TestResult;
import se.diabol.jenkins.pipeline.domain.status.SimpleStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import hudson.model.Descriptor;

public abstract class ComponentComparatorDescriptor extends Descriptor<ComponentComparator> {
public abstract class ComponentComparatorDescriptor extends Descriptor<GenericComponentComparator> {

public abstract ComponentComparator createInstance();
public abstract GenericComponentComparator createInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package se.diabol.jenkins.pipeline.sort;

import hudson.Extension;
import se.diabol.jenkins.pipeline.domain.Component;
import se.diabol.jenkins.core.GenericComponent;
import se.diabol.jenkins.core.GenericPipeline;
import se.diabol.jenkins.pipeline.domain.Pipeline;
import se.diabol.jenkins.pipeline.domain.Stage;
import se.diabol.jenkins.pipeline.domain.task.Task;

import java.io.Serializable;

public class FailedJobComparator extends ComponentComparator implements Serializable {
public class FailedJobComparator extends GenericComponentComparator implements Serializable {

@Override
public int compare(Component o1, Component o2) {
public int compare(GenericComponent o1, GenericComponent o2) {
if ((hasFailedJob(firstPipeline(o1)) && (!hasFailedJob(firstPipeline(o2))))) {
return -1;
} else if ((hasFailedJob(firstPipeline(o2)) && (!hasFailedJob(firstPipeline(o1))))) {
Expand All @@ -38,15 +39,28 @@ public int compare(Component o1, Component o2) {
}
}

private Pipeline firstPipeline(Component component) {
private GenericPipeline firstPipeline(GenericComponent component) {
if (component != null && component.getPipelines() != null && !component.getPipelines().isEmpty()) {
return component.getPipelines().get(0);
} else {
return null;
}
}

private boolean hasFailedJob(Pipeline pipeline) {
private boolean hasFailedJob(GenericPipeline pipeline) {
if (pipeline == null) {
return false;
}
if (pipeline instanceof Pipeline) {
return hasFailedJobs((Pipeline) pipeline);
} else if (pipeline instanceof se.diabol.jenkins.workflow.model.Pipeline) {
return hasFailed((se.diabol.jenkins.workflow.model.Pipeline) pipeline);
} else {
throw new IllegalStateException("Unable to resolve pipeline type for " + pipeline);
}
}

private boolean hasFailedJobs(Pipeline pipeline) {
if (pipeline != null) {
for (Stage stage : pipeline.getStages()) {
for (Task task : stage.getTasks()) {
Expand All @@ -59,6 +73,19 @@ private boolean hasFailedJob(Pipeline pipeline) {
return false;
}

private boolean hasFailed(se.diabol.jenkins.workflow.model.Pipeline pipeline) {
if (pipeline != null) {
for (se.diabol.jenkins.workflow.model.Stage stage : pipeline.getStages()) {
for (se.diabol.jenkins.workflow.model.Task task : stage.getTasks()) {
if (task.getStatus().isFailed()) {
return true;
}
}
}
}
return false;
}

@Extension
public static class DescriptorImpl extends ComponentComparatorDescriptor {
@Override
Expand All @@ -67,7 +94,7 @@ public String getDisplayName() {
}

@Override
public ComponentComparator createInstance() {
public GenericComponentComparator createInstance() {
return new FailedJobComparator();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Descriptor;
import se.diabol.jenkins.pipeline.domain.Component;
import se.diabol.jenkins.core.GenericComponent;
import se.diabol.jenkins.pipeline.util.JenkinsUtil;

import java.util.Comparator;

public abstract class ComponentComparator implements Comparator<Component>, ExtensionPoint,
Describable<ComponentComparator> {
public abstract class GenericComponentComparator implements Comparator<GenericComponent>,
ExtensionPoint,
Describable<GenericComponentComparator> {

@Override
public Descriptor<ComponentComparator> getDescriptor() {
public Descriptor<GenericComponentComparator> getDescriptor() {
return (ComponentComparatorDescriptor) JenkinsUtil.getInstance().getDescriptor(getClass());
}

public static DescriptorExtensionList<ComponentComparator, ComponentComparatorDescriptor> all() {
return JenkinsUtil.getInstance().getDescriptorList(ComponentComparator.class);
public static DescriptorExtensionList<GenericComponentComparator, ComponentComparatorDescriptor> all() {
return JenkinsUtil.getInstance().getDescriptorList(GenericComponentComparator.class);
}
}
Loading

0 comments on commit 00ab61d

Please sign in to comment.