diff --git a/pom.xml b/pom.xml index bc9f3ada5..a8c84edf1 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 1.609.3 - 1.115 + 2.1 ${jenkins.version} false diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImplTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImplTest.java index 7fe2468fe..0cbc52c62 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImplTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImplTest.java @@ -1,6 +1,8 @@ package com.dabsquared.gitlabjenkins.trigger.handler.merge; +import com.dabsquared.gitlabjenkins.gitlab.hook.model.Action; import com.dabsquared.gitlabjenkins.gitlab.hook.model.State; +import com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.MergeRequestObjectAttributesBuilder; import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory; import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterType; import hudson.Launcher; @@ -10,7 +12,15 @@ import hudson.plugins.git.GitSCM; import hudson.util.OneShotEvent; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.NoHeadException; +import org.eclipse.jgit.api.errors.NoMessageException; +import org.eclipse.jgit.api.errors.UnmergedPathsException; +import org.eclipse.jgit.api.errors.WrongRepositoryStateException; +import org.eclipse.jgit.errors.AmbiguousObjectException; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; +import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.revwalk.RevCommit; @@ -23,6 +33,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.EnumSet; import java.util.concurrent.ExecutionException; import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.CommitBuilder.commit; @@ -107,9 +118,47 @@ public void mergeRequest_do_not_build_when_closed() throws IOException, Interrup assertThat(buildTriggered.isSignaled(), is(false)); } + + @Test + public void mergeRequest_build_when_approved() throws IOException, InterruptedException, GitAPIException, ExecutionException { + MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.noneOf(State.class), EnumSet.of(Action.approved), false); + OneShotEvent buildTriggered = doHandle(mergeRequestHookTriggerHandler, Action.approved); + + assertThat(buildTriggered.isSignaled(), is(true)); + } + + @Test + public void mergeRequest_build_only_when_approved_and_not_when_opened() throws IOException, InterruptedException, GitAPIException, ExecutionException { + mergeRequest_build_only_when_approved(Action.open); + } + + @Test + public void mergeRequest_build_only_when_approved_and_not_when_updated() throws IOException, InterruptedException, GitAPIException, ExecutionException { + mergeRequest_build_only_when_approved(Action.update); + } + + + private void mergeRequest_build_only_when_approved(Action action) + throws GitAPIException, IOException, InterruptedException { + MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(EnumSet.noneOf(State.class), EnumSet.of(Action.approved), 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().withState(State.opened).withAction(action)); + } private OneShotEvent doHandle(MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler, State state) throws GitAPIException, IOException, InterruptedException { - Git.init().setDirectory(tmp.getRoot()).call(); + return doHandle(mergeRequestHookTriggerHandler, defaultMergeRequestObjectAttributes().withState(state)); + } + + private OneShotEvent doHandle(MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler, + MergeRequestObjectAttributesBuilder objectAttributes) throws GitAPIException, IOException, NoHeadException, + NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, + AmbiguousObjectException, IncorrectObjectTypeException, MissingObjectException, InterruptedException { + Git.init().setDirectory(tmp.getRoot()).call(); tmp.newFile("test"); Git git = Git.open(tmp.getRoot()); git.add().addFilepattern("test"); @@ -128,33 +177,10 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen } }); project.setQuietPeriod(0); - mergeRequestHookTriggerHandler.handle(project, mergeRequestHook() - .withObjectAttributes(mergeRequestObjectAttributes() - .withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head)) - .withState(state) - .withIid(1) - .withTitle("test") - .withTargetProjectId(1) - .withSourceProjectId(1) - .withSourceBranch("feature") - .withTargetBranch("master") - .withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build()) - .withSource(project() - .withName("test") - .withNamespace("test-namespace") - .withHomepage("https://gitlab.org/test") - .withUrl("git@gitlab.org:test.git") - .withSshUrl("git@gitlab.org:test.git") - .withHttpUrl("https://gitlab.org/test.git") - .build()) - .withTarget(project() - .withName("test") - .withNamespace("test-namespace") - .withHomepage("https://gitlab.org/test") - .withUrl("git@gitlab.org:test.git") - .withSshUrl("git@gitlab.org:test.git") - .withHttpUrl("https://gitlab.org/test.git") - .build()) + mergeRequestHookTriggerHandler.handle(project, mergeRequestHook() + .withObjectAttributes(objectAttributes + .withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head)) + .withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build()) .build()) .withProject(project() .withWebUrl("https://gitlab.org/test.git") @@ -165,6 +191,32 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen buildTriggered.block(10000); return buildTriggered; - } + } + + private MergeRequestObjectAttributesBuilder defaultMergeRequestObjectAttributes() { + return mergeRequestObjectAttributes() + .withIid(1) + .withTitle("test") + .withTargetProjectId(1) + .withSourceProjectId(1) + .withSourceBranch("feature") + .withTargetBranch("master") + .withSource(project() + .withName("test") + .withNamespace("test-namespace") + .withHomepage("https://gitlab.org/test") + .withUrl("git@gitlab.org:test.git") + .withSshUrl("git@gitlab.org:test.git") + .withHttpUrl("https://gitlab.org/test.git") + .build()) + .withTarget(project() + .withName("test") + .withNamespace("test-namespace") + .withHomepage("https://gitlab.org/test") + .withUrl("git@gitlab.org:test.git") + .withSshUrl("git@gitlab.org:test.git") + .withHttpUrl("https://gitlab.org/test.git") + .build()); + } }