Skip to content

Commit

Permalink
Merge branch 'JENKINS-43183' of https://github.com/rafal-prasal/globa…
Browse files Browse the repository at this point in the history
  • Loading branch information
Petkov authored and Petkov committed Oct 7, 2019
2 parents 2dde2f3 + f760664 commit 6c0ab3e
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 35 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>4.9</version>
</dependency>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.0</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WorkflowRun> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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));
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -103,7 +102,7 @@ public void changePluginStateBeforeSavingIt(GlobalBuildStatsPlugin plugin) {
handleItem(jobBuildResultsRead,i);
}
}
if (item instanceof AbstractProject) {
if (item instanceof Job) {
handleItem(jobBuildResultsRead, item);
}
}
Expand All @@ -115,8 +114,8 @@ public void changePluginStateBeforeSavingIt(GlobalBuildStatsPlugin plugin) {
}

public void handleItem(List<JobBuildResult> results, TopLevelItem item){
if (item instanceof AbstractProject){
addBuildsFrom(results, (AbstractProject)item);
if (item instanceof Job){
addBuildsFrom(results, (Job)item);
}
}

Expand Down Expand Up @@ -366,13 +365,13 @@ public List<AbstractBuildStatChartDimension> createDataSetBuilder(BuildStatConfi
return dimensions;
}

private static void addBuild(List<JobBuildResult> jobBuildResultsRead, AbstractBuild build){
private static void addBuild(List<JobBuildResult> jobBuildResultsRead, Run build){
jobBuildResultsRead.add(JobBuildResultFactory.INSTANCE.createJobBuildResult(build));
}

private static void addBuildsFrom(List<JobBuildResult> jobBuildResultsRead, AbstractProject project){
List<AbstractBuild> builds = project.getBuilds();
Iterator<AbstractBuild> buildIterator = builds.iterator();
private static void addBuildsFrom(List<JobBuildResult> jobBuildResultsRead, Job project){
List<Run> builds = (List<Run>)project.getBuilds();
Iterator<Run> buildIterator = builds.iterator();

while (buildIterator.hasNext()) {
addBuild(jobBuildResultsRead, buildIterator.next());
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 6c0ab3e

Please sign in to comment.