Add possibility to filter merge request triggers by labels (Fixes #406)

This commit is contained in:
Robin Müller 2016-09-19 22:42:08 +02:00
parent ca31969111
commit a48a09856b
22 changed files with 319 additions and 90 deletions

View File

@ -15,6 +15,9 @@ import com.dabsquared.gitlabjenkins.trigger.branch.ProjectBranchesProvider;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterType;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterConfig;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory;
import com.dabsquared.gitlabjenkins.trigger.handler.merge.MergeRequestHookTriggerHandler;
import com.dabsquared.gitlabjenkins.trigger.handler.note.NoteHookTriggerHandler;
import com.dabsquared.gitlabjenkins.trigger.handler.push.PushHookTriggerHandler;
@ -78,11 +81,14 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
private String includeBranchesSpec;
private String excludeBranchesSpec;
private String targetBranchRegex;
private final MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig;
private transient BranchFilter branchFilter;
private transient PushHookTriggerHandler pushHookTriggerHandler;
private transient MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler;
private transient NoteHookTriggerHandler noteHookTriggerHandler;
private transient boolean acceptMergeRequestOnSuccess;
private transient MergeRequestLabelFilter mergeRequestLabelFilter;
@DataBoundConstructor
@ -91,7 +97,8 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
boolean triggerOnNoteRequest, String noteRegex, boolean skipWorkInProgressMergeRequest, boolean ciSkip,
boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addCiMessage, boolean addVoteOnMergeRequest,
boolean acceptMergeRequestOnSuccess, BranchFilterType branchFilterType,
String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex) {
String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex,
MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig) {
this.triggerOnPush = triggerOnPush;
this.triggerOnMergeRequest = triggerOnMergeRequest;
this.triggerOnNoteRequest = triggerOnNoteRequest;
@ -108,9 +115,11 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
this.excludeBranchesSpec = excludeBranchesSpec;
this.targetBranchRegex = targetBranchRegex;
this.acceptMergeRequestOnSuccess = acceptMergeRequestOnSuccess;
this.mergeRequestLabelFilterConfig = mergeRequestLabelFilterConfig;
initializeTriggerHandler();
initializeBranchFilter();
initializeMergeRequestLabelFilter();
}
@Initializer(after = InitMilestone.JOB_LOADED)
@ -209,19 +218,23 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return targetBranchRegex;
}
public MergeRequestLabelFilterConfig getMergeRequestLabelFilterConfig() {
return mergeRequestLabelFilterConfig;
}
// executes when the Trigger receives a push request
public void onPost(final PushHook hook) {
pushHookTriggerHandler.handle(job, hook, ciSkip, branchFilter);
pushHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
// executes when the Trigger receives a merge request
public void onPost(final MergeRequestHook hook) {
mergeRequestHookTriggerHandler.handle(job, hook, ciSkip, branchFilter);
mergeRequestHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
// executes when the Trigger receives a note request
public void onPost(final NoteHook hook) {
noteHookTriggerHandler.handle(job, hook, ciSkip, branchFilter);
noteHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
private void initializeTriggerHandler() {
@ -238,6 +251,10 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
.build(branchFilterType));
}
private void initializeMergeRequestLabelFilter() {
mergeRequestLabelFilter = MergeRequestLabelFilterFactory.newMergeRequestLabelFilter(mergeRequestLabelFilterConfig);
}
@Override
protected Object readResolve() throws ObjectStreamException {
if (branchFilterType == null) {
@ -245,6 +262,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
}
initializeTriggerHandler();
initializeBranchFilter();
initializeMergeRequestLabelFilter();
return super.readResolve();
}

View File

@ -6,6 +6,7 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Date;
import java.util.List;
/**
* @author Robin Müller
@ -33,6 +34,7 @@ public class MergeRequestObjectAttributes {
private String url;
private Action action;
private Boolean workInProgress;
private List<String> labels;
public Integer getId() {
return id;
@ -194,6 +196,14 @@ public class MergeRequestObjectAttributes {
this.workInProgress = workInProgress;
}
public List<String> getLabels() {
return labels;
}
public void setLabels(List<String> labels) {
this.labels = labels;
}
@Override
public boolean equals(Object o) {
if (this == o) {
@ -204,78 +214,81 @@ public class MergeRequestObjectAttributes {
}
MergeRequestObjectAttributes that = (MergeRequestObjectAttributes) o;
return new EqualsBuilder()
.append(workInProgress, that.workInProgress)
.append(id, that.id)
.append(iid, that.iid)
.append(sourceBranch, that.sourceBranch)
.append(targetBranch, that.targetBranch)
.append(sourceProjectId, that.sourceProjectId)
.append(targetProjectId, that.targetProjectId)
.append(authorId, that.authorId)
.append(assigneeId, that.assigneeId)
.append(title, that.title)
.append(createdAt, that.createdAt)
.append(updatedAt, that.updatedAt)
.append(state, that.state)
.append(description, that.description)
.append(source, that.source)
.append(target, that.target)
.append(lastCommit, that.lastCommit)
.append(mergeStatus, that.mergeStatus)
.append(url, that.url)
.append(action, that.action)
.isEquals();
.append(id, that.id)
.append(iid, that.iid)
.append(sourceBranch, that.sourceBranch)
.append(targetBranch, that.targetBranch)
.append(sourceProjectId, that.sourceProjectId)
.append(targetProjectId, that.targetProjectId)
.append(authorId, that.authorId)
.append(assigneeId, that.assigneeId)
.append(title, that.title)
.append(createdAt, that.createdAt)
.append(updatedAt, that.updatedAt)
.append(state, that.state)
.append(description, that.description)
.append(source, that.source)
.append(target, that.target)
.append(lastCommit, that.lastCommit)
.append(mergeStatus, that.mergeStatus)
.append(url, that.url)
.append(action, that.action)
.append(workInProgress, that.workInProgress)
.append(labels, that.labels)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(id)
.append(iid)
.append(sourceBranch)
.append(targetBranch)
.append(sourceProjectId)
.append(targetProjectId)
.append(authorId)
.append(assigneeId)
.append(title)
.append(createdAt)
.append(updatedAt)
.append(state)
.append(description)
.append(source)
.append(target)
.append(lastCommit)
.append(mergeStatus)
.append(url)
.append(action)
.append(workInProgress)
.toHashCode();
.append(id)
.append(iid)
.append(sourceBranch)
.append(targetBranch)
.append(sourceProjectId)
.append(targetProjectId)
.append(authorId)
.append(assigneeId)
.append(title)
.append(createdAt)
.append(updatedAt)
.append(state)
.append(description)
.append(source)
.append(target)
.append(lastCommit)
.append(mergeStatus)
.append(url)
.append(action)
.append(workInProgress)
.append(labels)
.toHashCode();
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("id", id)
.append("iid", iid)
.append("sourceBranch", sourceBranch)
.append("targetBranch", targetBranch)
.append("sourceProjectId", sourceProjectId)
.append("targetProjectId", targetProjectId)
.append("authorId", authorId)
.append("assigneeId", assigneeId)
.append("title", title)
.append("createdAt", createdAt)
.append("updatedAt", updatedAt)
.append("state", state)
.append("description", description)
.append("source", source)
.append("target", target)
.append("lastCommit", lastCommit)
.append("mergeStatus", mergeStatus)
.append("url", url)
.append("action", action)
.append("workInProgress", workInProgress)
.toString();
.append("id", id)
.append("iid", iid)
.append("sourceBranch", sourceBranch)
.append("targetBranch", targetBranch)
.append("sourceProjectId", sourceProjectId)
.append("targetProjectId", targetProjectId)
.append("authorId", authorId)
.append("assigneeId", assigneeId)
.append("title", title)
.append("createdAt", createdAt)
.append("updatedAt", updatedAt)
.append("state", state)
.append("description", description)
.append("source", source)
.append("target", target)
.append("lastCommit", lastCommit)
.append("mergeStatus", mergeStatus)
.append("url", url)
.append("action", action)
.append("workInProgress", workInProgress)
.append("labels", labels)
.toString();
}
}

View File

@ -0,0 +1,10 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
import java.util.Collection;
/**
* @author Robin Müller
*/
public interface MergeRequestLabelFilter {
boolean isMergeRequestAllowed(Collection<String> labels);
}

View File

@ -0,0 +1,26 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* @author Robin Müller
*/
public class MergeRequestLabelFilterConfig {
private final String include;
private final String exclude;
@DataBoundConstructor
public MergeRequestLabelFilterConfig(String include, String exclude) {
this.include = include;
this.exclude = exclude;
}
public String getInclude() {
return include;
}
public String getExclude() {
return exclude;
}
}

View File

@ -0,0 +1,17 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
/**
* @author Robin Müller
*/
public class MergeRequestLabelFilterFactory {
private MergeRequestLabelFilterFactory() { }
public static MergeRequestLabelFilter newMergeRequestLabelFilter(MergeRequestLabelFilterConfig config) {
if (config == null) {
return new NopMergeRequestLabelFilter();
} else {
return new MergeRequestLabelFilterImpl(config.getInclude(), config.getExclude());
}
}
}

View File

@ -0,0 +1,52 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
import com.google.common.base.Splitter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* @author Robin Müller
*/
class MergeRequestLabelFilterImpl implements MergeRequestLabelFilter {
private final Set<String> includeLabels;
private final Set<String> excludeLabels;
public MergeRequestLabelFilterImpl(String includeLabels, String excludeLabels) {
this.includeLabels = convert(includeLabels);
this.excludeLabels = convert(excludeLabels);
}
@Override
public boolean isMergeRequestAllowed(Collection<String> labels) {
return containsNoExcludeLabel(labels) && containsIncludeLabel(labels);
}
private boolean containsNoExcludeLabel(Collection<String> labels) {
for (String excludeLabel : excludeLabels) {
if (labels.contains(excludeLabel)) {
return false;
}
}
return true;
}
private boolean containsIncludeLabel(Collection<String> labels) {
for (String includeLabel : includeLabels) {
if (labels.contains(includeLabel)) {
return true;
}
}
return includeLabels.isEmpty();
}
private Set<String> convert(String commaSeparatedString) {
Set<String> result = new HashSet<>();
for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split(commaSeparatedString)) {
result.add(s);
}
return result;
}
}

View File

@ -0,0 +1,13 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
import java.util.Collection;
/**
* @author Robin Müller
*/
class NopMergeRequestLabelFilter implements MergeRequestLabelFilter {
@Override
public boolean isMergeRequestAllowed(Collection<String> labels) {
return true;
}
}

View File

@ -9,6 +9,7 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.WebHook;
import com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import hudson.model.AbstractProject;
import hudson.model.Action;
@ -35,7 +36,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
private static final Logger LOGGER = Logger.getLogger(AbstractWebHookTriggerHandler.class.getName());
@Override
public void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (ciSkip && isCiSkip(hook)) {
LOGGER.log(Level.INFO, "Skipping due to ci-skip.");
return;

View File

@ -2,6 +2,7 @@ package com.dabsquared.gitlabjenkins.trigger.handler;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.WebHook;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import hudson.model.Job;
/**
@ -9,5 +10,5 @@ import hudson.model.Job;
*/
public interface WebHookTriggerHandler<H extends WebHook> {
void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter);
void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter);
}

