Skip to content

Commit

Permalink
Feature: include/exclude branches of multi-branch pipelines
Browse files Browse the repository at this point in the history
Closes #246
  • Loading branch information
reftel committed Oct 14, 2016
1 parent 046b102 commit 8b73ba9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
*/
package com.smartcodeltd.jenkinsci.plugins.buildmonitor;

import com.google.common.base.Strings;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.api.Respond;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.facade.StaticJenkinsAPIs;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.installation.BuildMonitorInstallation;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.JobView;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.viewmodel.JobViews;
import hudson.Extension;
import hudson.model.Descriptor.FormException;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.ListView;
import net.sf.json.JSONObject;
Expand All @@ -43,6 +45,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

import static hudson.Util.filter;

Expand Down Expand Up @@ -86,6 +89,16 @@ public String currentOrder() {
return currentConfig().getOrder().getClass().getSimpleName();
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public String include() {
return currentConfig().getInclude();
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public String exclude() {
return currentConfig().getExclude();
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public boolean isDisplayCommitters() {
return currentConfig().shouldDisplayCommitters();
Expand All @@ -112,9 +125,13 @@ protected void submit(StaplerRequest req) throws ServletException, IOException,
synchronized (this) {

String requestedOrdering = req.getParameter("order");
String include = req.getParameter("include");
String exclude = req.getParameter("exclude");
title = req.getParameter("title");

currentConfig().setDisplayCommitters(json.optBoolean("displayCommitters", true));
currentConfig().setInclude(include);
currentConfig().setExclude(exclude);

try {
currentConfig().setOrder(orderIn(requestedOrdering));
Expand Down Expand Up @@ -143,11 +160,28 @@ private boolean isGiven(String value) {

private List<JobView> jobViews() {
JobViews views = new JobViews(new StaticJenkinsAPIs(), currentConfig());
List<JobView> jobs = new ArrayList<JobView>();

//A little bit of evil to make the type system happy.
@SuppressWarnings("unchecked")
List<ItemGroup<?>> groups = new ArrayList(filter(super.getItems(), ItemGroup.class));

Pattern includePattern = Strings.isNullOrEmpty(currentConfig().getInclude()) ? null : Pattern.compile(currentConfig().getInclude());
Pattern excludePattern = Strings.isNullOrEmpty(currentConfig().getExclude()) ? null : Pattern.compile(currentConfig().getExclude());
for (ItemGroup<?> group : groups) {
@SuppressWarnings("unchecked")
List<Job<?, ?>> groupJobs = new ArrayList(filter(group.getItems(), Job.class));
for (Job<?, ?> job : groupJobs) {
boolean shouldInclude = includePattern == null || includePattern.matcher(job.getRelativeNameFrom(group)).find();
boolean shouldExclude = excludePattern != null && excludePattern.matcher(job.getRelativeNameFrom(group)).find();
if (shouldInclude && !shouldExclude) {
jobs.add(views.viewOf(job));
}
}
}

@SuppressWarnings("unchecked")
List<Job<?, ?>> projects = new ArrayList(filter(super.getItems(), Job.class));
List<JobView> jobs = new ArrayList<JobView>();

Collections.sort(projects, currentConfig().getOrder());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,32 @@ public void setDisplayCommitters(boolean flag) {
public String toString() {
return Objects.toStringHelper(this)
.add("order", order.getClass().getSimpleName())
.add("include", include)
.add("exclude", exclude)
.toString();
}

// --

private Comparator<Job<?, ?>> order;

private String include;

public String getInclude() {
return include;
}

public void setInclude(String include) {
this.include = include;
}

private String exclude;

public String getExclude() {
return exclude;
}

public void setExclude(String exclude) {
this.exclude = exclude;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
<f:textbox name="title" value="${it.title}"/>
</f:entry>

<f:entry title="${%Include Branches}" help="${descriptor.getHelpFile('includeBranches')}">
<f:textbox name="include" field="include" value="${it.include()}"/>
</f:entry>

<f:entry title="${%Exclude Branches}" help="${descriptor.getHelpFile('excludeBranches')}">
<f:textbox name="exclude" field="exclude" value="${it.exclude()}"/>
</f:entry>

<f:entry title="${%Ordered by}">
<select name="order" class="setting-input">
<f:option value="ByName" selected='${it.currentOrder()=="ByName"}'>${%Name}</f:option>
Expand Down
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>
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>

0 comments on commit 8b73ba9

Please sign in to comment.