Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-21971] Added a default combination filter.
  • Loading branch information
ikedam committed Mar 1, 2014
1 parent 23be256 commit f50dad9
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 13 deletions.
@@ -0,0 +1,98 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.plugins.matrix_configuration_parameter;

import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.model.AbstractBuild;
import hudson.util.VariableResolver;

/**
* {@link MatrixCombinationsParameterValue} created when the build started
* without specifying parameter.
*
* {@link MatrixCombinationsParameterValue#getConfs()} and {@link MatrixCombinationsParameterValue#getValues()}
* does not work (always return an empty array).
*/
public class DefaultMatrixCombinationsParameterValue extends MatrixCombinationsParameterValue {
private static final long serialVersionUID = -812826069693143705L;
private final String combinationFilter;

public DefaultMatrixCombinationsParameterValue(String name, String description, String combinationFilter) {
super(name, new Boolean[0], new String[0], description);
this.combinationFilter = combinationFilter;
}

@Override
public VariableResolver<String> createVariableResolver(AbstractBuild<?, ?> build) {
return new VariableResolver<String>() {
public String resolve(String name) {
if (!DefaultMatrixCombinationsParameterValue.this.name.equals(name)) {
return null;
}
return (combinationFilter != null)?combinationFilter:"";
}
};
}

/**
* @param axes
* @param c
* @return true if that combination should be built.
* @see hudson.plugins.matrix_configuration_parameter.MatrixCombinationsParameterValue#combinationExists(hudson.matrix.AxisList, hudson.matrix.Combination)
*/
@Override
public boolean combinationExists(AxisList axes, Combination c){
if (axes == null || combinationFilter == null) {
// when axes is null, the combination filter cannot be evaluated
// when combination filter is null, allow all combination.
return true;
}
return c.evalGroovyExpression(axes, combinationFilter);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(getClass().equals(obj.getClass()))) {
return false;
}
DefaultMatrixCombinationsParameterValue other = (DefaultMatrixCombinationsParameterValue)obj;

if (combinationFilter == null) {
return other.combinationFilter == null;
}

return combinationFilter.equals(other.combinationFilter);
}

@Override
public String toString() {
return String.format("(%s) %s: %s",
getClass().getName(), getName(), combinationFilter);
}
}
Expand Up @@ -29,21 +29,32 @@

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;