View File

@ -7,6 +7,7 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestObjectAttribut
import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import com.dabsquared.gitlabjenkins.util.BuildUtil;
import hudson.model.Job;
@ -38,12 +39,13 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
}
@Override
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
MergeRequestObjectAttributes objectAttributes = hook.getObjectAttributes();
if (allowedStates.contains(objectAttributes.getState())
&& isLastCommitNotYetBuild(job, hook)
&& isNotSkipWorkInProgressMergeRequest(objectAttributes)) {
super.handle(job, hook, ciSkip, branchFilter);
&& isNotSkipWorkInProgressMergeRequest(objectAttributes)
&& mergeRequestLabelFilter.isMergeRequestAllowed(hook.getObjectAttributes().getLabels())) {
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
}

View File

@ -2,6 +2,7 @@ package com.dabsquared.gitlabjenkins.trigger.handler.merge;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import hudson.model.Job;
/**
@ -9,7 +10,7 @@ import hudson.model.Job;
*/
class NopMergeRequestHookTriggerHandler implements MergeRequestHookTriggerHandler {
@Override
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do
}
}

View File

@ -2,6 +2,7 @@ package com.dabsquared.gitlabjenkins.trigger.handler.note;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.NoteHook;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import hudson.model.Job;
/**
@ -9,7 +10,7 @@ import hudson.model.Job;
*/
class NopNoteHookTriggerHandler implements NoteHookTriggerHandler {
@Override
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do
}
}

