Send build status pending before scheduling a build for jobs that have a GitLabCommitStatusPublisher configured (Fixes #317)
This commit is contained in:
parent
acf459ad5a
commit
9386f05d09
|
@ -16,7 +16,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
*/
|
||||
public final class CauseData {
|
||||
private final ActionType actionType;
|
||||
private final Integer projectId;
|
||||
private final Integer sourceProjectId;
|
||||
private final Integer targetProjectId;
|
||||
private final String branch;
|
||||
private final String sourceBranch;
|
||||
private final String userName;
|
||||
|
@ -39,13 +40,14 @@ public final class CauseData {
|
|||
private final String triggeredByUser;
|
||||
|
||||
@GeneratePojoBuilder(withFactoryMethod = "*")
|
||||
CauseData(ActionType actionType, Integer projectId, String branch, String sourceBranch, String userName, String userEmail,
|
||||
String sourceRepoHomepage, String sourceRepoName, String sourceNamespace, String sourceRepoUrl, String sourceRepoSshUrl,
|
||||
String sourceRepoHttpUrl, String mergeRequestTitle, String mergeRequestDescription, Integer mergeRequestId, Integer mergeRequestIid,
|
||||
String targetBranch, String targetRepoName, String targetNamespace, String targetRepoSshUrl, String targetRepoHttpUrl,
|
||||
String triggeredByUser) {
|
||||
CauseData(ActionType actionType, Integer sourceProjectId, Integer targetProjectId, String branch, String sourceBranch, String userName,
|
||||
String userEmail, String sourceRepoHomepage, String sourceRepoName, String sourceNamespace, String sourceRepoUrl,
|
||||
String sourceRepoSshUrl, String sourceRepoHttpUrl, String mergeRequestTitle, String mergeRequestDescription, Integer mergeRequestId,
|
||||
Integer mergeRequestIid, String targetBranch, String targetRepoName, String targetNamespace, String targetRepoSshUrl,
|
||||
String targetRepoHttpUrl, String triggeredByUser) {
|
||||
this.actionType = checkNotNull(actionType, "actionType must not be null.");
|
||||
this.projectId = checkNotNull(projectId, "projectId must not be null.");
|
||||
this.sourceProjectId = checkNotNull(sourceProjectId, "sourceProjectId must not be null.");
|
||||
this.targetProjectId = checkNotNull(targetProjectId, "targetProjectId must not be null.");
|
||||
this.branch = checkNotNull(branch, "branch must not be null.");
|
||||
this.sourceBranch = checkNotNull(sourceBranch, "sourceBranch must not be null.");
|
||||
this.userName = checkNotNull(userName, "userName must not be null.");
|
||||
|
@ -93,8 +95,12 @@ public final class CauseData {
|
|||
return variables;
|
||||
}
|
||||
|
||||
public Integer getProjectId() {
|
||||
return projectId;
|
||||
public Integer getSourceProjectId() {
|
||||
return sourceProjectId;
|
||||
}
|
||||
|
||||
public Integer getTargetProjectId() {
|
||||
return targetProjectId;
|
||||
}
|
||||
|
||||
public String getBranch() {
|
||||
|
@ -195,79 +201,88 @@ public final class CauseData {
|
|||
}
|
||||
CauseData causeData = (CauseData) o;
|
||||
return new EqualsBuilder()
|
||||
.append(projectId, causeData.projectId)
|
||||
.append(branch, causeData.branch)
|
||||
.append(sourceBranch, causeData.sourceBranch)
|
||||
.append(actionType, causeData.actionType)
|
||||
.append(userName, causeData.userName)
|
||||
.append(userEmail, causeData.userEmail)
|
||||
.append(sourceRepoHomepage, causeData.sourceRepoHomepage)
|
||||
.append(sourceRepoName, causeData.sourceRepoName)
|
||||
.append(sourceRepoUrl, causeData.sourceRepoUrl)
|
||||
.append(sourceRepoSshUrl, causeData.sourceRepoSshUrl)
|
||||
.append(sourceRepoHttpUrl, causeData.sourceRepoHttpUrl)
|
||||
.append(mergeRequestTitle, causeData.mergeRequestTitle)
|
||||
.append(mergeRequestDescription, causeData.mergeRequestDescription)
|
||||
.append(mergeRequestId, causeData.mergeRequestId)
|
||||
.append(mergeRequestIid, causeData.mergeRequestIid)
|
||||
.append(targetBranch, causeData.targetBranch)
|
||||
.append(targetRepoName, causeData.targetRepoName)
|
||||
.append(targetRepoSshUrl, causeData.targetRepoSshUrl)
|
||||
.append(targetRepoHttpUrl, causeData.targetRepoHttpUrl)
|
||||
.append(triggeredByUser, causeData.triggeredByUser)
|
||||
.isEquals();
|
||||
.append(actionType, causeData.actionType)
|
||||
.append(sourceProjectId, causeData.sourceProjectId)
|
||||
.append(targetProjectId, causeData.targetProjectId)
|
||||
.append(branch, causeData.branch)
|
||||
.append(sourceBranch, causeData.sourceBranch)
|
||||
.append(userName, causeData.userName)
|
||||
.append(userEmail, causeData.userEmail)
|
||||
.append(sourceRepoHomepage, causeData.sourceRepoHomepage)
|
||||
.append(sourceRepoName, causeData.sourceRepoName)
|
||||
.append(sourceNamespace, causeData.sourceNamespace)
|
||||
.append(sourceRepoUrl, causeData.sourceRepoUrl)
|
||||
.append(sourceRepoSshUrl, causeData.sourceRepoSshUrl)
|
||||
.append(sourceRepoHttpUrl, causeData.sourceRepoHttpUrl)
|
||||
.append(mergeRequestTitle, causeData.mergeRequestTitle)
|
||||
.append(mergeRequestDescription, causeData.mergeRequestDescription)
|
||||
.append(mergeRequestId, causeData.mergeRequestId)
|
||||
.append(mergeRequestIid, causeData.mergeRequestIid)
|
||||
.append(targetBranch, causeData.targetBranch)
|
||||
.append(targetRepoName, causeData.targetRepoName)
|
||||
.append(targetNamespace, causeData.targetNamespace)
|
||||
.append(targetRepoSshUrl, causeData.targetRepoSshUrl)
|
||||
.append(targetRepoHttpUrl, causeData.targetRepoHttpUrl)
|
||||
.append(triggeredByUser, causeData.triggeredByUser)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(projectId)
|
||||
.append(branch)
|
||||
.append(sourceBranch)
|
||||
.append(actionType)
|
||||
.append(userName)
|
||||
.append(userEmail)
|
||||
.append(sourceRepoHomepage)
|
||||
.append(sourceRepoName)
|
||||
.append(sourceRepoUrl)
|
||||
.append(sourceRepoSshUrl)
|
||||
.append(sourceRepoHttpUrl)
|
||||
.append(mergeRequestTitle)
|
||||
.append(mergeRequestDescription)
|
||||
.append(mergeRequestId)
|
||||
.append(mergeRequestIid)
|
||||
.append(targetBranch)
|
||||
.append(targetRepoName)
|
||||
.append(targetRepoSshUrl)
|
||||
.append(targetRepoHttpUrl)
|
||||
.append(triggeredByUser)
|
||||
.toHashCode();
|
||||
.append(actionType)
|
||||
.append(sourceProjectId)
|
||||
.append(targetProjectId)
|
||||
.append(branch)
|
||||
.append(sourceBranch)
|
||||
.append(userName)
|
||||
.append(userEmail)
|
||||
.append(sourceRepoHomepage)
|
||||
.append(sourceRepoName)
|
||||
.append(sourceNamespace)
|
||||
.append(sourceRepoUrl)
|
||||
.append(sourceRepoSshUrl)
|
||||
.append(sourceRepoHttpUrl)
|
||||
.append(mergeRequestTitle)
|
||||
.append(mergeRequestDescription)
|
||||
.append(mergeRequestId)
|
||||
.append(mergeRequestIid)
|
||||
.append(targetBranch)
|
||||
.append(targetRepoName)
|
||||
.append(targetNamespace)
|
||||
.append(targetRepoSshUrl)
|
||||
.append(targetRepoHttpUrl)
|
||||
.append(triggeredByUser)
|
||||
.toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this)
|
||||
.append("projectId", projectId)
|
||||
.append("branch", branch)
|
||||
.append("sourceBranch", sourceBranch)
|
||||
.append("actionType", actionType)
|
||||
.append("userName", userName)
|
||||
.append("userEmail", userEmail)
|
||||
.append("sourceRepoHomepage", sourceRepoHomepage)
|
||||
.append("sourceRepoName", sourceRepoName)
|
||||
.append("sourceRepoUrl", sourceRepoUrl)
|
||||
.append("sourceRepoSshUrl", sourceRepoSshUrl)
|
||||
.append("sourceRepoHttpUrl", sourceRepoHttpUrl)
|
||||
.append("mergeRequestTitle", mergeRequestTitle)
|
||||
.append("mergeRequestDescription", mergeRequestDescription)
|
||||
.append("mergeRequestId", mergeRequestId)
|
||||
.append("mergeRequestIid", mergeRequestIid)
|
||||
.append("targetBranch", targetBranch)
|
||||
.append("targetRepoName", targetRepoName)
|
||||
.append("targetRepoSshUrl", targetRepoSshUrl)
|
||||
.append("targetRepoHttpUrl", targetRepoHttpUrl)
|
||||
.append("triggeredByUser", triggeredByUser)
|
||||
.toString();
|
||||
.append("actionType", actionType)
|
||||
.append("sourceProjectId", sourceProjectId)
|
||||
.append("targetProjectId", targetProjectId)
|
||||
.append("branch", branch)
|
||||
.append("sourceBranch", sourceBranch)
|
||||
.append("userName", userName)
|
||||
.append("userEmail", userEmail)
|
||||
.append("sourceRepoHomepage", sourceRepoHomepage)
|
||||
.append("sourceRepoName", sourceRepoName)
|
||||
.append("sourceNamespace", sourceNamespace)
|
||||
.append("sourceRepoUrl", sourceRepoUrl)
|
||||
.append("sourceRepoSshUrl", sourceRepoSshUrl)
|
||||
.append("sourceRepoHttpUrl", sourceRepoHttpUrl)
|
||||
.append("mergeRequestTitle", mergeRequestTitle)
|
||||
.append("mergeRequestDescription", mergeRequestDescription)
|
||||
.append("mergeRequestId", mergeRequestId)
|
||||
.append("mergeRequestIid", mergeRequestIid)
|
||||
.append("targetBranch", targetBranch)
|
||||
.append("targetRepoName", targetRepoName)
|
||||
.append("targetNamespace", targetNamespace)
|
||||
.append("targetRepoSshUrl", targetRepoSshUrl)
|
||||
.append("targetRepoHttpUrl", targetRepoHttpUrl)
|
||||
.append("triggeredByUser", triggeredByUser)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public enum ActionType {
|
||||
|
|
|
@ -75,6 +75,17 @@ public interface GitLabApi {
|
|||
@QueryParam("target_url") String targetUrl,
|
||||
@QueryParam("description") String description);
|
||||
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/projects/{projectId}/statuses/{sha}")
|
||||
void changeBuildStatus(@PathParam("projectId") Integer projectId,
|
||||
@PathParam("sha") String sha,
|
||||
@QueryParam("state") BuildState state,
|
||||
@QueryParam("ref") String ref,
|
||||
@QueryParam("context") String context,
|
||||
@QueryParam("target_url") String targetUrl,
|
||||
@QueryParam("description") String description);
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/projects/{projectId}/repository/commits/{sha}")
|
||||
|
|
|
@ -31,7 +31,7 @@ public class GitLabMergeRequestRunListener extends RunListener<Run<?, ?>> {
|
|||
if (trigger != null && cause != null && (cause.getData().getActionType() == CauseData.ActionType.MERGE || cause.getData().getActionType() == CauseData.ActionType.NOTE)) {
|
||||
String buildUrl = getBuildUrl(build);
|
||||
Result buildResult = build.getResult();
|
||||
Integer projectId = cause.getData().getProjectId();
|
||||
Integer projectId = cause.getData().getTargetProjectId();
|
||||
Integer mergeRequestId = cause.getData().getMergeRequestId();
|
||||
addNoteOnMergeRequestIfNecessary(build, trigger, listener, projectId.toString(), mergeRequestId, build.getParent().getDisplayName(), build.getNumber(),
|
||||
buildUrl, getResultIcon(trigger, buildResult), buildResult.color.getDescription());
|
||||
|
|
|
@ -54,6 +54,10 @@ public class GitLabCommitStatusPublisher extends Notifier {
|
|||
return true;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
protected GitLabCommitStatusPublisher readResolve() {
|
||||
if (name == null) {
|
||||
name = "jenkins";
|
||||
|
|
|
@ -2,17 +2,26 @@ package com.dabsquared.gitlabjenkins.trigger.handler;
|
|||
|
||||
import com.dabsquared.gitlabjenkins.cause.CauseData;
|
||||
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
|
||||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
|
||||
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.util.LoggerUtil;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
import hudson.plugins.git.RevisionParameterAction;
|
||||
import jenkins.model.Jenkins;
|
||||
import jenkins.model.ParameterizedJobMixIn;
|
||||
import net.karneim.pojobuilder.GeneratePojoBuilder;
|
||||
import org.eclipse.jgit.transport.URIish;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Level;
|
||||
|
@ -35,6 +44,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
String targetBranch = getTargetBranch(hook);
|
||||
if (branchFilter.isBranchAllowed(targetBranch)) {
|
||||
LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType()));
|
||||
setCommitStatusPendingIfNecessary(job, hook);
|
||||
scheduleBuild(job, createActions(job, hook));
|
||||
} else {
|
||||
LOGGER.log(Level.INFO, "branch {0} is not allowed", targetBranch);
|
||||
|
@ -45,6 +55,21 @@ 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);
|
||||
GitLabApi client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
BuildStatusUpdate buildStatusUpdate = retrieveBuildStatusUpdate(hook);
|
||||
try {
|
||||
client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(), BuildState.pending, buildStatusUpdate.getRef(),
|
||||
publisher.getName(), Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber(), null);
|
||||
} catch (WebApplicationException | ProcessingException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Action[] createActions(Job<?, ?> job, H hook) {
|
||||
ArrayList<Action> actions = new ArrayList<>();
|
||||
actions.add(new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook))));
|
||||
|
@ -63,6 +88,8 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
|
||||
protected abstract RevisionParameterAction createRevisionParameter(H hook) throws NoRevisionToBuildException;
|
||||
|
||||
protected abstract BuildStatusUpdate retrieveBuildStatusUpdate(H hook);
|
||||
|
||||
protected URIish retrieveUrIish(WebHook hook) {
|
||||
try {
|
||||
if (hook.getRepository() != null) {
|
||||
|
@ -94,4 +121,29 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static class BuildStatusUpdate {
|
||||
private final Integer projectId;
|
||||
private final String sha;
|
||||
private final String ref;
|
||||
|
||||
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
|
||||
public BuildStatusUpdate(Integer projectId, String sha, String ref) {
|
||||
this.projectId = projectId;
|
||||
this.sha = sha;
|
||||
this.ref = ref;
|
||||
}
|
||||
|
||||
public Integer getProjectId() {
|
||||
return projectId;
|
||||
}
|
||||
|
||||
public String getSha() {
|
||||
return sha;
|
||||
}
|
||||
|
||||
public String getRef() {
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import java.util.logging.Level;
|
|||
import java.util.logging.Logger;
|
||||
|
||||
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
|
||||
import static com.dabsquared.gitlabjenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
|
||||
|
||||
/**
|
||||
* @author Robin Müller
|
||||
|
@ -61,7 +62,8 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
|
|||
protected CauseData retrieveCauseData(MergeRequestHook hook) {
|
||||
return causeData()
|
||||
.withActionType(CauseData.ActionType.MERGE)
|
||||
.withProjectId(hook.getObjectAttributes().getTargetProjectId())
|
||||
.withSourceProjectId(hook.getObjectAttributes().getSourceProjectId())
|
||||
.withTargetProjectId(hook.getObjectAttributes().getTargetProjectId())
|
||||
.withBranch(hook.getObjectAttributes().getSourceBranch())
|
||||
.withSourceBranch(hook.getObjectAttributes().getSourceBranch())
|
||||
.withUserName(hook.getObjectAttributes().getLastCommit().getAuthor().getName())
|
||||
|
@ -90,6 +92,15 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
|
|||
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BuildStatusUpdate retrieveBuildStatusUpdate(MergeRequestHook hook) {
|
||||
return buildStatusUpdate()
|
||||
.withProjectId(hook.getObjectAttributes().getSourceProjectId())
|
||||
.withSha(hook.getObjectAttributes().getLastCommit().getId())
|
||||
.withRef(hook.getObjectAttributes().getSourceBranch())
|
||||
.build();
|
||||
}
|
||||
|
||||
private String retrieveRevisionToBuild(MergeRequestHook hook) throws NoRevisionToBuildException {
|
||||
if (hook.getObjectAttributes() != null
|
||||
&& hook.getObjectAttributes().getLastCommit() != null
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.util.logging.Logger;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
|
||||
import static com.dabsquared.gitlabjenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
|
||||
|
||||
/**
|
||||
* @author Nikolay Ustinov
|
||||
|
@ -55,7 +56,8 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
|
|||
protected CauseData retrieveCauseData(NoteHook hook) {
|
||||
return causeData()
|
||||
.withActionType(CauseData.ActionType.NOTE)
|
||||
.withProjectId(hook.getMergeRequest().getTargetProjectId())
|
||||
.withSourceProjectId(hook.getMergeRequest().getSourceProjectId())
|
||||
.withTargetProjectId(hook.getMergeRequest().getTargetProjectId())
|
||||
.withBranch(hook.getMergeRequest().getSourceBranch())
|
||||
.withSourceBranch(hook.getMergeRequest().getSourceBranch())
|
||||
.withUserName(hook.getMergeRequest().getLastCommit().getAuthor().getName())
|
||||
|
@ -84,6 +86,15 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
|
|||
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BuildStatusUpdate retrieveBuildStatusUpdate(NoteHook hook) {
|
||||
return buildStatusUpdate()
|
||||
.withProjectId(hook.getMergeRequest().getSourceProjectId())
|
||||
.withSha(hook.getMergeRequest().getLastCommit().getId())
|
||||
.withRef(hook.getMergeRequest().getSourceBranch())
|
||||
.build();
|
||||
}
|
||||
|
||||
private String retrieveRevisionToBuild(NoteHook hook) throws NoRevisionToBuildException {
|
||||
if (hook.getMergeRequest() != null
|
||||
&& hook.getMergeRequest().getLastCommit() != null
|
||||
|
|
|
@ -6,16 +6,19 @@ import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
|
|||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.model.Branch;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.api.model.BuildState;
|
||||
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.util.LoggerUtil;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
import jenkins.model.Jenkins;
|
||||
import jenkins.model.ParameterizedJobMixIn;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
|
@ -45,7 +48,7 @@ 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, projectId, mergeRequest);
|
||||
handleMergeRequest(job, hook, ciSkip, branchFilter, client, mergeRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +68,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
return result;
|
||||
}
|
||||
|
||||
private void handleMergeRequest(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, GitLabApi client, Integer projectId, MergeRequest mergeRequest) {
|
||||
private void handleMergeRequest(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, GitLabApi client, MergeRequest mergeRequest) {
|
||||
if (ciSkip && mergeRequest.getDescription() != null && mergeRequest.getDescription().contains("[ci-skip]")) {
|
||||
LOGGER.log(Level.INFO, "Skipping MR " + mergeRequest.getTitle() + " due to ci-skip.");
|
||||
return;
|
||||
|
@ -78,6 +81,8 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
|
||||
Branch branch = client.getBranch(mergeRequest.getSourceProjectId().toString(), sourceBranch);
|
||||
Project project = client.getProject(mergeRequest.getSourceProjectId().toString());
|
||||
String commit = branch.getCommit().getId();
|
||||
setCommitStatusPendingIfNecessary(job, mergeRequest.getSourceProjectId(), commit, branch.getName());
|
||||
scheduleBuild(job, new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook, project, mergeRequest, branch))));
|
||||
}
|
||||
}
|
||||
|
@ -85,7 +90,8 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
private CauseData retrieveCauseData(PushHook hook, Project project, MergeRequest mergeRequest, Branch branch) {
|
||||
return causeData()
|
||||
.withActionType(CauseData.ActionType.MERGE)
|
||||
.withProjectId(hook.getProjectId())
|
||||
.withSourceProjectId(mergeRequest.getSourceProjectId())
|
||||
.withTargetProjectId(hook.getProjectId())
|
||||
.withBranch(branch.getName())
|
||||
.withSourceBranch(branch.getName())
|
||||
.withUserName(branch.getCommit().getAuthorName())
|
||||
|
@ -109,6 +115,20 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
|
|||
.build();
|
||||
}
|
||||
|
||||
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);
|
||||
GitLabApi client = job.getProperty(GitLabConnectionProperty.class).getClient();
|
||||
try {
|
||||
client.changeBuildStatus(projectId, commit, BuildState.pending, ref, publisher.getName(),
|
||||
Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber(), null);
|
||||
} catch (WebApplicationException | ProcessingException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void scheduleBuild(Job<?, ?> job, Action action) {
|
||||
int projectBuildDelay = 0;
|
||||
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.eclipse.jgit.util.StringUtils;
|
|||
import java.util.List;
|
||||
|
||||
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
|
||||
import static com.dabsquared.gitlabjenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
|
||||
|
||||
/**
|
||||
* @author Robin Müller
|
||||
|
@ -41,7 +42,8 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
|
|||
protected CauseData retrieveCauseData(PushHook hook) {
|
||||
return causeData()
|
||||
.withActionType(CauseData.ActionType.PUSH)
|
||||
.withProjectId(hook.getProjectId())
|
||||
.withSourceProjectId(hook.getProjectId())
|
||||
.withTargetProjectId(hook.getProjectId())
|
||||
.withBranch(getTargetBranch(hook))
|
||||
.withSourceBranch(getTargetBranch(hook))
|
||||
.withUserName(hook.getUserName())
|
||||
|
@ -80,6 +82,15 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
|
|||
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BuildStatusUpdate retrieveBuildStatusUpdate(PushHook hook) {
|
||||
return buildStatusUpdate()
|
||||
.withProjectId(hook.getProjectId())
|
||||
.withSha(hook.getAfter())
|
||||
.withRef(getTargetBranch(hook))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String retrievePushedBy(final PushHook hook) {
|
||||
|
||||
final String userName = hook.getUserName();
|
||||
|
|
|
@ -17,6 +17,7 @@ import javax.ws.rs.ProcessingException;
|
|||
import javax.ws.rs.WebApplicationException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
@ -113,9 +114,13 @@ public class CommitStatusUpdater {
|
|||
}
|
||||
|
||||
private static List<String> retrieveGitlabProjectIds(Run<?, ?> build, EnvVars environment) {
|
||||
|
||||
LOGGER.log(Level.INFO, "Retrieving gitlab project ids");
|
||||
|
||||
GitLabWebHookCause cause = build.getCause(GitLabWebHookCause.class);
|
||||
if (cause != null) {
|
||||
return Collections.singletonList(cause.getData().getSourceProjectId().toString());
|
||||
}
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
GitLabApi gitLabClient = getClient(build);
|
||||
if (gitLabClient == null) {
|
||||
|
|
|
@ -111,7 +111,8 @@ public class MergeRequestBuildActionTest {
|
|||
testProject.setScm(new GitSCM(gitRepoUrl));
|
||||
QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new GitLabWebHookCause(causeData()
|
||||
.withActionType(CauseData.ActionType.MERGE)
|
||||
.withProjectId(1)
|
||||
.withSourceProjectId(1)
|
||||
.withTargetProjectId(1)
|
||||
.withBranch("feature")
|
||||
.withSourceBranch("feature")
|
||||
.withUserName("")
|
||||
|
|
|
@ -99,7 +99,8 @@ public class NoteBuildActionTest {
|
|||
testProject.setScm(new GitSCM(gitRepoUrl));
|
||||
QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new GitLabWebHookCause(causeData()
|
||||
.withActionType(CauseData.ActionType.NOTE)
|
||||
.withProjectId(1)
|
||||
.withSourceProjectId(1)
|
||||
.withTargetProjectId(1)
|
||||
.withBranch("feature")
|
||||
.withSourceBranch("feature")
|
||||
.withUserName("")
|
||||
|
|
Loading…
Reference in New Issue