Add also for MR builds a RevisionParameterAction to force the build of the latest commit of the branch to merge

This commit is contained in:
Robin Müller 2016-03-18 22:03:51 +01:00
parent e9e13d5536
commit ed8d57c1a7
4 changed files with 64 additions and 26 deletions

View File

@ -1,12 +1,17 @@
package com.dabsquared.gitlabjenkins.trigger.handler;
import com.dabsquared.gitlabjenkins.model.PushHook;
import com.dabsquared.gitlabjenkins.model.WebHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import hudson.model.Action;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
import jenkins.model.ParameterizedJobMixIn;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -34,10 +39,24 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected abstract boolean isCiSkip(H hook);
protected abstract Action[] createActions(Job<?, ?> job, H hook);
protected Action[] createActions(Job<?, ?> job, H hook) {
ArrayList<Action> actions = new ArrayList<Action>();
actions.add(createCauseAction(job, hook));
try {
actions.add(createRevisionParameter(hook));
} catch (NoRevisionToBuildException e) {
LOGGER.log(Level.WARNING, "unknown handled situation, dont know what revision to build for req {0} for job {1}",
new Object[]{hook, (job != null ? job.getFullName() : null)});
}
return actions.toArray(new Action[actions.size()]);
}
protected abstract CauseAction createCauseAction(Job<?, ?> job, H hook);
protected abstract String getTargetBranch(H hook);
protected abstract RevisionParameterAction createRevisionParameter(H hook) throws NoRevisionToBuildException;
private void scheduleBuild(Job<?, ?> job, Action[] actions) {
int projectBuildDelay = 0;
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {

View File

@ -1,9 +1,12 @@
package com.dabsquared.gitlabjenkins.trigger.handler.merge;
import com.dabsquared.gitlabjenkins.cause.GitLabMergeCause;
import com.dabsquared.gitlabjenkins.model.Commit;
import com.dabsquared.gitlabjenkins.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.model.ObjectAttributes;
import com.dabsquared.gitlabjenkins.model.PushHook;
import com.dabsquared.gitlabjenkins.model.State;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import com.dabsquared.gitlabjenkins.util.BuildUtil;
@ -11,10 +14,13 @@ import hudson.model.Action;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.git.RevisionParameterAction;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -49,11 +55,6 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
return hook.getObjectAttributes().optDescription().or("").contains("[ci-skip]");
}
@Override
protected Action[] createActions(Job<?, ?> job, MergeRequestHook hook) {
return new Action[] { new CauseAction(createGitLabMergeCause(job, hook)) };
}
@Override
protected String getTargetBranch(MergeRequestHook hook) {
return hook.getObjectAttributes().optTargetBranch().orNull();
@ -64,6 +65,16 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
return "merge request";
}
@Override
protected CauseAction createCauseAction(Job<?, ?> job, MergeRequestHook hook) {
return new CauseAction(createGitLabMergeCause(job, hook));
}
@Override
protected RevisionParameterAction createRevisionParameter(MergeRequestHook hook) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
}
private GitLabMergeCause createGitLabMergeCause(Job<?, ?> job, MergeRequestHook mergeRequestHook) {
try {
return new GitLabMergeCause(mergeRequestHook, new File(job.getRootDir(), "gitlab-polling.log"));
@ -72,6 +83,23 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
}
}
private String retrieveRevisionToBuild(MergeRequestHook hook) throws NoRevisionToBuildException {
if (hook.getObjectAttributes().getLastCommit().optId().isPresent()) {
return hook.getObjectAttributes().getLastCommit().optId().get();
} else {
throw new NoRevisionToBuildException();
}
}
private URIish retrieveUrIish(MergeRequestHook hook) {
try {
return new URIish(hook.getRepository().optUrl().orNull());
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not parse URL");
return null;
}
}
private boolean isLastCommitNotYetBuild(Job<?, ?> project, MergeRequestHook hook) {
ObjectAttributes objectAttributes = hook.getObjectAttributes();
if (objectAttributes.optLastCommit().isPresent()) {

View File

@ -37,16 +37,9 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
return !commits.isEmpty() && commits.get(0).optMessage().or("").contains("[ci-skip]");
}
protected Action[] createActions(Job<?, ?> job, PushHook hook) {
ArrayList<Action> actions = new ArrayList<Action>();
actions.add(new CauseAction(createGitLabPushCause(job, hook)));
try {
actions.add(createPushRequestRevisionParameter(hook));
} catch (NoRevisionToBuildException e) {
LOGGER.log(Level.WARNING, "unknown handled situation, dont know what revision to build for req {0} for job {1}",
new Object[]{hook, (job != null ? job.getFullName() : null)});
}
return actions.toArray(new Action[actions.size()]);
@Override
protected CauseAction createCauseAction(Job<?, ?> job, PushHook hook) {
return new CauseAction(createGitLabPushCause(job, hook));
}
@Override
@ -67,7 +60,8 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
}
}
RevisionParameterAction createPushRequestRevisionParameter(PushHook hook) throws NoRevisionToBuildException {
@Override
protected RevisionParameterAction createRevisionParameter(PushHook hook) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
}

View File

@ -7,7 +7,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.model.PushHook;
import com.dabsquared.gitlabjenkins.testhelpers.GitLabPushRequestSamples_7_10_5_489b413;
import com.dabsquared.gitlabjenkins.testhelpers.GitLabPushRequestSamples_7_5_1_36679b5;
@ -15,11 +14,9 @@ import com.dabsquared.gitlabjenkins.testhelpers.GitLabPushRequestSamples_8_1_2_8
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import org.eclipse.jgit.transport.RemoteConfig;
import org.junit.Rule;
import org.junit.Test;
import com.dabsquared.gitlabjenkins.testhelpers.GitLabPushRequestSamples;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
@ -46,7 +43,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
public void createRevisionParameterAction_pushBrandNewMasterBranchRequest(GitLabPushRequestSamples samples) throws Exception {
PushHook hook = samples.pushBrandNewMasterBranchRequest();
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
@ -57,7 +54,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
public void createRevisionParameterAction_mergeRequestMergePushRequest(GitLabPushRequestSamples samples) throws Exception {
PushHook hook = samples.mergePushRequest();
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
@ -68,7 +65,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
public void createRevisionParameterAction_pushCommitRequest(GitLabPushRequestSamples samples) throws Exception {
PushHook hook = samples.pushCommitRequest();
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
@ -79,7 +76,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
public void createRevisionParameterAction_pushNewBranchRequest(GitLabPushRequestSamples samples) throws Exception {
PushHook hook = samples.pushNewBranchRequest();
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
@ -90,7 +87,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
public void createRevisionParameterAction_pushNewTagRequest(GitLabPushRequestSamples samples) throws Exception {
PushHook hook = samples.pushNewTagRequest();
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
@ -102,6 +99,6 @@ public class PushHookTriggerHandlerGitlabServerTest {
PushHook hook = samples.deleteBranchRequest();
exception.expect(NoRevisionToBuildException.class);
new PushHookTriggerHandlerImpl().createPushRequestRevisionParameter(hook);
new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
}
}