diff --git a/src/main/java/jenkins/branch/NamedExceptionsBranchPropertyStrategy.java b/src/main/java/jenkins/branch/NamedExceptionsBranchPropertyStrategy.java index 91587479..c34c0563 100644 --- a/src/main/java/jenkins/branch/NamedExceptionsBranchPropertyStrategy.java +++ b/src/main/java/jenkins/branch/NamedExceptionsBranchPropertyStrategy.java @@ -102,12 +102,19 @@ public List getNamedExceptions() { @NonNull @Override public List getPropertiesFor(SCMHead head) { + List properties = new ArrayList<>(); + for (Named named : namedExceptions) { if (named.isMatch(head)) { - return new ArrayList<>(named.getProps()); + properties.addAll(named.getProps()); } } - return new ArrayList<>(defaultProperties); + + if (properties.isEmpty()) { + // if no one defined adds default + properties.addAll(defaultProperties); + } + return properties; } /** diff --git a/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/config.jelly b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/config.jelly index 2c713d1e..3ece8c17 100644 --- a/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/config.jelly +++ b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/config.jelly @@ -35,11 +35,15 @@ + + + + diff --git a/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help-named.html b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help-named.html new file mode 100644 index 00000000..6731b6d0 --- /dev/null +++ b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help-named.html @@ -0,0 +1,28 @@ + +
+ Properties are collected from all exceptions that matches the current branch name. + In case different exceptions define the same property but different configuration both + of them will be are applied. +
\ No newline at end of file diff --git a/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help.html b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help.html new file mode 100644 index 00000000..557132e2 --- /dev/null +++ b/src/main/resources/jenkins/branch/NamedExceptionsBranchPropertyStrategy/help.html @@ -0,0 +1,26 @@ + +
+ If no exceptions matches than defaults are used. +
\ No newline at end of file diff --git a/src/test/java/jenkins/branch/NamedExceptionsBranchPropertyStrategyTest.java b/src/test/java/jenkins/branch/NamedExceptionsBranchPropertyStrategyTest.java index 2e51252a..6d65f9f2 100644 --- a/src/test/java/jenkins/branch/NamedExceptionsBranchPropertyStrategyTest.java +++ b/src/test/java/jenkins/branch/NamedExceptionsBranchPropertyStrategyTest.java @@ -1,6 +1,11 @@ package jenkins.branch; +import java.util.List; +import org.hamcrest.Matchers; import org.junit.Test; +import org.mockito.Mockito; +import jenkins.branch.NamedExceptionsBranchPropertyStrategy.Named; +import jenkins.scm.api.SCMHead; import static jenkins.branch.NamedExceptionsBranchPropertyStrategy.Named.isMatch; import static org.hamcrest.MatcherAssert.assertThat; @@ -10,6 +15,27 @@ * @author Stephen Connolly */ public class NamedExceptionsBranchPropertyStrategyTest { + + @Test + public void test_that_strategy_aggregates_properties_from_matching_branches() throws Exception { + BranchProperty defaultProp = Mockito.mock(BranchProperty.class); + + BranchProperty prop1 = Mockito.mock(BranchProperty.class); + BranchProperty prop2 = Mockito.mock(BranchProperty.class); + BranchProperty prop3 = Mockito.mock(BranchProperty.class); + + BranchPropertyStrategy strategy = new NamedExceptionsBranchPropertyStrategy( // + new BranchProperty[] { defaultProp }, // + new Named[] { // + new Named("master", new BranchProperty[] { prop1 }), // + new Named("master,support/*", new BranchProperty[] { prop2 }), // + new Named("support/*", new BranchProperty[] { prop3 }), // + }); + + List matches = strategy.getPropertiesFor(new SCMHead("master")); + assertThat(matches, Matchers.containsInAnyOrder(prop1, prop2)); + } + @Test public void examplesFromHelpText() throws Exception { // "production" matches one and only one branch