Make it possible to send pending build status to gitlab when a pipeline job is triggered
This commit is contained in:
parent
f36a970149
commit
77fbc95c34
5
pom.xml
5
pom.xml
|
@ -167,6 +167,11 @@
|
|||
<artifactId>workflow-step-api</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-job</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins</groupId>
|
||||
<artifactId>credentials</artifactId>
|
||||
|
|
|
@ -100,6 +100,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
private String targetBranchRegex;
|
||||
private MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig;
|
||||
private volatile Secret secretToken;
|
||||
private String pendingBuildName;
|
||||
|
||||
private transient BranchFilter branchFilter;
|
||||
private transient PushHookTriggerHandler pushHookTriggerHandler;
|
||||
|
@ -120,8 +121,8 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addCiMessage, boolean addVoteOnMergeRequest,
|
||||
boolean acceptMergeRequestOnSuccess, BranchFilterType branchFilterType,
|
||||
String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex,
|
||||
MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig, String secretToken, boolean triggerOnPipelineEvent,
|
||||
boolean triggerOnApprovedMergeRequest) {
|
||||
MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig, String secretToken, boolean triggerOnPipelineEvent,
|
||||
boolean triggerOnApprovedMergeRequest, String pendingBuildName) {
|
||||
this.triggerOnPush = triggerOnPush;
|
||||
this.triggerOnMergeRequest = triggerOnMergeRequest;
|
||||
this.triggerOnAcceptedMergeRequest = triggerOnAcceptedMergeRequest;
|
||||
|
@ -144,6 +145,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
this.mergeRequestLabelFilterConfig = mergeRequestLabelFilterConfig;
|
||||
this.secretToken = Secret.fromString(secretToken);
|
||||
this.triggerOnApprovedMergeRequest = triggerOnApprovedMergeRequest;
|
||||
this.pendingBuildName = pendingBuildName;
|
||||
|
||||
initializeTriggerHandler();
|
||||
initializeBranchFilter();
|
||||
|
@ -273,6 +275,10 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
return secretToken == null ? null : secretToken.getPlainText();
|
||||
}
|
||||
|
||||
public String getPendingBuildName() {
|
||||
return pendingBuildName;
|
||||
}
|
||||
|
||||
@DataBoundSetter
|
||||
public void setTriggerOnPush(boolean triggerOnPush) {
|
||||
this.triggerOnPush = triggerOnPush;
|
||||
|
@ -388,6 +394,11 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
|
|||
this.triggerOnPipelineEvent = triggerOnPipelineEvent;
|
||||
}
|
||||
|
||||
@DataBoundSetter
|
||||
public void setPendingBuildName(String pendingBuildName) {
|
||||
this.pendingBuildName = pendingBuildName;
|
||||
}
|
||||
|
||||
// executes when the Trigger receives a push request
|
||||
public void onPost(final PushHook hook) {
|
||||
if (branchFilter == null) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
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;
|
||||
|
@ -22,7 +23,9 @@ import jenkins.model.Jenkins;
|
|||
import jenkins.model.ParameterizedJobMixIn;
|
||||
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;
|
||||
|
@ -60,9 +63,8 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
protected abstract boolean isCiSkip(H hook);
|
||||
|
||||
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, H hook) {
|
||||
if (job instanceof AbstractProject && ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class) != null) {
|
||||
GitLabCommitStatusPublisher publisher =
|
||||
(GitLabCommitStatusPublisher) ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class);
|
||||
String buildName = resolvePendingBuildName(job);
|
||||
if (StringUtils.isNotBlank(buildName)) {
|
||||
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
BuildStatusUpdate buildStatusUpdate = retrieveBuildStatusUpdate(hook);
|
||||
try {
|
||||
|
@ -72,7 +74,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
String targetUrl =
|
||||
Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber() + "/";
|
||||
client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(),
|
||||
BuildState.pending, buildStatusUpdate.getRef(), publisher.getName(), targetUrl, BuildState.pending.name());
|
||||
BuildState.pending, buildStatusUpdate.getRef(), buildName, targetUrl, BuildState.pending.name());
|
||||
}
|
||||
} catch (WebApplicationException | ProcessingException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
|
||||
|
@ -169,4 +171,20 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,10 @@ import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequest;
|
|||
import com.dabsquared.gitlabjenkins.gitlab.api.model.Project;
|
||||
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.trigger.handler.AbstractWebHookTriggerHandler;
|
||||
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
|
@ -25,6 +24,7 @@ import hudson.triggers.Trigger;
|
|||
import jenkins.model.Jenkins;
|
||||
import jenkins.model.ParameterizedJobMixIn;
|
||||
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.eclipse.jgit.transport.URIish;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
|
@ -153,13 +153,12 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
}
|
||||
|
||||
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, Integer projectId, String commit, String ref) {
|
||||
if (job instanceof AbstractProject && ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class) != null) {
|
||||
GitLabCommitStatusPublisher publisher =
|
||||
(GitLabCommitStatusPublisher) ((AbstractProject) job).getPublishersList().get(GitLabCommitStatusPublisher.class);
|
||||
String buildName = AbstractWebHookTriggerHandler.resolvePendingBuildName(job);
|
||||
if (StringUtils.isNotBlank(buildName)) {
|
||||
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
try {
|
||||
String targetUrl = Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber() + "/";
|
||||
client.changeBuildStatus(projectId, commit, BuildState.pending, ref, publisher.getName(), targetUrl, BuildState.pending.name());
|
||||
client.changeBuildStatus(projectId, commit, BuildState.pending, ref, buildName, targetUrl, BuildState.pending.name());
|
||||
} catch (WebApplicationException | ProcessingException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
|
||||
}
|
||||
|
|
|
@ -42,6 +42,9 @@
|
|||
<f:entry title="Build on successful pipeline events" field="triggerOnPipelineEvent">
|
||||
<f:checkbox default="false"/>
|
||||
</f:entry>
|
||||
<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="Allowed branches">
|
||||
<table>
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<div>
|
||||
<div>
|
||||
<p>Applicable only for pipelines.</p>
|
||||
<p>When filled, a 'pending' build status with the given build name is published to Gitlab when the pipeline is
|
||||
triggered. Further status updates should be defined in pipeline steps.</p>
|
||||
<p>For other types of jobs, the 'Publish build status to Gitlab commit' post-build action can be used.</p>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in New Issue