Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-62141] Allow more than one named exception to match each individual branch #193

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,19 @@ public List<Named> getNamedExceptions() {
@NonNull
@Override
public List<BranchProperty> getPropertiesFor(SCMHead head) {
List<BranchProperty> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
</f:entry>
</table>
</f:repeatable>
<f:helpLink url="${descriptor.getHelpFile('named')}" featureName="${%Exceptions}"/>
<f:helpArea/>
</f:block>
</f:section>
<f:section title="${%Defaults}">
<f:block>
<f:repeatableHeteroProperty field="defaultProperties" hasHeader="true" oneEach="true" honorOrder="true" addCaption="${%Add property}" deleteCaption="${%Delete property}"/>
<f:helpLink url="${descriptor.helpFile}" featureName="${%Defaults}"/>
<f:helpArea/>
</f:block>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ The MIT License
~
~ Copyright (c) 2020, Nikolas Falco
~
~ 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.
-->
<div>
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.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ The MIT License
~
~ Copyright (c) 2020, Nikolas Falco
~
~ 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.
-->
<div>
If no exceptions matches than defaults are used.
</div>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BranchProperty> 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
Expand Down