View File

@ -4,6 +4,7 @@ import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.NoteHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
@ -29,9 +30,10 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
}
@Override
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter) {
if (isValidTriggerPhrase(hook.getObjectAttributes().getNote())) {
super.handle(job, hook, ciSkip, branchFilter);
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (isValidTriggerPhrase(hook.getObjectAttributes().getNote())
&& mergeRequestLabelFilter.isMergeRequestAllowed(hook.getMergeRequest().getLabels())) {
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
}

View File

@ -2,6 +2,7 @@ package com.dabsquared.gitlabjenkins.trigger.handler.push;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import hudson.model.Job;
/**
@ -9,7 +10,7 @@ import hudson.model.Job;
*/
class NopPushHookTriggerHandler implements PushHookTriggerHandler {
@Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do
}
}

View File

@ -13,6 +13,7 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
import com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import hudson.model.AbstractProject;
import hudson.model.Action;
@ -49,7 +50,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
}
@Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
try {
if (job instanceof AbstractProject<?, ?>) {
AbstractProject<?, ?> project = (AbstractProject<?, ?>) job;
@ -59,7 +60,9 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
if (property != null && property.getClient() != null && projectId != null && trigger != null) {
GitLabApi client = property.getClient();
for (MergeRequest mergeRequest : getOpenMergeRequests(client, projectId.toString())) {
handleMergeRequest(job, hook, ciSkip, branchFilter, client, mergeRequest);
if (mergeRequestLabelFilter.isMergeRequestAllowed(mergeRequest.getLabels())) {
handleMergeRequest(job, hook, ciSkip, branchFilter, client, mergeRequest);
}
}
}
}

View File

@ -5,6 +5,7 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.Commit;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
@ -23,9 +24,9 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
private static final String NO_COMMIT = "0000000000000000000000000000000000000000";
@Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (isNoRemoveBranchPush(hook)) {
super.handle(job, hook, ciSkip, branchFilter);
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
}

View File

@ -2,6 +2,7 @@ package com.dabsquared.gitlabjenkins.trigger.handler.push;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilter;
import hudson.model.Job;
import java.util.List;
@ -18,9 +19,9 @@ class PushHookTriggerHandlerList implements PushHookTriggerHandler {
}
@Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter) {
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
for (PushHookTriggerHandler handler : handlers) {
handler.handle(job, hook, ciSkip, branchFilter);
handler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter);
}
}
}

