-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: include/exclude branches of multi-branch pipelines
Closes #246
- Loading branch information
Showing
8 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...nitor-plugin/src/main/java/com/smartcodeltd/jenkinsci/plugins/buildmonitor/JobFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.smartcodeltd.jenkinsci.plugins.buildmonitor; | ||
|
||
import com.google.common.base.Strings; | ||
import hudson.model.Item; | ||
import hudson.model.ItemGroup; | ||
import hudson.model.Job; | ||
import hudson.model.TopLevelItem; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import static hudson.Util.filter; | ||
|
||
public class JobFilter { | ||
private final Pattern includePattern; | ||
private final Pattern excludePattern; | ||
private Class<? extends ItemGroup> abstractFolderClass; | ||
|
||
public JobFilter(Config config) { | ||
String include = config.getBranchesToInclude(); | ||
String exclude = config.getBranchesToExclude(); | ||
includePattern = Strings.isNullOrEmpty(include) ? null : Pattern.compile(include); | ||
excludePattern = Strings.isNullOrEmpty(exclude) ? null : Pattern.compile(exclude); | ||
try { | ||
abstractFolderClass = Class.forName("com.cloudbees.hudson.plugins.folder.AbstractFolder") | ||
.asSubclass(ItemGroup.class); | ||
} catch (ClassNotFoundException e) { | ||
abstractFolderClass = null; | ||
} | ||
} | ||
|
||
public List<Job<?, ?>> filterJobs(Collection<? extends Item> items) { | ||
List<Job<?, ?>> jobs = new ArrayList<Job<?, ?>>(); | ||
|
||
for (ItemGroup<?> folder : filter(items, abstractFolderClass)) { | ||
List<Job> groupJobs = filter(folder.getItems(), Job.class); | ||
for (Job job : groupJobs) { | ||
String relativename = job.getRelativeNameFrom(folder); | ||
boolean shouldInclude = includePattern == null || includePattern.matcher(relativename).find(); | ||
boolean shouldExclude = excludePattern != null && excludePattern.matcher(relativename).find(); | ||
if (shouldInclude && !shouldExclude) { | ||
jobs.add(job); | ||
} | ||
} | ||
} | ||
|
||
for (Job job : filter(items, Job.class)) { | ||
jobs.add(job); | ||
} | ||
|
||
return jobs; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/help-branchesToExclude.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<div> | ||
<p> | ||
The Exclude Branches setting controls which branches of a multi-branch build to hide. | ||
If set, branches whose names match the value will be excluded, even if they match the Include Branches setting. | ||
If not set, no branches are excluded. | ||
</p> | ||
<p> | ||
Matching is done by treating the value as an unanchored regular expression. | ||
</p> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
.../smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/help-branchesToInclude.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div> | ||
<p> | ||
The Include Branches setting controls which branches of a multi-branch build to include on the screen. | ||
If set, only branches whose names match the value will be included. If not set, all branches are included. | ||
</p> | ||
<p> | ||
Matching is done by treating the value as an unanchored regular expression. | ||
</p> | ||
</div> |
116 changes: 116 additions & 0 deletions
116
...r-plugin/src/test/java/com/smartcodeltd/jenkinsci/plugins/buildmonitor/JobFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.smartcodeltd.jenkinsci.plugins.buildmonitor; | ||
|
||
import com.cloudbees.hudson.plugins.folder.AbstractFolder; | ||
import hudson.model.ItemGroup; | ||
import hudson.model.Job; | ||
import hudson.model.TopLevelItem; | ||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.anyObject; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JobFilterTest { | ||
@Test | ||
public void shouldNotAddStuff() { | ||
JobFilter filter = configuredJobFilter(null, null); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.<TopLevelItem>emptySet()); | ||
assertThat("should be empty", jobs.isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldCopyDirectJobs() { | ||
JobFilter filter = configuredJobFilter(null, null); | ||
final Job<?, ?> job = mockedJob("foo"); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.singleton(job)); | ||
assertThat("should contain one element", jobs.size(), is(1)); | ||
assertThat(jobs.get(0), jobIsSame(job)); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeJobsOfFoldersByDefault() { | ||
JobFilter filter = configuredJobFilter(null, null); | ||
final Job<?, ?> job = mockedJob("job"); | ||
final AbstractFolder folder = mockedFolder(job); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.singleton(folder)); | ||
assertThat("should contain one element", jobs.size(), is(1)); | ||
assertThat(jobs.get(0), jobIsSame(job)); | ||
} | ||
|
||
@Test | ||
public void shouldNotIncludeJobsThatDontMatchTheIncludePattern() { | ||
JobFilter filter = configuredJobFilter("special", null); | ||
final Job<?, ?> job = mockedJob("job"); | ||
final AbstractFolder folder = mockedFolder(job); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.singleton(folder)); | ||
assertThat("should be empty", jobs.isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldExcludeMatchingJobs() { | ||
JobFilter filter = configuredJobFilter(null, "nope"); | ||
final Job<?, ?> job = mockedJob("nope"); | ||
final AbstractFolder folder = mockedFolder(job); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.singleton(folder)); | ||
assertThat("should be empty", jobs.isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void excludeShouldWin() { | ||
JobFilter filter = configuredJobFilter("a", "b"); | ||
final Job<?, ?> job = mockedJob("ab"); | ||
final AbstractFolder folder = mockedFolder(job); | ||
List<Job<?, ?>> jobs = filter.filterJobs(Collections.singleton(folder)); | ||
assertThat("should be empty", jobs.isEmpty(), is(true)); | ||
} | ||
|
||
private Job<?, ?> mockedJob(String relativeName) { | ||
final Job<?, ?> job = mock(Job.class); | ||
when(job.getRelativeNameFrom((ItemGroup) anyObject())).thenReturn(relativeName); | ||
return job; | ||
} | ||
|
||
private AbstractFolder mockedFolder(Job<?, ?>... jobs) { | ||
final AbstractFolder folder = mock(AbstractFolder.class); | ||
when(folder.getItems()).thenReturn(Arrays.asList(jobs)); | ||
return folder; | ||
} | ||
|
||
private JobFilter configuredJobFilter(String include, String exclude) { | ||
Config config = new Config(); | ||
config.setBranchesToInclude(include); | ||
config.setBranchesToExclude(exclude); | ||
return new JobFilter(config); | ||
} | ||
|
||
private static Matcher<Job> jobIsSame(Job job) { | ||
return new JobIsSame(job); | ||
} | ||
|
||
static class JobIsSame extends BaseMatcher<Job> { | ||
private final Job<?, ?> job; | ||
|
||
JobIsSame(Job<?, ?> job) { | ||
this.job = job; | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("object was not the same as the expected one"); | ||
} | ||
|
||
@Override | ||
public boolean matches(Object item) { | ||
return item == job; | ||
} | ||
} | ||
} |