Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-7280 - Allow filtering of the Run Paramters List - All,
Completed, Successful & Stable using Enum
  • Loading branch information
gcummings committed May 23, 2013
1 parent 0e0ef4c commit b1415bd
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 12 deletions.
85 changes: 80 additions & 5 deletions core/src/main/java/hudson/model/RunParameterDefinition.java
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, Geoff Cummings
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,31 +30,63 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import hudson.Extension;
import hudson.util.EnumConverter;
import hudson.util.RunList;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.QueryParameter;

public class RunParameterDefinition extends SimpleParameterDefinition {

/**
* Constants that control how Run Parameter is filtered
*/
public enum RunParameterFilter {
ALL,
STABLE,
SUCCESSFUL,
COMPLETED;

public String getName() {
return name();
}

static {
Stapler.CONVERT_UTILS.register(new EnumConverter(), RunParameterFilter.class);
}
}

private final String projectName;
private final String runId;
private final RunParameterFilter filter;

@DataBoundConstructor
public RunParameterDefinition(String name, String projectName, String description) {
public RunParameterDefinition(String name, String projectName, String description, RunParameterFilter filter) {
super(name, description);
this.projectName = projectName;
this.runId = null;
this.filter = filter;
}

/**
* @deprecated as of 1.517
*/
public RunParameterDefinition(String name, String projectName, String description) {
// delegate to updated constructor with additional RunParameterFilter parameter defaulted to ALL.
this(name, projectName, description, RunParameterFilter.ALL);
}

private RunParameterDefinition(String name, String projectName, String runId, String description) {
private RunParameterDefinition(String name, String projectName, String runId, String description, RunParameterFilter filter) {
super(name, description);
this.projectName = projectName;
this.runId = runId;
this.filter = filter;
}

@Override
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
if (defaultValue instanceof RunParameterValue) {
RunParameterValue value = (RunParameterValue) defaultValue;
return new RunParameterDefinition(getName(), value.getRunId(), getDescription());
return new RunParameterDefinition(getName(), value.getRunId(), getDescription(), getFilter());
} else {
return this;
}
Expand All @@ -69,6 +101,32 @@ public Job getProject() {
return Jenkins.getInstance().getItemByFullName(projectName, Job.class);
}

/**
* @return The current filter value, if filter is null, returns ALL
*/
public RunParameterFilter getFilter() {
// if filter is null, default to RunParameterFilter.ALL
return (null == filter) ? RunParameterFilter.ALL : filter;
}

/**
* @since 1.517
* @return Returns a list of builds, filtered based on the filter value.
*/
public RunList getBuilds() {
// use getFilter() method so we dont have to worry about null filter value.
switch (getFilter()) {
case COMPLETED:
return getProject().getBuilds().overThresholdOnly(Result.ABORTED);
case SUCCESSFUL:
return getProject().getBuilds().overThresholdOnly(Result.UNSTABLE);
case STABLE :
return getProject().getBuilds().overThresholdOnly(Result.SUCCESS);
default:
return getProject().getBuilds();
}
}

@Extension
public static class DescriptorImpl extends ParameterDescriptor {
@Override
Expand Down Expand Up @@ -98,7 +156,24 @@ public ParameterValue getDefaultParameterValue() {
return createValue(runId);
}

Run<?,?> lastBuild = getProject().getLastBuild();
Run<?,?> lastBuild = null;

// use getFilter() so we dont have to worry about null filter value.
switch (getFilter()) {
case COMPLETED:
lastBuild = getProject().getLastCompletedBuild();
break;
case SUCCESSFUL:
lastBuild = getProject().getLastSuccessfulBuild();
break;
case STABLE :
lastBuild = getProject().getLastStableBuild();
break;
default:
lastBuild = getProject().getLastBuild();
break;
}

if (lastBuild != null) {
return createValue(lastBuild.getExternalizableId());
} else {
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/model/RunParameterValue.java
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, Geoff Cummings
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -82,6 +82,9 @@ public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {

env.put(name + "_NAME", run.getDisplayName()); // since 1.504

String buildResult = (null == run.getResult()) ? "UNKNOWN" : run.getResult().toString();
env.put(name + "_RESULT", buildResult); // since 1.517

env.put(name.toUpperCase(Locale.ENGLISH),value); // backward compatibility pre 1.345

}
Expand Down
15 changes: 14 additions & 1 deletion core/src/main/java/hudson/util/RunList.java
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Geoff Cummings
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -240,6 +240,19 @@ public boolean apply(R r) {
});
}

/**
* Filter the list to builds >= threshold.
*
* @since 1.516
*/
public RunList<R> overThresholdOnly(final Result threshold) {
return filter(new Predicate<R>() {
public boolean apply(R r) {
return (r.getResult() != null && r.getResult().isBetterOrEqualTo(threshold));
}
});
}

/**
* Filter the list to builds on a single node only
*/
Expand Down
Expand Up @@ -35,4 +35,12 @@ THE SOFTWARE.
<f:entry title="${%Description}" help="/help/parameter/description.html">
<f:textarea name="parameter.description" value="${instance.description}" />
</f:entry>
<f:entry name="filter" title="${%Filter}" help="/help/parameter/run-filter.html">
<select name="filter">
<f:option selected="${instance.filter=='ALL'}" value="ALL">${%All Builds}</f:option>
<f:option selected="${instance.filter=='COMPLETED'}" value="COMPLETED">${%Completed Builds Only}</f:option>
<f:option selected="${instance.filter=='SUCCESSFUL'}" value="SUCCESSFUL">${%Successful Builds Only}</f:option>
<f:option selected="${instance.filter=='STABLE'}" value="STABLE">${%Stable Builds Only}</f:option>
</select>
</f:entry>
</j:jelly>
Expand Up @@ -30,7 +30,7 @@ THE SOFTWARE.
<div name="parameter" description="${it.description}">
<input type="hidden" name="name" value="${it.name}" />
<select name="runId">
<j:forEach var="run" items="${it.project.builds}">
<j:forEach var="run" items="${it.builds}">
<option value="${run.externalizableId}">${run}</option>
</j:forEach>
</select>
Expand Down
2 changes: 1 addition & 1 deletion test/src/test/java/hudson/model/ParametersTest.java
Expand Up @@ -24,7 +24,7 @@ public void testParameterTypes() throws Exception {
new StringParameterDefinition("string", "defaultValue", "string description"),
new BooleanParameterDefinition("boolean", true, "boolean description"),
new ChoiceParameterDefinition("choice", "Choice 1\nChoice 2", "choice description"),
new RunParameterDefinition("run", otherProject.getName(), "run description"));
new RunParameterDefinition("run", otherProject.getName(), "run description", null));
project.addProperty(pdp);
CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
project.getBuildersList().add(builder);
Expand Down

0 comments on commit b1415bd

Please sign in to comment.