View File

@ -56,6 +56,17 @@
</f:entry>
</f:radioBlock>
<!--</f:section> -->
<f:optionalBlock title="${%Filter merge request by label}" field="mergeRequestLabelFilterConfig"
checked="${instance.mergeRequestLabelFilterConfig != null}">
<f:entry title="${%Include}">
<f:textbox field="includeMergeRequestLabels" name="include" value="${instance.mergeRequestLabelFilterConfig.include}"
autoCompleteDelimChar=","/>
</f:entry>
<f:entry title="${%Exclude}">
<f:textbox field="excludeMergeRequestLabels" name="exclude" value="${instance.mergeRequestLabelFilterConfig.exclude}"
autoCompleteDelimChar=","/>
</f:entry>
</f:optionalBlock>
</table>
</f:entry>
</f:advanced>

View File

@ -0,0 +1,46 @@
package com.dabsquared.gitlabjenkins.trigger.filter;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Robin Müller
*/
public class MergeRequestLabelFilterImplTest {
@Test
public void includeLabels() {
MergeRequestLabelFilterImpl mergeRequestLabelFilter = new MergeRequestLabelFilterImpl("include, include2", "");
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("include")), is(true));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("include2")), is(true));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("other-label")), is(false));
}
@Test
public void excludeLabels() {
MergeRequestLabelFilterImpl mergeRequestLabelFilter = new MergeRequestLabelFilterImpl("", "exclude, exclude2");
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("exclude")), is(false));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("exclude2")), is(false));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("other-label")), is(true));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.<String>emptySet()), is(true));
}
@Test
public void includeAndExcludeLabels() {
MergeRequestLabelFilterImpl mergeRequestLabelFilter = new MergeRequestLabelFilterImpl("include, include2", "exclude, exclude2");
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("include")), is(true));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("include2")), is(true));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("exclude")), is(false));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("exclude2")), is(false));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Collections.singleton("other-label")), is(false));
assertThat(mergeRequestLabelFilter.isMergeRequestAllowed(Arrays.asList("include", "exclude")), is(false));
}
}

View File

@ -32,6 +32,7 @@ import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.M
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.ProjectBuilder.project;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.UserBuilder.user;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig;
import static com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory.newMergeRequestLabelFilter;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@ -67,7 +68,8 @@ public class MergeRequestHookTriggerHandlerImplTest {
project.setQuietPeriod(0);
mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
.withObjectAttributes(mergeRequestObjectAttributes().withDescription("[ci-skip]").build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
@ -126,7 +128,8 @@ public class MergeRequestHookTriggerHandlerImplTest {
.withWebUrl("https://gitlab.org/test.git")
.build()
)
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));

View File

@ -33,6 +33,7 @@ import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.N
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.ProjectBuilder.project;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.UserBuilder.user;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig;
import static com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory.newMergeRequestLabelFilter;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@ -78,7 +79,8 @@ public class NoteHookTriggerHandlerImplTest {
.withUrl("https://gitlab.org/test/merge_requests/1#note_1")
.build())
.withMergeRequest(mergeRequestObjectAttributes().withDescription("[ci-skip]").build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
@ -144,7 +146,8 @@ public class NoteHookTriggerHandlerImplTest {
.withWebUrl("https://gitlab.org/test.git")
.build())
.build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));

View File

@ -30,6 +30,7 @@ import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.P
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.RepositoryBuilder.repository;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory.newBranchFilter;
import static com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory.newMergeRequestLabelFilter;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@ -66,7 +67,8 @@ public class PushHookTriggerHandlerImplTest {
pushHookTriggerHandler.handle(project, pushHook()
.withCommits(Arrays.asList(commit().withMessage("some message").build(),
commit().withMessage("[ci-skip]").build()))
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
@ -110,7 +112,8 @@ public class PushHookTriggerHandlerImplTest {
.build())
.withAfter(commit.name())
.withRef("refs/heads/" + git.nameRev().add(head).call().get(head))
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));