public class MatrixCombinationsParameterDefinition extends ParameterDefinition {

private static final long serialVersionUID = 1L;
private final String defaultCombinationFilter;


/**
* @return groovy expression to specify default checked combinations
*/
public String getDefaultCombinationFilter() {
return defaultCombinationFilter;
}

@DataBoundConstructor
public MatrixCombinationsParameterDefinition(String name, String description) {
public MatrixCombinationsParameterDefinition(String name, String description, String defaultCombinationFilter) {
super(name, description);
this.defaultCombinationFilter = !StringUtils.isBlank(defaultCombinationFilter)?defaultCombinationFilter:null;
}

public MatrixCombinationsParameterDefinition(String name, String description) {
this(name, description, null);
}

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
Expand All @@ -67,7 +78,7 @@ public ParameterValue createValue(StaplerRequest req) {

@Override
public MatrixCombinationsParameterValue getDefaultParameterValue() {
MatrixCombinationsParameterValue v = new MatrixCombinationsParameterValue(getName(), new Boolean[]{},new String[]{});
MatrixCombinationsParameterValue v = new DefaultMatrixCombinationsParameterValue(getName(), getDescription(), getDefaultCombinationFilter());
return v;
}

Expand Down
Expand Up @@ -54,7 +54,7 @@ public boolean doBuildConfiguration(MatrixBuild b, MatrixConfiguration c) {
}
for (MatrixCombinationsParameterValue value
:Util.filter(paction.getParameters(), MatrixCombinationsParameterValue.class)) {
if (!value.combinationExists(c.getCombination())) {
if (!value.combinationExists(b.getParent().getAxes(), c.getCombination())) {
return false;
}
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package hudson.plugins.matrix_configuration_parameter;

import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.model.*;

Expand Down Expand Up @@ -85,7 +86,7 @@ public String resolve(String name) {
}
};
}
public boolean combinationExists(Combination c){
public boolean combinationExists(AxisList axes, Combination c){


if (values == null || confs == null || values.length != confs.length)
Expand All @@ -97,6 +98,10 @@ public boolean combinationExists(Combination c){
}
return false;
}
@Deprecated
public boolean combinationExists(Combination c){
return combinationExists(null, c);
}

@Override
public int hashCode() {
Expand Down
Expand Up @@ -31,4 +31,9 @@ THE SOFTWARE.
<f:entry title="${%Description}" help="/help/parameter/description.html">
<f:textarea field="description" />
</f:entry>
<f:advanced>
<f:entry field="defaultCombinationFilter" title="${%Default Filter}">
<f:textbox />
</f:entry>
</f:advanced>
</j:jelly>
Expand Up @@ -7,6 +7,7 @@ import hudson.matrix.MatrixBuild
import hudson.matrix.MatrixProject
import lib.LayoutTagLib
import org.kohsuke.stapler.jelly.groovy.Namespace
import hudson.plugins.matrix_configuration_parameter.MatrixCombinationsParameterDefinition;

l = namespace(LayoutTagLib)
t = namespace("/lib/hudson")
Expand All @@ -20,6 +21,7 @@ if (project == null) //in case project is not a Matrix Project
return;

AxisList axes = project.getAxes();
def paramDef = it;
String nameIt = it.getName();
Layouter layouter = axes == null ? null : new Layouter<Combination>(axes) {
protected Combination getT(Combination c) {
Expand All @@ -31,9 +33,9 @@ Layouter layouter = axes == null ? null : new Layouter<Combination>(axes) {



drawMainBody(f, nameIt, axes, project, layouter)
drawMainBody(paramDef, f, nameIt, axes, project, layouter)

private void drawMainBody(Namespace f, String nameIt, AxisList axes,MatrixProject project,Layouter layouter) {
private void drawMainBody(MatrixCombinationsParameterDefinition paramDef, Namespace f, String nameIt, AxisList axes,MatrixProject project,Layouter layouter) {

drawMainLinksJS(nameIt)

Expand All @@ -42,7 +44,7 @@ private void drawMainBody(Namespace f, String nameIt, AxisList axes,MatrixProjec
div(name: "parameter") {
input(type: "hidden", name: "name", value: nameIt)
nsProject.matrix(it: project) {
drawMainBall(p.combination, project.axes, nameIt, project, layouter);
drawMainBall(paramDef, p.combination, project.axes, nameIt, project, layouter);
}
raw("<span style=\"font-weight:bold\">Select: </span> \n" +
"<a href=\"#\" onclick=\"click2Change(0);\">Successful</a> - \n" +
Expand Down Expand Up @@ -80,7 +82,7 @@ private void drawMainLinksJS(String nameIt) {
"</script>\n")
}

private void drawMainBall(Combination combination,AxisList axes,String matrixName,MatrixProject project,Layouter layouter) {
private void drawMainBall(MatrixCombinationsParameterDefinition paramDef, Combination combination,AxisList axes,String matrixName,MatrixProject project,Layouter layouter) {

lastBuild = project.getLastBuild();
if (lastBuild != null && lastBuild.getRun(combination)!=null){
Expand All @@ -92,7 +94,7 @@ private void drawMainBall(Combination combination,AxisList axes,String matrixNam
text(combination.toString(layouter.z))
}
}
checked = combination.evalGroovyExpression(axes, project.combinationFilter)
checked = combination.evalGroovyExpression(axes, paramDef.defaultCombinationFilter?:project.combinationFilter)
f.checkbox(checked: checked, name: "values",id: "checkbox"+matrixName)
input(type: "hidden", name: "confs", value: combination.toString())

Expand All @@ -104,7 +106,7 @@ private void drawMainBall(Combination combination,AxisList axes,String matrixNam
text(combination.toString(layouter.z))
}

checked = combination.evalGroovyExpression(axes, project.combinationFilter)
checked = combination.evalGroovyExpression(axes, paramDef.defaultCombinationFilter?:project.combinationFilter)
f.checkbox(checked: checked, name: "values",id: "checkbox"+matrixName, value: combination.toIndex((AxisList) axes))
input(type: "hidden", name: "confs", value: combination.toString())
}
Expand Down
Expand Up @@ -45,7 +45,7 @@ private void drawParameterBody(Namespace f,valueIt,AxisList axes,MatrixProject p
private void drawTableBall(MatrixBuild.RunPtr runPtr,AxisList axes,matrixValue,MatrixProject project,Layouter layouter) {

run = runPtr.getRun();
result = matrixValue.combinationExists(runPtr.combination);
result = matrixValue.combinationExists(axes, runPtr.combination);
if (result){
a(href:request.getRootPath()+"/"+run.getUrl()){
img(src: "${imagesURL}/24x24/"+run.getBuildStatusUrl());
Expand Down
Expand Up @@ -97,7 +97,7 @@ private void drawTableHeader(Layouter layouter) {
private void drawTableBall(MatrixBuild.RunPtr runPtr,AxisList axes,MatrixCombinationsParameterValue matrixValue,MatrixProject project) {

run = runPtr.getRun();
result = matrixValue.combinationExists(runPtr.combination);
result = matrixValue.combinationExists(axes, runPtr.combination);
if (result){
a(href:request.getRootPath()+"/"+run.getUrl()){
img(src: "${imagesURL}/24x24/"+run.getBuildStatusUrl());
Expand Down

0 comments on commit f50dad9

Please sign in to comment.