diff --git a/pom.xml b/pom.xml index e591f64..c2a5d38 100644 --- a/pom.xml +++ b/pom.xml @@ -167,6 +167,11 @@ workflow-step-api 1.15 + + org.jenkins-ci.plugins.workflow + workflow-job + 1.15 + org.jenkins-ci.plugins credentials diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java index 4a77d19..8895158 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java @@ -100,6 +100,7 @@ public class GitLabPushTrigger extends Trigger> { 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> { 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> { 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> { 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> { 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) { diff --git a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java index f4aae4c..871c020 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java @@ -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 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 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 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; + } } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java index 75cf572..bbc4bbc 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java @@ -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); } diff --git a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly index 2c0aae2..d34fc7c 100644 --- a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly +++ b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly @@ -42,6 +42,9 @@ + + + diff --git a/src/main/webapp/help/help-pendingBuildName.html b/src/main/webapp/help/help-pendingBuildName.html new file mode 100644 index 0000000..761aa86 --- /dev/null +++ b/src/main/webapp/help/help-pendingBuildName.html @@ -0,0 +1,8 @@ +
+
+

Applicable only for pipelines.

+

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.

+

For other types of jobs, the 'Publish build status to Gitlab commit' post-build action can be used.

+
+