Make it possible to cancel pending builds on merge request update.
This commit is contained in:
parent
77fbc95c34
commit
df7432a760
|
@ -101,6 +101,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
private MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig;
|
||||
private volatile Secret secretToken;
|
||||
private String pendingBuildName;
|
||||
private boolean cancelPendingBuildsOnUpdate;
|
||||
|
||||
private transient BranchFilter branchFilter;
|
||||
private transient PushHookTriggerHandler pushHookTriggerHandler;
|
||||
|
@ -122,7 +123,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
boolean acceptMergeRequestOnSuccess, BranchFilterType branchFilterType,
|
||||
String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex,
|
||||
MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig, String secretToken, boolean triggerOnPipelineEvent,
|
||||
boolean triggerOnApprovedMergeRequest, String pendingBuildName) {
|
||||
boolean triggerOnApprovedMergeRequest, String pendingBuildName, boolean cancelPendingBuildsOnUpdate) {
|
||||
this.triggerOnPush = triggerOnPush;
|
||||
this.triggerOnMergeRequest = triggerOnMergeRequest;
|
||||
this.triggerOnAcceptedMergeRequest = triggerOnAcceptedMergeRequest;
|
||||
|
@ -146,6 +147,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
this.secretToken = Secret.fromString(secretToken);
|
||||
this.triggerOnApprovedMergeRequest = triggerOnApprovedMergeRequest;
|
||||
this.pendingBuildName = pendingBuildName;
|
||||
this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate;
|
||||
|
||||
initializeTriggerHandler();
|
||||
initializeBranchFilter();
|
||||
|
@ -279,6 +281,10 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
return pendingBuildName;
|
||||
}
|
||||
|
||||
public boolean getCancelPendingBuildsOnUpdate() {
|
||||
return this.cancelPendingBuildsOnUpdate;
|
||||
}
|
||||
|
||||
@DataBoundSetter
|
||||
public void setTriggerOnPush(boolean triggerOnPush) {
|
||||
this.triggerOnPush = triggerOnPush;
|
||||
|
@ -399,6 +405,11 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
this.pendingBuildName = pendingBuildName;
|
||||
}
|
||||
|
||||
@DataBoundSetter
|
||||
public void setCancelPendingBuildsOnUpdate(boolean cancelPendingBuildsOnUpdate) {
|
||||
this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate;
|
||||
}
|
||||
|
||||
// executes when the Trigger receives a push request
|
||||
public void onPost(final PushHook hook) {
|
||||
if (branchFilter == null) {
|
||||
|
@ -449,9 +460,9 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
private void initializeTriggerHandler() {
|
||||
mergeRequestHookTriggerHandler = newMergeRequestHookTriggerHandler(triggerOnMergeRequest,
|
||||
triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest, triggerOpenMergeRequestOnPush,
|
||||
skipWorkInProgressMergeRequest, triggerOnApprovedMergeRequest);
|
||||
skipWorkInProgressMergeRequest, triggerOnApprovedMergeRequest, cancelPendingBuildsOnUpdate);
|
||||
noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex);
|
||||
pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest);
|
||||
pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate);
|
||||
pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
package com.dabsquared.gitlabjenkins.trigger.handler;
|
||||
|
||||
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
|
||||
import com.dabsquared.gitlabjenkins.cause.CauseData;
|
||||
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
|
||||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClient;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.model.BuildState;
|
||||
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 com.dabsquared.gitlabjenkins.util.PendingBuildsUtil;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
|
@ -25,7 +23,6 @@ import jenkins.triggers.SCMTriggerItem;
|
|||
import net.karneim.pojobuilder.GeneratePojoBuilder;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.eclipse.jgit.transport.URIish;
|
||||
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
@ -52,6 +49,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
if (branchFilter.isBranchAllowed(targetBranch)) {
|
||||
LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType()));
|
||||
setCommitStatusPendingIfNecessary(job, hook);
|
||||
cancelPendingBuildsIfNecessary(job, hook);
|
||||
scheduleBuild(job, createActions(job, hook));
|
||||
} else {
|
||||
LOGGER.log(Level.INFO, "branch {0} is not allowed", targetBranch);
|
||||
|
@ -63,7 +61,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
protected abstract boolean isCiSkip(H hook);
|
||||
|
||||
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, H hook) {
|
||||
String buildName = resolvePendingBuildName(job);
|
||||
String buildName = PendingBuildsUtil.resolvePendingBuildName(job);
|
||||
if (StringUtils.isNotBlank(buildName)) {
|
||||
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
BuildStatusUpdate buildStatusUpdate = retrieveBuildStatusUpdate(hook);
|
||||
|
@ -96,6 +94,8 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
return actions.toArray(new Action[actions.size()]);
|
||||
}
|
||||
|
||||
protected void cancelPendingBuildsIfNecessary(Job<?, ?> job, H hook) {}
|
||||
|
||||
protected abstract CauseData retrieveCauseData(H hook);
|
||||
|
||||
protected abstract String getTargetBranch(H hook);
|
||||
|
@ -171,20 +171,4 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
return ref;
|
||||
}
|
||||
}
|
||||
|
||||
public static String resolvePendingBuildName(Job<?, ?> job) {
|
||||
if (job instanceof AbstractProject) {
|
||||
GitLabCommitStatusPublisher publisher =
|
||||
(GitLabCommitStatusPublisher) ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class);
|
||||
if (publisher != null) {
|
||||
return publisher.getName();
|
||||
}
|
||||
} else if (job instanceof WorkflowJob) {
|
||||
GitLabPushTrigger trigger = GitLabPushTrigger.getFromJob(job);
|
||||
if (trigger != null) {
|
||||
return trigger.getPendingBuildName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,11 +21,12 @@ public final class MergeRequestHookTriggerHandlerFactory {
|
|||
boolean triggerOnClosedMergeRequest,
|
||||
TriggerOpenMergeRequest triggerOpenMergeRequest,
|
||||
boolean skipWorkInProgressMergeRequest,
|
||||
boolean triggerOnApprovedMergeRequest) {
|
||||
boolean triggerOnApprovedMergeRequest,
|
||||
boolean cancelPendingBuildsOnUpdate) {
|
||||
if (triggerOnMergeRequest || triggerOnAcceptedMergeRequest || triggerOnClosedMergeRequest || triggerOpenMergeRequest != TriggerOpenMergeRequest.never || triggerOnApprovedMergeRequest) {
|
||||
return new MergeRequestHookTriggerHandlerImpl(retrieveAllowedStates(triggerOnMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest, triggerOpenMergeRequest),
|
||||
retrieveAllowedActions(triggerOnApprovedMergeRequest),
|
||||
skipWorkInProgressMergeRequest);
|
||||
skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate);
|
||||
} else {
|
||||
return new NopMergeRequestHookTriggerHandler();
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ 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 com.dabsquared.gitlabjenkins.util.PendingBuildsUtil;
|
||||
import hudson.model.Job;
|
||||
import hudson.model.Run;
|
||||
import hudson.plugins.git.GitSCM;
|
||||
|
@ -39,15 +40,17 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
|
|||
private final Collection<State> allowedStates;
|
||||
private final boolean skipWorkInProgressMergeRequest;
|
||||
private final Collection<Action> allowedActions;
|
||||
private final boolean cancelPendingBuildsOnUpdate;
|
||||
|
||||
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, boolean skipWorkInProgressMergeRequest) {
|
||||
this(allowedStates, EnumSet.allOf(Action.class),skipWorkInProgressMergeRequest);
|
||||
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, boolean skipWorkInProgressMergeRequest, boolean cancelPendingBuildsOnUpdate) {
|
||||
this(allowedStates, EnumSet.allOf(Action.class), skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate);
|
||||
}
|
||||
|
||||
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, Collection<Action> allowedActions, boolean skipWorkInProgressMergeRequest) {
|
||||
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, Collection<Action> allowedActions, boolean skipWorkInProgressMergeRequest, boolean cancelPendingBuildsOnUpdate) {
|
||||
this.allowedStates = allowedStates;
|
||||
this.allowedActions = allowedActions;
|
||||
this.skipWorkInProgressMergeRequest = skipWorkInProgressMergeRequest;
|
||||
this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -77,6 +80,17 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
|
|||
&& hook.getObjectAttributes().getDescription().contains("[ci-skip]");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cancelPendingBuildsIfNecessary(Job<?, ?> job, MergeRequestHook hook) {
|
||||
if (!this.cancelPendingBuildsOnUpdate) {
|
||||
return;
|
||||
}
|
||||
if (!hook.getObjectAttributes().getAction().equals(Action.update)) {
|
||||
return;
|
||||
}
|
||||
PendingBuildsUtil.cancelPendingBuilds(job, hook.getObjectAttributes().getSourceProjectId(), hook.getObjectAttributes().getSourceBranch());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTargetBranch(MergeRequestHook hook) {
|
||||
return hook.getObjectAttributes() == null ? null : hook.getObjectAttributes().getTargetBranch();
|
||||
|
|
|
@ -14,8 +14,8 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
|
|||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
|
||||
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.LoggerUtil;
|
||||
import com.dabsquared.gitlabjenkins.util.PendingBuildsUtil;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
|
@ -47,9 +47,11 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
private final static Logger LOGGER = Logger.getLogger(OpenMergeRequestPushHookTriggerHandler.class.getName());
|
||||
|
||||
private final boolean skipWorkInProgressMergeRequest;
|
||||
private final boolean cancelPendingBuildsOnUpdate;
|
||||
|
||||
OpenMergeRequestPushHookTriggerHandler(boolean skipWorkInProgressMergeRequest) {
|
||||
OpenMergeRequestPushHookTriggerHandler(boolean skipWorkInProgressMergeRequest, boolean cancelPendingBuildsOnUpdate) {
|
||||
this.skipWorkInProgressMergeRequest = skipWorkInProgressMergeRequest;
|
||||
this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -114,7 +116,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
Project project = client.getProject(mergeRequest.getSourceProjectId().toString());
|
||||
String commit = branch.getCommit().getId();
|
||||
setCommitStatusPendingIfNecessary(job, mergeRequest.getSourceProjectId(), commit, branch.getName());
|
||||
|
||||
cancelPendingBuildsIfNecessary(job, mergeRequest.getSourceProjectId(), mergeRequest.getSourceBranch());
|
||||
List<Action> actions = Arrays.<Action>asList(new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook, project, mergeRequest, branch))),
|
||||
new RevisionParameterAction(commit, retrieveUrIish(hook)));
|
||||
scheduleBuild(job, actions.toArray(new Action[actions.size()]));
|
||||
|
@ -153,7 +155,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
}
|
||||
|
||||
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, Integer projectId, String commit, String ref) {
|
||||
String buildName = AbstractWebHookTriggerHandler.resolvePendingBuildName(job);
|
||||
String buildName = PendingBuildsUtil.resolvePendingBuildName(job);
|
||||
if (StringUtils.isNotBlank(buildName)) {
|
||||
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
try {
|
||||
|
@ -165,6 +167,13 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
}
|
||||
}
|
||||
|
||||
private void cancelPendingBuildsIfNecessary(Job<?, ?> job, Integer projectId, String branch) {
|
||||
if (!this.cancelPendingBuildsOnUpdate) {
|
||||
return;
|
||||
}
|
||||
PendingBuildsUtil.cancelPendingBuilds(job, projectId, branch);
|
||||
}
|
||||
|
||||
private void scheduleBuild(Job<?, ?> job, Action[] actions) {
|
||||
int projectBuildDelay = 0;
|
||||
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
|
||||
|
|
|
@ -14,9 +14,10 @@ public final class PushHookTriggerHandlerFactory {
|
|||
|
||||
public static PushHookTriggerHandler newPushHookTriggerHandler(boolean triggerOnPush,
|
||||
TriggerOpenMergeRequest triggerOpenMergeRequestOnPush,
|
||||
boolean skipWorkInProgressMergeRequest) {
|
||||
boolean skipWorkInProgressMergeRequest,
|
||||
boolean cancelPendingBuildsOnUpdate) {
|
||||
if (triggerOnPush || triggerOpenMergeRequestOnPush == TriggerOpenMergeRequest.both) {
|
||||
return new PushHookTriggerHandlerList(retrieveHandlers(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest));
|
||||
return new PushHookTriggerHandlerList(retrieveHandlers(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate));
|
||||
} else {
|
||||
return new NopPushHookTriggerHandler();
|
||||
}
|
||||
|
@ -24,13 +25,14 @@ public final class PushHookTriggerHandlerFactory {
|
|||
|
||||
private static List<PushHookTriggerHandler> retrieveHandlers(boolean triggerOnPush,
|
||||
TriggerOpenMergeRequest triggerOpenMergeRequestOnPush,
|
||||
boolean skipWorkInProgressMergeRequest) {
|
||||
boolean skipWorkInProgressMergeRequest,
|
||||
boolean cancelPendingBuildsOnUpdate) {
|
||||
List<PushHookTriggerHandler> result = new ArrayList<>();
|
||||
if (triggerOnPush) {
|
||||
result.add(new PushHookTriggerHandlerImpl());
|
||||
}
|
||||
if (triggerOpenMergeRequestOnPush == TriggerOpenMergeRequest.both) {
|
||||
result.add(new OpenMergeRequestPushHookTriggerHandler(skipWorkInProgressMergeRequest));
|
||||
result.add(new OpenMergeRequestPushHookTriggerHandler(skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
package com.dabsquared.gitlabjenkins.util;
|
||||
|
||||
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
|
||||
import com.dabsquared.gitlabjenkins.cause.CauseData;
|
||||
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
|
||||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClient;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.model.BuildState;
|
||||
import com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.Cause;
|
||||
import hudson.model.Job;
|
||||
import hudson.model.Queue;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PendingBuildsUtil {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(PendingBuildsUtil.class.getName());
|
||||
|
||||
public static void cancelPendingBuilds(Job<?, ?> job, Integer projectId, String branch) {
|
||||
Queue queue = Jenkins.getInstance().getQueue();
|
||||
for (Queue.Item item : queue.getItems()) {
|
||||
if (!job.getName().equals(item.task.getName())) {
|
||||
continue;
|
||||
}
|
||||
GitLabWebHookCause queueItemGitLabWebHookCause = getGitLabWebHookCauseData(item);
|
||||
if (queueItemGitLabWebHookCause == null) {
|
||||
continue;
|
||||
}
|
||||
CauseData queueItemCauseData = queueItemGitLabWebHookCause.getData();
|
||||
if (!projectId.equals(queueItemCauseData.getSourceProjectId())) {
|
||||
continue;
|
||||
}
|
||||
if (branch.equals(queueItemCauseData.getBranch())) {
|
||||
cancel(item, queue, branch);
|
||||
setCommitStatusCancelledIfNecessary(queueItemCauseData, job);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static GitLabWebHookCause getGitLabWebHookCauseData(Queue.Item item) {
|
||||
for (Cause cause : item.getCauses()) {
|
||||
if (cause instanceof GitLabWebHookCause) {
|
||||
return (GitLabWebHookCause) cause;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void cancel(Queue.Item item, Queue queue, String branch) {
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Cancelling job {0} for branch {1}", LoggerUtil.toArray(item.task.getName(), branch));
|
||||
queue.cancel(item);
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.SEVERE, "Error cancelling queued build", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setCommitStatusCancelledIfNecessary(CauseData causeData, Job<?, ?> job) {
|
||||
String buildName = resolvePendingBuildName(job);
|
||||
if (StringUtils.isBlank(buildName)) {
|
||||
return;
|
||||
}
|
||||
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
try {
|
||||
client.changeBuildStatus(causeData.getSourceProjectId(), causeData.getLastCommit(), BuildState.canceled,
|
||||
causeData.getSourceBranch(), buildName, null, BuildState.canceled.name());
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String resolvePendingBuildName(Job<?, ?> job) {
|
||||
if (job instanceof AbstractProject) {
|
||||
GitLabCommitStatusPublisher publisher =
|
||||
(GitLabCommitStatusPublisher) ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class);
|
||||
if (publisher != null) {
|
||||
return publisher.getName();
|
||||
}
|
||||
} else if (job instanceof WorkflowJob) {
|
||||
GitLabPushTrigger trigger = GitLabPushTrigger.getFromJob(job);
|
||||
if (trigger != null) {
|
||||
return trigger.getPendingBuildName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -45,6 +45,9 @@
|
|||
<f:entry title="Pending build name for pipeline" help="/plugin/gitlab-plugin/help/help-pendingBuildName.html">
|
||||
<f:textbox field="pendingBuildName"/>
|
||||
</f:entry>
|
||||
<f:entry title="Cancel pending merge request builds on update" field="cancelPendingBuildsOnUpdate">
|
||||
<f:checkbox default="false"/>
|
||||
</f:entry>
|
||||
|
||||
<f:entry title="Allowed branches">
|
||||
<table>
|
||||
|
|
|
@ -69,7 +69,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
}
|
||||
});
|
||||
project.setQuietPeriod(0);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), false, false);
|
||||
mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
|
||||
.withObjectAttributes(mergeRequestObjectAttributes().withDescription("[ci-skip]").build())
|
||||
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
|
||||
|
@ -81,7 +81,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, State.opened);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(true));
|
||||
|
@ -89,7 +89,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_build_when_accepted() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.merged), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.merged), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, State.merged);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(true));
|
||||
|
@ -97,7 +97,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_build_when_closed() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.closed), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.closed), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, State.closed);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(true));
|
||||
|
@ -105,7 +105,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_do_not_build_when_accepted() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.updated), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.updated), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, State.merged);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(false));
|
||||
|
@ -113,7 +113,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_do_not_build_when_closed() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.updated), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.updated), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, State.closed);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(false));
|
||||
|
@ -121,7 +121,8 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_build_when_approved() throws IOException, InterruptedException, GitAPIException, ExecutionException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.approved), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.approved), false, false);
|
||||
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, Action.approved);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(true));
|
||||
|
@ -129,7 +130,7 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
@Test
|
||||
public void mergeRequest_do_not_build_when_when_approved() throws Exception {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.update), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.update), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, defaultMergeRequestObjectAttributes().withState(State.opened).withAction(Action.approved));
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is (false));
|
||||
|
@ -148,11 +149,11 @@ public class MergeRequestHookTriggerHandlerImplTest {
|
|||
|
||||
private void mergeRequest_build_only_when_approved(Action action)
|
||||
throws GitAPIException, IOException, InterruptedException {
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.approved), false);
|
||||
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.allOf(State.class), EnumSet.of(Action.approved), false, false);
|
||||
OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, action);
|
||||
|
||||
assertThat(buildTriggered.isSignaled(), is(false));
|
||||
}
|
||||
}
|
||||
|
||||
private OneShotEvent doHandle(MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler, Action action) throws GitAPIException, IOException, InterruptedException {
|
||||
return doHandle(mergeRequestHookTriggerHandler, defaultMergeRequestObjectAttributes().withAction(action));
|
||||
|
|
Loading…
Reference in New Issue