diff --git a/CHANGELOG.md b/CHANGELOG.md index c0e946453..d430dab47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ ChangeLog -1.5.14 - to be released +1.5.15 - 1.5.19 +===================== +**TODO** + +1.5.14 ===================== * #866: Better compatibility with Configuration as Code plugin (useAuthenticatedEndpoint) diff --git a/README.md b/README.md index 0140c275a..5211a6e6e 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ This plugin allows GitLab to trigger builds in Jenkins when code is committed or ### Seeking maintainers -I am no longer using GitLab on a daily basis, and therefore I have less time to spend working on the plugin. If you are a regular user and would like to help out, please consider volunteering as a maintainer. There are verified bugs that need fixes, open PRs that need review, and feature requests that range from simple to complex. If you are interested in contributing, contact Marky (email address in git log) for additional access. +**This plugin is not maintained!** I no longer have the time or available infrastructure to test PRs. If you are willing to invest some time in this, please comment on https://github.com/jenkinsci/gitlab-plugin/issues/1115 to request access. # User support ### Relationship with GitLab Inc. @@ -171,23 +171,24 @@ In your job configuration, click 'This build is parameterized' and add any param In the Groovy Script field insert something similar to: ``` -import hudson.model.* -def env = Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars') +def env = currentBuild.getEnvironment(currentListener) def map = [:] -if (env['gitlabSourceBranch'] != null) { - map['sourceBranch'] = env['gitlabSourceBranch'] +if (env.gitlabSourceBranch != null) { + map['sourceBranch'] = env.gitlabSourceBranch } -if (env['gitlabTargetBranch'] != null) { - map['targetBranch'] = env['gitlabTargetBranch'] + +if (env.gitlabTargetBranch != null) { + map['targetBranch'] = env.gitlabTargetBranch } -// Add additional entries for any other parameters you have created return map ``` You can then reference these variables in your job config, e.g. as `${sourceBranch}`. You will need to update this code anytime you add or remove parameters. +Note: If you use the Groovy Sandbox, you might need to approve the script yourself or let an administrator approve the script in the Jenkins configuration. + ## Git configuration ### Freestyle jobs In the *Source Code Management* section: @@ -213,9 +214,7 @@ In the *Source Code Management* section: checkout changelog: true, poll: true, scm: [ $class: 'GitSCM', branches: [[name: "origin/${env.gitlabSourceBranch}"]], - doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'origin', mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]], - submoduleCfg: [], userRemoteConfigs: [[name: 'origin', url: 'git@gitlab.example.com:foo/testrepo.git']] ] ``` diff --git a/pom.xml b/pom.xml index bc18ebfdc..5a27245d7 100644 --- a/pom.xml +++ b/pom.xml @@ -3,23 +3,27 @@ org.jenkins-ci.plugins plugin - 4.2 + 4.16 + + gitlab-plugin + 1.5.20-SNAPSHOT + GitLab Plugin + https://github.com/jenkinsci/gitlab-plugin/ + hpi + 1.5.18 + -SNAPSHOT 8 - 2.190.1 - false - 1.36 + 2.222.4 + false + 1.46 1.4.0 - 2.11.0 + 2.12.1 + 2.71 - gitlab-plugin - 1.5.17-SNAPSHOT - GitLab Plugin - https://wiki.jenkins-ci.org/display/JENKINS/GitLab+Plugin - hpi @@ -77,6 +81,10 @@ repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ + + repo.jenkins-ci.org-r + https://repo.jenkins-ci.org/releases/ + @@ -84,119 +92,80 @@ org.jenkins-ci.plugins git - 3.2.0 org.jenkins-ci.plugins git-client - 2.7.7 org.jenkins-ci.plugins junit - 1.28 org.jenkins-ci.plugins structs - 1.20 org.jenkins-ci.plugins scm-api - 2.6.3 org.jenkins-ci.plugins.workflow workflow-api - 2.37 org.jenkins-ci.plugins script-security - 1.66 org.jenkins-ci.plugins.workflow workflow-step-api - 2.22 org.jenkins-ci.plugins.workflow workflow-durable-task-step - 2.34 org.jenkins-ci.plugins.workflow workflow-cps-global-lib - 2.15 org.jenkins-ci.plugins.workflow workflow-support - 3.3 - - - - org.jboss.marshalling - jboss-marshalling - - - org.jboss.marshalling - jboss-marshalling-river - - - - - org.jboss.marshalling - jboss-marshalling - 1.4.12.jenkins-3 - - - org.jboss.marshalling - jboss-marshalling-river - 1.4.12.jenkins-3 org.jenkins-ci.plugins.workflow workflow-scm-step - 2.9 org.jenkins-ci.plugins.workflow workflow-job - 2.35 org.jenkins-ci.plugins credentials - 2.2.1 org.jenkins-ci.plugins plain-credentials - 1.5 true org.jenkins-ci.plugins ssh-credentials - 1.18 + true org.jenkins-ci symbol-annotation - 1.20 - org.jenkins-ci.plugins - matrix-project - 1.14 + org.jenkins-ci.plugins + matrix-project org.jenkins-ci.plugins display-url-api - 2.1.0 @@ -204,62 +173,49 @@ org.jboss.spec.javax.ws.rs jboss-jaxrs-api_2.0_spec - 1.0.0.Final org.jboss.resteasy resteasy-client - 3.0.16.Final com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider - ${jackson.version} - com.fasterxml.jackson.core - jackson-databind - 2.9.10.7 org.jenkins-ci.plugins jackson2-api - ${jackson.version} org.jenkins-ci.plugins apache-httpcomponents-client-4-api - 4.5.10-2.0 com.google.guava guava - 24.1.1 net.karneim pojobuilder - 3.4.0 provided + + junit + junit + test + io.jenkins configuration-as-code - ${configuration-as-code-plugin.version} test io.jenkins.configuration-as-code test-harness - ${configuration-as-code-plugin.version} - test - - - org.hamcrest - hamcrest-core - 2.2 test @@ -280,7 +236,6 @@ org.mock-server mockserver-netty - 3.10.2 test @@ -304,41 +259,90 @@ org.postgresql postgresql - 9.4.1208 test org.jenkins-ci.plugins matrix-auth - 2.3 test org.jenkins-ci.plugins.workflow workflow-cps - 2.74 test org.jenkins-ci.plugins.workflow workflow-basic-steps - 2.2 test - + + + + + io.jenkins.tools.bom + bom-2.222.x + 23 + import + pom + + + + + org.postgresql + postgresql + 9.4.1208 + test + + + org.mock-server + mockserver-netty + 3.10.2 + test + + + + org.jboss.spec.javax.ws.rs + jboss-jaxrs-api_2.0_spec + 1.0.0.Final + + + org.jboss.resteasy + resteasy-client + 3.0.16.Final + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + ${jackson.version} + + + + + com.google.guava + guava + 18.0 + + + net.karneim + pojobuilder + 3.4.0 + org.apache.commons commons-lang3 - 3.9 + 3.11 + + integration-test diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java index 8004314e5..c7f3c1803 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java @@ -70,7 +70,7 @@ public String getUserUsername() { return userUsername; } - public void setUserUserName(String userUsername) { + public void setUserUsername(String userUsername) { this.userUsername = userUsername; } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher.java b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher.java index 2168fc9a4..751ac8e54 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher.java @@ -12,6 +12,7 @@ import hudson.tasks.BuildStepMonitor; import hudson.tasks.Publisher; import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; @@ -24,8 +25,20 @@ public class GitLabAcceptMergeRequestPublisher extends MergeRequestNotifier { private static final Logger LOGGER = Logger.getLogger(GitLabAcceptMergeRequestPublisher.class.getName()); + private boolean deleteSourceBranch = false; + @DataBoundConstructor - public GitLabAcceptMergeRequestPublisher() { } + public GitLabAcceptMergeRequestPublisher() { + } + + @DataBoundSetter + public void setDeleteSourceBranch(boolean deleteSourceBranch) { + this.deleteSourceBranch = deleteSourceBranch; + } + + public boolean isDeleteSourceBranch() { return deleteSourceBranch; } + + public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; @@ -49,7 +62,7 @@ public String getDisplayName() { protected void perform(Run build, TaskListener listener, GitLabClient client, MergeRequest mergeRequest) { try { if (build.getResult() == Result.SUCCESS) { - client.acceptMergeRequest(mergeRequest, "Merge Request accepted by jenkins build success", false); + client.acceptMergeRequest(mergeRequest, "Merge Request accepted by jenkins build success", this.deleteSourceBranch); } } catch (WebApplicationException | ProcessingException e) { listener.getLogger().printf("Failed to accept merge request for project '%s': %s%n", mergeRequest.getProjectId(), e.getMessage()); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabMessagePublisher.java b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabMessagePublisher.java index fd8a5cfe4..12d30794b 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabMessagePublisher.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabMessagePublisher.java @@ -40,6 +40,15 @@ public class GitLabMessagePublisher extends MergeRequestNotifier { /** * @deprecated use {@link #GitLabMessagePublisher()} with setters to configure an instance of this class. + * @param onlyForFailure Option to only post message on failure + * @param replaceSuccessNote Option to replace message on success + * @param replaceFailureNote Option to replace message on failure + * @param replaceAbortNote Option to replace message on abort + * @param replaceUnstableNote Option to replace message on unstable + * @param successNoteText Text of message for build success + * @param failureNoteText Text of message for build failure + * @param abortNoteText Text of message for build abort + * @param unstableNoteText Text of message for unstable build */ @Deprecated public GitLabMessagePublisher(boolean onlyForFailure, boolean replaceSuccessNote, boolean replaceFailureNote, boolean replaceAbortNote, boolean replaceUnstableNote, diff --git a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImpl.java b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImpl.java index cd2951522..19ce4fcc6 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImpl.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImpl.java @@ -241,6 +241,10 @@ private boolean isLastCommitNotYetBuild(Job project, MergeRequestHook hook return true; } + if (!StringUtils.equals(getTargetMergeRequestStateFromBuild(mergeBuild), objectAttributes.getState().name())) { + return true; + } + if (StringUtils.equals(getTargetBranchFromBuild(mergeBuild), objectAttributes.getTargetBranch())) { LOGGER.log(Level.INFO, "Last commit in Merge Request has already been built in build #" + mergeBuild.getNumber()); return false; @@ -254,6 +258,11 @@ private String getTargetBranchFromBuild(Run mergeBuild) { return cause == null ? null : cause.getData().getTargetBranch(); } + private String getTargetMergeRequestStateFromBuild(Run mergeBuild) { + GitLabWebHookCause cause = mergeBuild.getCause(GitLabWebHookCause.class); + return cause == null ? null : cause.getData().getMergeRequestState(); + } + private boolean isAllowedByConfig(MergeRequestObjectAttributes objectAttributes) { return triggerConfig.apply(objectAttributes); } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java index fd648b56b..4a91bb599 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java @@ -11,8 +11,10 @@ import hudson.util.HttpResponses; import jenkins.model.Jenkins; import jenkins.plugins.git.GitSCMSource; +import jenkins.plugins.git.traits.IgnoreOnPushNotificationTrait; import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceOwner; +import jenkins.scm.api.trait.SCMTrait; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.URIish; @@ -102,7 +104,7 @@ public void run() { GitSCMSource gitSCMSource = (GitSCMSource) scmSource; try { if (new URIish(gitSCMSource.getRemote()).equals(new URIish(gitSCMSource.getRemote()))) { - if (!gitSCMSource.isIgnoreOnPushNotifications()) { + if (SCMTrait.find(gitSCMSource.getTraits(), IgnoreOnPushNotificationTrait.class) == null) { LOGGER.log(Level.FINE, "Notify scmSourceOwner {0} about changes for {1}", toArray(project.getName(), gitSCMSource.getRemote())); ((SCMSourceOwner) project).onSCMSourceUpdated(scmSource); diff --git a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly index 9b42361a8..7360ff473 100644 --- a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly +++ b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly @@ -1,8 +1,26 @@ + xmlns:f="/lib/form" + xmlns:d="jelly:define" + xmlns:local="local"> + + + + +
+ +
+
+ + + +
+
+
+
+
- + @@ -34,7 +52,7 @@ -
+
@@ -60,7 +78,7 @@ - + -
+
- + -
+
diff --git a/src/main/resources/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher/config.jelly b/src/main/resources/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher/config.jelly new file mode 100644 index 000000000..0021cd2e4 --- /dev/null +++ b/src/main/resources/com/dabsquared/gitlabjenkins/publisher/GitLabAcceptMergeRequestPublisher/config.jelly @@ -0,0 +1,9 @@ + + + + + + + + diff --git a/src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java b/src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java index 72b54ba88..1a9516e77 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java @@ -333,6 +333,7 @@ private AbstractBuild mockBuild(String gitLabConnection, Result result, String.. List buildDatas = new ArrayList<>(); BuildData buildData = mock(BuildData.class); Revision revision = mock(Revision.class); + when(revision.getSha1()).thenReturn(ObjectId.fromString(SHA1)); when(revision.getSha1String()).thenReturn(SHA1); when(buildData.getLastBuiltRevision()).thenReturn(revision); when(buildData.getRemoteUrls()).thenReturn(new HashSet<>(Arrays.asList(remoteUrls))); diff --git a/src/test/java/com/dabsquared/gitlabjenkins/publisher/TestUtility.java b/src/test/java/com/dabsquared/gitlabjenkins/publisher/TestUtility.java index 1064f3e07..d9974a708 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/publisher/TestUtility.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/publisher/TestUtility.java @@ -65,14 +65,14 @@ static void setupGitLabConnections(JenkinsRule jenkins, MockServerRule mockServe } - static void verifyMatrixAggregatable(Class publisherClass, BuildListener listener) throws InterruptedException, IOException { + static void verifyMatrixAggregatable(Class publisherClass, BuildListener listener) throws InterruptedException, IOException { AbstractBuild build = mock(AbstractBuild.class); AbstractProject project = mock(MatrixConfiguration.class); Notifier publisher = mock(publisherClass); MatrixBuild parentBuild = mock(MatrixBuild.class); when(build.getParent()).thenReturn(project); - when(((MatrixAggregatable) publisher).createAggregator(any(MatrixBuild.class), any(Launcher.class), any(BuildListener.class))).thenCallRealMethod(); + when(((MatrixAggregatable) publisher).createAggregator(any(MatrixBuild.class), any(), any(BuildListener.class))).thenCallRealMethod(); when(publisher.perform(any(AbstractBuild.class), any(Launcher.class), any(BuildListener.class))).thenReturn(true); MatrixAggregator aggregator = ((MatrixAggregatable) publisher).createAggregator(parentBuild, null, listener); 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 fb188741e..138eade8d 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 @@ -17,6 +17,7 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.After; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; @@ -56,7 +57,7 @@ public class MergeRequestHookTriggerHandlerImplTest { static { // Every negative (or failing positive) test adds 10 seconds to run time. The default 180 seconds might not // suffice - System.setProperty("jenkins.test.timeout", "300"); + System.setProperty("jenkins.test.timeout", "450"); jenkins = new JenkinsRule(); } @@ -331,6 +332,64 @@ public void mergeRequest_build_only_when_approved_and_not_when_merge() throws IO mergeRequest_build_only_when_approved(Action.merge); } + @Test + public void mergeRequest_build_only_when_state_modified()throws IOException, InterruptedException, GitAPIException, ExecutionException { + MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = withConfig() + .setTriggerOnAcceptedMergeRequest(true) + .setTriggerOnClosedMergeRequest(true) + .setTriggerOpenMergeRequest(TriggerOpenMergeRequest.never) + .build(); + Git.init().setDirectory(tmp.getRoot()).call(); + tmp.newFile("test"); + Git git = Git.open(tmp.getRoot()); + git.add().addFilepattern("test"); + RevCommit commit = git.commit().setMessage("test").call(); + ObjectId head = git.getRepository().resolve(Constants.HEAD); + String repositoryUrl = tmp.getRoot().toURI().toString(); + + final OneShotEvent buildTriggered = new OneShotEvent(); + FreeStyleProject project = jenkins.createFreeStyleProject(); + project.setScm(new GitSCM(repositoryUrl)); + project.getBuildersList().add(new TestBuilder() { + @Override + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { + buildTriggered.signal(); + return true; + } + }); + project.setQuietPeriod(0); + MergeRequestObjectAttributesBuilder objectAttributes = defaultMergeRequestObjectAttributes().withAction(Action.update); + 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") + .build() + ) + .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), + newMergeRequestLabelFilter(null)); + + buildTriggered.block(10000); + assertThat(buildTriggered.isSignaled(), is(true)); + MergeRequestObjectAttributesBuilder objectAttributes2 = defaultMergeRequestObjectAttributes().withState(State.merged).withAction(Action.merge); + mergeRequestHookTriggerHandler.handle(project, mergeRequestHook() + .withObjectAttributes(objectAttributes2 + .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") + .build() + ) + .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), + newMergeRequestLabelFilter(null)); + + buildTriggered.block(10000); + assertThat(buildTriggered.isSignaled(), is(true)); + } + private void do_not_build_for_state_when_nothing_enabled(State state) throws IOException, InterruptedException, GitAPIException, ExecutionException { MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = withConfig() .setTriggerOnMergeRequest(false) @@ -450,4 +509,16 @@ private MergeRequestObjectAttributesBuilder defaultMergeRequestObjectAttributes( .build()); } + @After + /* Add sleep(5000) on after to avoid following error on Windows test + Unable to delete 'C:\Jenkins\workspace\Plugins_gitlab-plugin_PR-1121\target\tmp\j h4861043637706712359'. Tried 3 times (of a maximum of 3) waiting 0.1 sec between attempts. + */ + public void after() + { + try { + Thread.sleep(5000); + } catch (InterruptedException ignored) { + + } + } } diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.java index 925da23e6..d7f7ab3ef 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.java @@ -14,10 +14,7 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.revwalk.RevCommit; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import org.junit.*; import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestBuilder; @@ -132,9 +129,17 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen project.setQuietPeriod(0); pipelineHookTriggerHandler.handle(project, pipelineHook, false, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), - newMergeRequestLabelFilter(null)); + newMergeRequestLabelFilter(null)); buildTriggered.block(10000); assertThat(buildTriggered.isSignaled(), is(true)); } + @After + public void after() { + try { + Thread.sleep(5000); + } catch (InterruptedException ignored) { + + } + } } diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerImplTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerImplTest.java index e5648b504..ca764320d 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerImplTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerImplTest.java @@ -13,10 +13,7 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.revwalk.RevCommit; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import org.junit.*; import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestBuilder; @@ -68,9 +65,9 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen project.setQuietPeriod(0); pushHookTriggerHandler.handle(project, pushHook() .withCommits(Arrays.asList(commit().withMessage("some message").build(), - commit().withMessage("[ci-skip]").build())) + commit().withMessage("[ci-skip]").build())) .build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), - newMergeRequestLabelFilter(null)); + newMergeRequestLabelFilter(null)); buildTriggered.block(10000); assertThat(buildTriggered.isSignaled(), is(false)); @@ -103,20 +100,20 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen .withUserName("test") .withObjectKind("tag_push") .withRepository(repository() - .withName("test") - .withHomepage("https://gitlab.org/test") - .withUrl("git@gitlab.org:test.git") - .withGitSshUrl("git@gitlab.org:test.git") - .withGitHttpUrl("https://gitlab.org/test.git") - .build()) + .withName("test") + .withHomepage("https://gitlab.org/test") + .withUrl("git@gitlab.org:test.git") + .withGitSshUrl("git@gitlab.org:test.git") + .withGitHttpUrl("https://gitlab.org/test.git") + .build()) .withProject(project() - .withNamespace("test-namespace") - .withWebUrl("https://gitlab.org/test") - .build()) + .withNamespace("test-namespace") + .withWebUrl("https://gitlab.org/test") + .build()) .withAfter(commit.name()) .withRef("refs/heads/" + git.nameRev().add(head).call().get(head)) .build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), - newMergeRequestLabelFilter(null)); + newMergeRequestLabelFilter(null)); buildTriggered.block(10000); assertThat(buildTriggered.isSignaled(), is(true)); @@ -154,25 +151,35 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen .withUserName("test") .withObjectKind("push") .withRepository(repository() - .withName("test") - .withHomepage("https://gitlab.org/test") - .withUrl("git@gitlab.org:test.git") - .withGitSshUrl("git@gitlab.org:test.git") - .withGitHttpUrl("https://gitlab.org/test.git") - .build()) + .withName("test") + .withHomepage("https://gitlab.org/test") + .withUrl("git@gitlab.org:test.git") + .withGitSshUrl("git@gitlab.org:test.git") + .withGitHttpUrl("https://gitlab.org/test.git") + .build()) .withProject(project() - .withNamespace("test-namespace") - .withWebUrl("https://gitlab.org/test") - .build()) + .withNamespace("test-namespace") + .withWebUrl("https://gitlab.org/test") + .build()) .withAfter(commit.name()) .withRef("refs/heads/" + git.nameRev().add(head).call().get(head)); pushHookTriggerHandler.handle(project, pushHookBuilder.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), - newMergeRequestLabelFilter(null)); + newMergeRequestLabelFilter(null)); pushHookTriggerHandler.handle(project, pushHookBuilder - .but().withRef("refs/heads/" + git.nameRev().add(head).call().get(head) + "-2").build(), true, - newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), newMergeRequestLabelFilter(null)); + .but().withRef("refs/heads/" + git.nameRev().add(head).call().get(head) + "-2").build(), true, + newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), newMergeRequestLabelFilter(null)); buildTriggered.block(10000); assertThat(buildTriggered.isSignaled(), is(true)); assertThat(buildCount.intValue(), is(2)); } + + @After + public void after() { + try { + Thread.sleep(5000); + } catch (InterruptedException ignored) { + + } + } + } diff --git a/src/test/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdaterTest.java b/src/test/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdaterTest.java index 9416158db..9753d7c3f 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdaterTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdaterTest.java @@ -1,10 +1,9 @@ package com.dabsquared.gitlabjenkins.util; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData; +import static org.mockito.Mockito.*; import java.io.PrintStream; import java.util.ArrayList; @@ -13,6 +12,7 @@ import com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig; import com.dabsquared.gitlabjenkins.workflow.GitLabBranchBuild; import hudson.Functions; +import hudson.Util; import org.eclipse.jgit.lib.ObjectId; import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; import org.junit.Before; @@ -46,7 +46,7 @@ * @author Daumantas Stulgis */ @RunWith(PowerMockRunner.class) -@PrepareForTest({GitLabConnectionProperty.class, Jenkins.class}) +@PrepareForTest({GitLabConnectionProperty.class, Jenkins.class, DisplayURLProvider.class}) public class CommitStatusUpdaterTest { private static final int PROJECT_ID = 1; @@ -99,6 +99,11 @@ public void setUp() throws Exception { } else { when(taskListener.getLogger()).thenReturn(new PrintStream("/dev/null")); } + PowerMockito.mockStatic(DisplayURLProvider.class); + DisplayURLProvider urlProvider = mock(DisplayURLProvider.class); + when(DisplayURLProvider.get()).thenReturn(urlProvider); + String url = JENKINS_URL+ Util.encode(build.getUrl()); + when(urlProvider.getRunURL(any())).thenReturn(url); causeData = causeData() diff --git a/src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildActionTest.java b/src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildActionTest.java index 204e6a1f2..816fa0291 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildActionTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildActionTest.java @@ -4,7 +4,11 @@ import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook; import com.dabsquared.gitlabjenkins.trigger.TriggerOpenMergeRequest; import hudson.model.FreeStyleProject; +import jenkins.plugins.git.GitSCMSource; +import jenkins.plugins.git.traits.IgnoreOnPushNotificationTrait; +import jenkins.scm.api.SCMSourceOwner; import org.apache.commons.io.IOUtils; +import org.junit.Assert; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; @@ -16,9 +20,11 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; + import static org.hamcrest.CoreMatchers.containsString; import java.io.IOException; +import java.util.Collections; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -89,4 +95,24 @@ public void invalidToken() throws IOException { private String getJson(String name) throws IOException { return IOUtils.toString(getClass().getResourceAsStream(name)); } + + @Test + public void scmSourceOnUpdateExecuted() { + GitSCMSource source = new GitSCMSource("http://test"); + SCMSourceOwner item = mock(SCMSourceOwner.class); + when(item.getSCMSources()).thenReturn(Collections.singletonList(source)); + Assert.assertThrows(HttpResponses.HttpResponseException.class, () -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response)); + verify(item).onSCMSourceUpdated(isA(GitSCMSource.class)); + + } + + @Test + public void scmSourceOnUpdateNotExecuted() { + GitSCMSource source = new GitSCMSource("http://test"); + source.getTraits().add(new IgnoreOnPushNotificationTrait()); + SCMSourceOwner item = mock(SCMSourceOwner.class); + when(item.getSCMSources()).thenReturn(Collections.singletonList(source)); + Assert.assertThrows(HttpResponses.HttpResponseException.class, () -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response)); + verify(item, never()).onSCMSourceUpdated(isA(GitSCMSource.class)); + } }