Skip to content
Permalink
Browse files

[JENKINS-36894] Introduce CombinationFilterShortcut

  • Loading branch information
ikedam committed Aug 8, 2016
1 parent 913ae0c commit 8637c076f70499a13eb8a0b2bfb0b27df7e065a3
@@ -0,0 +1,130 @@
/*
* The MIT License
*
* Copyright (c) 2016 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.shortcut;

import java.util.Collection;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

import hudson.Extension;
import hudson.Util;
import hudson.matrix.Combination;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;

/**
* Shortcuts to select combinations with a filter expression
*/
public class CombinationFilterShortcut extends MatrixCombinationsShortcut {
private final String name;
private final String combinationFilter;

/**
* ctor
*
* @param name display name of the link
* @param combinationFilter filter expression
*/
@DataBoundConstructor
public CombinationFilterShortcut(String name, String combinationFilter) {
this.name = Util.fixNull(name);
this.combinationFilter = Util.fixNull(combinationFilter);
}

/**
* @return filter expression
*/
@Nonnull
public String getCombinationFilter() {
return combinationFilter;
}

/**
* {@inheritDoc}
*/
@Nonnull
@Override
public Collection<Combination> getCombinations(
@Nonnull final MatrixProject project,
@CheckForNull MatrixBuild build
) {
return Collections2.filter(
Collections2.transform(
project.getActiveConfigurations(),
new Function<MatrixConfiguration, Combination>() {
public Combination apply(MatrixConfiguration c) {
return c.getCombination();
}
}
),
new Predicate<Combination>() {
public boolean apply(Combination c) {
return c.evalGroovyExpression(project.getAxes(), getCombinationFilter());
}
}
);
}

/**
* {@inheritDoc}
*/
@Override
public String getName() {
return name;
}

/**
* {@inheritDoc}
*/
@Override
public String getId() {
return String.format(
"filter-%s",
getName().replaceAll("[^A-Za-z0-9]+", "-")
);
}

/**
* Descriptor for {@link CombinationFilterShortcut}
*/
@Extension
public static class DescriptorImpl extends MatrixCombinationsShortcutDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "By combination filter";
}
}
}
@@ -0,0 +1,32 @@
<!--
The MIT License
Copyright (c) 2016 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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="name" title="${%Name}">
<f:textbox />
</f:entry>
<f:entry field="combinationFilter" title="${%Combination filter}">
<f:textbox />
</f:entry>
</j:jelly>
@@ -24,10 +24,15 @@

Behaviour.specify(".matrix-combinations-parameter .shortcut", "matrix-combinations-parameter-shortcut", 0, function (e) {
e.observe("click", function(e) {
var indexToCheck = this.readAttribute("data-combinations").split(",");
var combinations = this.readAttribute("data-combinations");
if (!combinations) {
combinations = "";
}
var indexToCheck = combinations.split(",");
var block = this.up(".matrix-combinations-parameter");
block.select(".combination").each(function(c) {
var toCheck = (indexToCheck.indexOf(c.readAttribute("data-combination")) >= 0);
var combination = c.readAttribute("data-combination");
var toCheck = combination && (indexToCheck.indexOf(combination) >= 0);
c.select("input[type='checkbox']").each(function(checkbox) {
checkbox.checked = toCheck;
});
@@ -0,0 +1,132 @@
/*
* The MIT License
*
* Copyright (c) 2016 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.shortcut;

import java.util.Arrays;

import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import hudson.matrix.AxisList;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.model.Item;
import hudson.model.ParametersDefinitionProperty;
import hudson.plugins.matrix_configuration_parameter.MatrixCombinationsJenkinsRule;
import hudson.plugins.matrix_configuration_parameter.MatrixCombinationsParameterDefinition;

/**
* Tests for {@link CombinationFilterShortcut}
*/
public class CombinationFilterShortcutTest {
@ClassRule
public static MatrixCombinationsJenkinsRule j = new MatrixCombinationsJenkinsRule();

@Test
public void testConfiguration() throws Exception {
AxisList axes = new AxisList(
new TextAxis("axis1", "value1-1", "value2-1"),
new TextAxis("axis2", "value2-1", "value2-2")
);
MatrixProject p = j.createMatrixProject();
p.setAxes(axes);
MatrixCombinationsParameterDefinition def = new MatrixCombinationsParameterDefinition(
"COMBINATIONS",
"",
"",
Arrays.<MatrixCombinationsShortcut>asList(
new CombinationFilterShortcut(
"FILTER",
"!(axis1 == 'value1-2' && axis2 == 'value2-2')"
)
)
);
p.addProperty(new ParametersDefinitionProperty(def));

j.configRoundtrip((Item)p);

j.assertEqualDataBoundBeans(
def,
p.getProperty(ParametersDefinitionProperty.class).getParameterDefinition("COMBINATIONS")
);
}

@Test
public void testCheck() throws Exception {
AxisList axes = new AxisList(
new TextAxis("axis1", "value1-1", "value1-2"),
new TextAxis("axis2", "value2-1", "value2-2")
);
MatrixProject p = j.createMatrixProject();
p.setAxes(axes);
MatrixCombinationsParameterDefinition def = new MatrixCombinationsParameterDefinition(
"COMBINATIONS",
"",
"",
Arrays.<MatrixCombinationsShortcut>asList(
new CombinationFilterShortcut(
"FILTER",
"!(axis1 == 'value1-2' && axis2 == 'value2-2')"
)
)
);
p.addProperty(new ParametersDefinitionProperty(def));

WebClient wc = j.createAllow405WebClient();
HtmlPage page = wc.getPage(p, "build");

j.clickShortcut(page, "filter-FILTER");

j.assertCombinationChecked(page, true, axes, "value1-1", "value2-1");
j.assertCombinationChecked(page, true, axes, "value1-2", "value2-1");
j.assertCombinationChecked(page, true, axes, "value1-1", "value2-2");
j.assertCombinationChecked(page, false, axes, "value1-2", "value2-2");
}

@Test
public void testCheckEmpty() throws Exception {
MatrixProject p = j.createMatrixProject();
MatrixCombinationsParameterDefinition def = new MatrixCombinationsParameterDefinition(
"COMBINATIONS",
"",
"",
Arrays.<MatrixCombinationsShortcut>asList(
new CombinationFilterShortcut(
"FILTER",
""
)
)
);
p.addProperty(new ParametersDefinitionProperty(def));

WebClient wc = j.createAllow405WebClient();
HtmlPage page = wc.getPage(p, "build");

j.clickShortcut(page, "filter-FILTER");
}
}

0 comments on commit 8637c07

Please sign in to comment.
You can’t perform that action at this time.