From 395adfffc39eeaa52044fee7afd6f1ea55c03bd1 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 24 Jan 2009 22:04:35 +0000 Subject: [PATCH] [FIXED HUDSON-2813] Applied a change with a bit of modifications. This will be 1.279 git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@14777 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/matrix/Combination.java | 39 +++++++++ .../java/hudson/matrix/MatrixProject.java | 82 +++++++++++++++---- .../MatrixProject/configure-entries.jelly | 8 ++ .../configure-entries_tr.properties | 2 + .../java/hudson/matrix/CombinationTest.java | 26 ++++++ .../help/matrix/combinationfilter.html | 41 ++++++++++ 6 files changed, 183 insertions(+), 15 deletions(-) create mode 100644 core/src/test/java/hudson/matrix/CombinationTest.java create mode 100644 war/resources/help/matrix/combinationfilter.html diff --git a/core/src/main/java/hudson/matrix/Combination.java b/core/src/main/java/hudson/matrix/Combination.java index ef013497ff..baa1df17e4 100644 --- a/core/src/main/java/hudson/matrix/Combination.java +++ b/core/src/main/java/hudson/matrix/Combination.java @@ -13,6 +13,10 @@ import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; +import static java.lang.Boolean.TRUE; + +import groovy.lang.Binding; +import groovy.lang.GroovyShell; /** * A particular combination of {@link Axis} values. @@ -38,6 +42,27 @@ public final class Combination extends TreeMap implements Compara super.put(e.getKey(),e.getValue()); } + /** + * Evaluates the given Groovy expression with values bound from this combination. + * + *

+ * For example, if this combination is a=X,b=Y, then expressions like a=="X" would evaluate to + * true. + */ + public boolean evalGroovyExpression(String expression) { + if(Util.fixEmptyAndTrim(expression)==null) + return true; + + Binding binding = new Binding(); + for (Map.Entry e : entrySet()) + binding.setVariable(e.getKey(),e.getValue()); + + GroovyShell shell = new GroovyShell(binding); + + Object result = shell.evaluate("use("+BooleanCategory.class.getName().replace('$','.')+") {"+expression+"}"); + return TRUE.equals(result); + } + public int compareTo(Combination that) { int d = this.size()-that.size(); if(d!=0) return d; @@ -170,4 +195,18 @@ public final class Combination extends TreeMap implements Compara public String remove(Object key) { throw new UnsupportedOperationException(); } + + /** + * Duck-typing for boolean expressions. + * + * @see Combination#evalGroovyExpression(String) + */ + public static final class BooleanCategory { + /** + * x -> y + */ + public static Boolean implies(Boolean lhs, Boolean rhs) { + return !lhs || rhs; + } + } } diff --git a/core/src/main/java/hudson/matrix/MatrixProject.java b/core/src/main/java/hudson/matrix/MatrixProject.java index 207cc191ef..45d6c50958 100644 --- a/core/src/main/java/hudson/matrix/MatrixProject.java +++ b/core/src/main/java/hudson/matrix/MatrixProject.java @@ -1,13 +1,15 @@ package hudson.matrix; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; import hudson.CopyOnWrite; import hudson.FilePath; import hudson.XmlFile; +import hudson.Util; import hudson.model.AbstractProject; import hudson.model.Action; import hudson.model.DependencyGraph; import hudson.model.Descriptor; -import hudson.model.Descriptor.FormException; import hudson.model.Hudson; import hudson.model.Item; import hudson.model.ItemGroup; @@ -17,9 +19,10 @@ import hudson.model.Job; import hudson.model.Label; import hudson.model.Node; import hudson.model.SCMedItem; +import hudson.model.Saveable; import hudson.model.TopLevelItem; import hudson.model.TopLevelItemDescriptor; -import hudson.model.Saveable; +import hudson.model.Descriptor.FormException; import hudson.tasks.BuildStep; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildWrapper; @@ -29,11 +32,7 @@ import hudson.tasks.Publisher; import hudson.triggers.Trigger; import hudson.util.CopyOnWriteMap; import hudson.util.DescribableList; -import net.sf.json.JSONObject; -import org.kohsuke.stapler.StaplerRequest; -import org.kohsuke.stapler.StaplerResponse; -import javax.servlet.ServletException; import java.io.File; import java.io.FileFilter; import java.io.IOException; @@ -46,11 +45,19 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; +import javax.servlet.ServletException; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringEscapeUtils; +import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerResponse; + /** * {@link Job} that allows you to run multiple different configurations * from a single setting. @@ -64,6 +71,14 @@ public class MatrixProject extends AbstractProject im * This also includes special axis "label" and "jdk" if they are configured. */ private volatile AxisList axes = new AxisList(); + + /** + * The filter that is applied to combinatios. It is a Groovy if condition. + * This can be null, which means "true". + * + * @see #getCombinationFilter() + */ + private volatile String combinationFilter; /** * List of active {@link Builder}s configured for this project. @@ -111,7 +126,36 @@ public class MatrixProject extends AbstractProject im save(); } - protected void updateTransientActions() { + /** + * Sets the combination filter. + * + * @param combinationFilter the combinationFilter to set + */ + public void setCombinationFilter(String combinationFilter) throws IOException { + this.combinationFilter = combinationFilter; + rebuildConfigurations(); + save(); + } + + /** + * Obtains the combination filter, used to trim down the size of the matrix. + * + *

+ * By default, a {@link MatrixConfiguration} is created for every possible combination of axes exhaustively. + * But by specifying a Groovy expression as a combination filter, one can trim down the # of combinations built. + * + *

+ * Namely, this expression is evaluated for each axis value combination, and only when it evaluates to true, + * a corresponding {@link MatrixConfiguration} will be created and built. + * + * @return can be null. + * @since 1.279 + */ + public String getCombinationFilter() { + return combinationFilter; + } + + protected void updateTransientActions() { synchronized(transientActions) { super.updateTransientActions(); @@ -272,13 +316,16 @@ public class MatrixProject extends AbstractProject im // find all active configurations Set active = new LinkedHashSet(); for (Combination c : axes.list()) { - MatrixConfiguration config = configurations.get(c); - if(config==null) { - config = new MatrixConfiguration(this,c); - config.save(); - configurations.put(config.getCombination(), config); - } - active.add(config); + if(c.evalGroovyExpression(combinationFilter)) { + LOGGER.fine("Adding configuration: " + c); + MatrixConfiguration config = configurations.get(c); + if(config==null) { + config = new MatrixConfiguration(this,c); + config.save(); + configurations.put(config.getCombination(), config); + } + active.add(config); + } } this.activeConfigurations = active; } @@ -452,6 +499,11 @@ public class MatrixProject extends AbstractProject im if(a.values.isEmpty()) itr.remove(); } } + + if(req.getParameter("hasCombinationFilter")!=null) + this.combinationFilter = Util.nullify(req.getParameter("combinationFilter")); + else + this.combinationFilter = null; // parse system axes newAxes.add(Axis.parsePrefixed(req,"jdk")); diff --git a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly index ea1b5869cd..da7284fd8c 100644 --- a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly +++ b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly @@ -81,6 +81,14 @@ + + + + + + + diff --git a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries_tr.properties b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries_tr.properties index bff5f9ee14..1af6e2bd8d 100644 --- a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries_tr.properties +++ b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries_tr.properties @@ -9,3 +9,5 @@ Delete=Sil Individual\ nodes=Tek nodlar Labels=Etiketler Axes=Eksenler +Filter\ Combinations=Kombinasyonlari Filtrele +Filter=Filtre \ No newline at end of file diff --git a/core/src/test/java/hudson/matrix/CombinationTest.java b/core/src/test/java/hudson/matrix/CombinationTest.java new file mode 100644 index 0000000000..e4ba4218bf --- /dev/null +++ b/core/src/test/java/hudson/matrix/CombinationTest.java @@ -0,0 +1,26 @@ +package hudson.matrix; + +import junit.framework.TestCase; + +import java.util.Map; +import java.util.HashMap; + +/** + * @author Kohsuke Kawaguchi + */ +public class CombinationTest extends TestCase { + @SuppressWarnings({"RedundantStringConstructorCall"}) + public void testEval() { + Map r = new HashMap(); + r.put("a","X"); + r.put("b",new String("Y")); // make sure this 'Y' is not the same object as literal "Y". + Combination c = new Combination(r); + assertTrue(c.evalGroovyExpression(null)); + assertTrue(c.evalGroovyExpression(" ")); + assertTrue(c.evalGroovyExpression("true")); + assertTrue(c.evalGroovyExpression("a=='X'")); + assertTrue(c.evalGroovyExpression("b=='Y'")); + assertTrue(c.evalGroovyExpression("(a=='something').implies(b=='other')")); + + } +} diff --git a/war/resources/help/matrix/combinationfilter.html b/war/resources/help/matrix/combinationfilter.html new file mode 100644 index 0000000000..199447635c --- /dev/null +++ b/war/resources/help/matrix/combinationfilter.html @@ -0,0 +1,41 @@ +

+ By default, Hudson builds all the possible combinations of axes exhaustively. But sometimes this is too many. + In such a situation, you can make the matrix sparse by filtering out combinations that you don't want through + a Goorvy expression that returns true or false. + +

+ When you specify a Groovy expression here, only the combinations that result in true + will be built. In evaluating the expression, axes are exposed as variables (with their values set + to the current combination evaluated.) + +

+ For example, let's say you are building on different operating systems for + different compilers. Assume that your slave labels are label=[linux,solaris] and you have + created an axis as compiler=[gcc,cc]. + + Any of the following expressions will filter out cc builds on linux. Depending on + how you think about this constraint, you'll probably find some more intuitive than others. + +

+ + + + + + + + + + + + + +
Read "if both linux and cc, it's invalid" +
!(label=="linux" && compiler=="cc")
+
Read "for a combination to be valid, it has to be either on solaris or on gcc." +
label=="solaris" || compiler=="gcc"
+
Read "if on Solaris, just do cc" +
(label=="solaris").implies(compiler=="cc")
+
+
+
\ No newline at end of file