diff --git a/pom.xml b/pom.xml index 44774c5..4775b50 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,12 @@ org.jenkins-ci.plugins cloudbees-folder 4.9 - + + + org.jenkins-ci.plugins.workflow + workflow-job + 2.0 + diff --git a/src/main/java/hudson/plugins/global_build_stats/GlobalBuildStatsPlugin.java b/src/main/java/hudson/plugins/global_build_stats/GlobalBuildStatsPlugin.java index 00c4822..a70e882 100644 --- a/src/main/java/hudson/plugins/global_build_stats/GlobalBuildStatsPlugin.java +++ b/src/main/java/hudson/plugins/global_build_stats/GlobalBuildStatsPlugin.java @@ -17,6 +17,7 @@ import hudson.util.ChartUtil; import hudson.util.FormValidation; import java.io.File; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; import java.io.IOException; import java.util.ArrayList; @@ -256,6 +257,31 @@ public void onDeleted(AbstractBuild build) { getPluginBusiness().onBuildDeleted(build); } } + + /** + * At the end of every pipeline job, let's gather job result informations into global build stats + * persisted data + */ + + @Extension + public static class GlobalBuildStatsWorkflowRunListener extends RunListener { + public GlobalBuildStatsWorkflowRunListener() { + super(WorkflowRun.class); + } + + @Override + public void onCompleted(WorkflowRun w, TaskListener l) { + super.onCompleted(w, l); + + getPluginBusiness().onJobCompleted(w); + } + + @Override + public void onDeleted(WorkflowRun w) { + super.onDeleted(w); + getPluginBusiness().onBuildDeleted(w); + } + } public static GlobalBuildStatsBusiness getPluginBusiness() { // Retrieving global build stats plugin & adding build result to the registered build diff --git a/src/main/java/hudson/plugins/global_build_stats/JobBuildResultFactory.java b/src/main/java/hudson/plugins/global_build_stats/JobBuildResultFactory.java index 550c7a4..328a3b0 100644 --- a/src/main/java/hudson/plugins/global_build_stats/JobBuildResultFactory.java +++ b/src/main/java/hudson/plugins/global_build_stats/JobBuildResultFactory.java @@ -5,6 +5,7 @@ import hudson.plugins.global_build_stats.model.JobBuildResult; import hudson.plugins.global_build_stats.model.JobBuildSearchResult; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; public class JobBuildResultFactory { @@ -15,19 +16,31 @@ public class JobBuildResultFactory { private JobBuildResultFactory(){ } - public JobBuildResult createJobBuildResult(AbstractBuild build){ - String buildName = build.getProject().getFullName(); - long duration = build.getDuration(); - String nodeName = build.getBuiltOnStr(); - /* Can't do that since MavenModuleSet is in maven-plugin artefact which is in test scope - if(build.getProject() instanceof MavenModuleSet){ - buildName = ((MavenModuleSet)build.getProject()).getRootModule().toString(); - }*/ - return new JobBuildResult(createBuildResult(build.getResult()), buildName, - build.getNumber(), build.getTimestamp(), duration, nodeName, extractUserNameIn(build)); + public JobBuildResult createJobBuildResult(Run r){ + String builtOn = "pipeline"; + String name = ""; + + if (r instanceof AbstractBuild) { + builtOn = ((AbstractBuild)r).getBuiltOnStr(); + name = ((AbstractBuild)r).getProject().getFullName(); + } + + if (r instanceof WorkflowRun) { + name = ((WorkflowRun)r).getFullDisplayName().replace(" ยป " ,"/").replaceAll(" #[1-9]+$",""); + } + + return new JobBuildResult( + createBuildResult(r.getResult()), + name, + r.getNumber(), + r.getTimestamp(), + r.getDuration(), + builtOn, + extractUserNameIn(r) + ); } - public JobBuildSearchResult createJobBuildSearchResult(AbstractBuild build){ + public JobBuildSearchResult createJobBuildSearchResult(Run build){ return createJobBuildSearchResult(createJobBuildResult(build)); } @@ -49,7 +62,7 @@ public JobBuildSearchResult createJobBuildSearchResult(JobBuildResult r){ return new JobBuildSearchResult(r, isJobAccessible, isBuildAccessible); } - public static String extractUserNameIn(AbstractBuild build){ + public static String extractUserNameIn(Run build){ String userName; @SuppressWarnings("deprecation") Cause.UserCause uc = build.getCause(Cause.UserCause.class); Cause.UserIdCause uic = build.getCause(Cause.UserIdCause.class); diff --git a/src/main/java/hudson/plugins/global_build_stats/business/GlobalBuildStatsBusiness.java b/src/main/java/hudson/plugins/global_build_stats/business/GlobalBuildStatsBusiness.java index e973adb..10dff89 100644 --- a/src/main/java/hudson/plugins/global_build_stats/business/GlobalBuildStatsBusiness.java +++ b/src/main/java/hudson/plugins/global_build_stats/business/GlobalBuildStatsBusiness.java @@ -1,11 +1,10 @@ package hudson.plugins.global_build_stats.business; import hudson.model.TopLevelItem; -import hudson.model.AbstractBuild; -import hudson.model.AbstractProject; import hudson.model.Hudson; import hudson.model.Item; import hudson.model.Job; +import hudson.model.Run; import hudson.plugins.global_build_stats.GlobalBuildStatsPlugin; import hudson.plugins.global_build_stats.JobBuildResultFactory; import hudson.plugins.global_build_stats.model.*; @@ -52,7 +51,7 @@ public GlobalBuildStatsBusiness(GlobalBuildStatsPlugin _plugin){ /** * Records the result of a build. */ - public void onJobCompleted(final AbstractBuild build) { + public void onJobCompleted(final Run build) { for(RetentionStrategy s : plugin.getRetentionStrategies()){ s.onBuildCompleted(build, pluginSaver); } @@ -103,7 +102,7 @@ public void changePluginStateBeforeSavingIt(GlobalBuildStatsPlugin plugin) { handleItem(jobBuildResultsRead,i); } } - if (item instanceof AbstractProject) { + if (item instanceof Job) { handleItem(jobBuildResultsRead, item); } } @@ -115,8 +114,8 @@ public void changePluginStateBeforeSavingIt(GlobalBuildStatsPlugin plugin) { } public void handleItem(List results, TopLevelItem item){ - if (item instanceof AbstractProject){ - addBuildsFrom(results, (AbstractProject)item); + if (item instanceof Job){ + addBuildsFrom(results, (Job)item); } } @@ -366,13 +365,13 @@ public List createDataSetBuilder(BuildStatConfi return dimensions; } - private static void addBuild(List jobBuildResultsRead, AbstractBuild build){ + private static void addBuild(List jobBuildResultsRead, Run build){ jobBuildResultsRead.add(JobBuildResultFactory.INSTANCE.createJobBuildResult(build)); } - private static void addBuildsFrom(List jobBuildResultsRead, AbstractProject project){ - List builds = project.getBuilds(); - Iterator buildIterator = builds.iterator(); + private static void addBuildsFrom(List jobBuildResultsRead, Job project){ + List builds = (List)project.getBuilds(); + Iterator buildIterator = builds.iterator(); while (buildIterator.hasNext()) { addBuild(jobBuildResultsRead, buildIterator.next()); @@ -404,7 +403,7 @@ public void reloadPlugin() { } } - public void onBuildDeleted(AbstractBuild build) { + public void onBuildDeleted(Run build) { for(RetentionStrategy s : plugin.getRetentionStrategies()){ s.onBuildDeleted(build, pluginSaver); } diff --git a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DiscardResultsOlderThanDays.java b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DiscardResultsOlderThanDays.java index 2e772b7..ef0165a 100644 --- a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DiscardResultsOlderThanDays.java +++ b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DiscardResultsOlderThanDays.java @@ -1,6 +1,6 @@ package hudson.plugins.global_build_stats.rententionstrategies; -import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.plugins.global_build_stats.GlobalBuildStatsPlugin; import hudson.plugins.global_build_stats.JobBuildResultFactory; import hudson.plugins.global_build_stats.business.GlobalBuildStatsPluginSaver; @@ -47,7 +47,7 @@ public void strategyActivated(GlobalBuildStatsPluginSaver pluginSaver) { purgeOldBuildResults(pluginSaver, System.currentTimeMillis()); } - public void buildCompleted(AbstractBuild buils, GlobalBuildStatsPluginSaver pluginSaver) { + public void buildCompleted(Run build, GlobalBuildStatsPluginSaver pluginSaver) { final long now = System.currentTimeMillis(); if(lastPurgeDate == null || now > lastPurgeDate.getTime() + PURGE_FREQUENCY){ purgeOldBuildResults(pluginSaver, now); diff --git a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DoNotKeepBuildResultWhenDiscarded.java b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DoNotKeepBuildResultWhenDiscarded.java index 6e7f59a..ee98b93 100644 --- a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DoNotKeepBuildResultWhenDiscarded.java +++ b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/DoNotKeepBuildResultWhenDiscarded.java @@ -1,6 +1,6 @@ package hudson.plugins.global_build_stats.rententionstrategies; -import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.plugins.global_build_stats.GlobalBuildStatsPlugin; import hudson.plugins.global_build_stats.JobBuildResultFactory; import hudson.plugins.global_build_stats.business.GlobalBuildStatsPluginSaver; @@ -18,7 +18,7 @@ public String getConfigPage() { return "doNotKeepBuildResultWhenDiscarded.jelly"; } - public void buildDeleted(final AbstractBuild build, GlobalBuildStatsPluginSaver pluginSaver) { + public void buildDeleted(final Run build, GlobalBuildStatsPluginSaver pluginSaver) { pluginSaver.updatePlugin(new GlobalBuildStatsPluginSaver.BeforeSavePluginCallback() { @Override public void changePluginStateBeforeSavingIt(GlobalBuildStatsPlugin plugin) { diff --git a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/RetentionStrategy.java b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/RetentionStrategy.java index 3527455..62573eb 100644 --- a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/RetentionStrategy.java +++ b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/RetentionStrategy.java @@ -1,6 +1,6 @@ package hudson.plugins.global_build_stats.rententionstrategies; -import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.plugins.global_build_stats.business.GlobalBuildStatsPluginSaver; import hudson.plugins.global_build_stats.rententionstrategies.strategybehaviours.BuildCompletedListener; import hudson.plugins.global_build_stats.rententionstrategies.strategybehaviours.BuildDeletedListener; @@ -52,14 +52,14 @@ public void from(T strategyToCopy) { } // Overridable if retention strategy is a build deleted listener - public void onBuildDeleted(AbstractBuild build, GlobalBuildStatsPluginSaver pluginSaver) { + public void onBuildDeleted(Run build, GlobalBuildStatsPluginSaver pluginSaver) { if(this instanceof BuildDeletedListener){ ((BuildDeletedListener)this).buildDeleted(build, pluginSaver); } } // Overridable if retention strategy is a build completed listener - public void onBuildCompleted(AbstractBuild build, GlobalBuildStatsPluginSaver pluginSaver) { + public void onBuildCompleted(Run build, GlobalBuildStatsPluginSaver pluginSaver) { if(this instanceof BuildCompletedListener){ ((BuildCompletedListener)this).buildCompleted(build, pluginSaver); } diff --git a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildCompletedListener.java b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildCompletedListener.java index 0700310..da995b1 100644 --- a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildCompletedListener.java +++ b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildCompletedListener.java @@ -1,11 +1,11 @@ package hudson.plugins.global_build_stats.rententionstrategies.strategybehaviours; -import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.plugins.global_build_stats.business.GlobalBuildStatsPluginSaver; /** * @author fcamblor */ public interface BuildCompletedListener { - public void buildCompleted(AbstractBuild buils, GlobalBuildStatsPluginSaver pluginSaver); + public void buildCompleted(Run buils, GlobalBuildStatsPluginSaver pluginSaver); } diff --git a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildDeletedListener.java b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildDeletedListener.java index 61ee567..56c532f 100644 --- a/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildDeletedListener.java +++ b/src/main/java/hudson/plugins/global_build_stats/rententionstrategies/strategybehaviours/BuildDeletedListener.java @@ -1,11 +1,11 @@ package hudson.plugins.global_build_stats.rententionstrategies.strategybehaviours; -import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.plugins.global_build_stats.business.GlobalBuildStatsPluginSaver; /** * @author fcamblor */ public interface BuildDeletedListener { - public void buildDeleted(AbstractBuild buils, GlobalBuildStatsPluginSaver pluginSaver); + public void buildDeleted(Run buils, GlobalBuildStatsPluginSaver pluginSaver); }