|
| 1 | +package com.gitee.jenkins.trigger.handler.push; |
| 2 | + |
| 3 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.CommitBuilder.commit; |
| 4 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.ProjectBuilder.project; |
| 5 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.PushHookBuilder.pushHook; |
| 6 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.UserBuilder.user; |
| 7 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.RepositoryBuilder.repository; |
| 8 | +import static com.gitee.jenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig; |
| 9 | +import static com.gitee.jenkins.trigger.filter.BranchFilterFactory.newBranchFilter; |
| 10 | +import static com.gitee.jenkins.trigger.filter.PullRequestLabelFilterFactory.newPullRequestLabelFilter; |
| 11 | +import static org.hamcrest.CoreMatchers.is; |
| 12 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 14 | + |
| 15 | +import com.gitee.jenkins.gitee.hook.model.builder.generated.PushHookBuilder; |
| 16 | +import com.gitee.jenkins.trigger.filter.BranchFilterType; |
| 17 | +import com.gitee.jenkins.trigger.filter.PullRequestLabelFilterFactory; |
| 18 | + |
| 19 | +import hudson.Launcher; |
| 20 | +import hudson.model.AbstractBuild; |
| 21 | +import hudson.model.BuildListener; |
| 22 | +import hudson.model.FreeStyleBuild; |
| 23 | +import hudson.model.FreeStyleProject; |
| 24 | +import hudson.plugins.git.GitSCM; |
| 25 | +import hudson.util.OneShotEvent; |
| 26 | +import java.io.File; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.concurrent.atomic.AtomicInteger; |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | +import org.eclipse.jgit.api.Git; |
| 31 | +import org.eclipse.jgit.lib.Constants; |
| 32 | +import org.eclipse.jgit.lib.ObjectId; |
| 33 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.io.TempDir; |
| 38 | +import org.jvnet.hudson.test.JenkinsRule; |
| 39 | +import org.jvnet.hudson.test.TestBuilder; |
| 40 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Robin Müller |
| 44 | + */ |
| 45 | +@WithJenkins |
| 46 | +class PushHookTriggerHandlerImplTest { |
| 47 | + |
| 48 | + private static JenkinsRule jenkins; |
| 49 | + |
| 50 | + @TempDir |
| 51 | + private File tmp; |
| 52 | + |
| 53 | + private PushHookTriggerHandler pushHookTriggerHandler; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + static void setUp(JenkinsRule rule) { |
| 57 | + jenkins = rule; |
| 58 | + } |
| 59 | + |
| 60 | + @BeforeEach |
| 61 | + void setUp() { |
| 62 | + pushHookTriggerHandler = new PushHookTriggerHandlerImpl(false, false); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void push_ciSkip() throws Exception { |
| 67 | + final OneShotEvent buildTriggered = new OneShotEvent(); |
| 68 | + FreeStyleProject project = jenkins.createFreeStyleProject(); |
| 69 | + final AtomicReference<FreeStyleBuild> buildHolder = new AtomicReference<>(); |
| 70 | + project.getBuildersList().add(new TestBuilder() { |
| 71 | + @Override |
| 72 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { |
| 73 | + buildHolder.set((FreeStyleBuild) build); |
| 74 | + buildTriggered.signal(); |
| 75 | + return true; |
| 76 | + } |
| 77 | + }); |
| 78 | + project.setQuietPeriod(0); |
| 79 | + pushHookTriggerHandler.handle( |
| 80 | + project, |
| 81 | + pushHook() |
| 82 | + .withCommits(Arrays.asList( |
| 83 | + commit().withMessage("some message").build(), |
| 84 | + commit().withMessage("[ci-skip]").build())) |
| 85 | + .build(), |
| 86 | + null, true, |
| 87 | + newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), |
| 88 | + PullRequestLabelFilterFactory.newPullRequestLabelFilter(null)); |
| 89 | + |
| 90 | + buildTriggered.block(10000); |
| 91 | + assertThat(buildTriggered.isSignaled(), is(false)); |
| 92 | + assertNull(buildHolder.get()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void push_build() throws Exception { |
| 97 | + Git.init().setDirectory(tmp).call(); |
| 98 | + File.createTempFile("test", null, tmp); |
| 99 | + Git git = Git.open(tmp); |
| 100 | + git.add().addFilepattern("test"); |
| 101 | + RevCommit commit = git.commit().setSign(false).setMessage("test").call(); |
| 102 | + ObjectId head = git.getRepository().resolve(Constants.HEAD); |
| 103 | + String repositoryUrl = tmp.toURI().toString(); |
| 104 | + |
| 105 | + final OneShotEvent buildTriggered = new OneShotEvent(); |
| 106 | + FreeStyleProject project = jenkins.createFreeStyleProject(); |
| 107 | + project.setScm(new GitSCM(repositoryUrl)); |
| 108 | + final AtomicReference<FreeStyleBuild> buildHolder = new AtomicReference<>(); |
| 109 | + project.getBuildersList().add(new TestBuilder() { |
| 110 | + @Override |
| 111 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { |
| 112 | + buildHolder.set((FreeStyleBuild) build); |
| 113 | + buildTriggered.signal(); |
| 114 | + return true; |
| 115 | + } |
| 116 | + }); |
| 117 | + project.setQuietPeriod(0); |
| 118 | + pushHookTriggerHandler.handle( |
| 119 | + project, |
| 120 | + pushHook() |
| 121 | + .withBefore("0000000000000000000000000000000000000000") |
| 122 | + .withUserName("test") |
| 123 | + .withObjectKind("tag_push") |
| 124 | + .withRepository(repository() |
| 125 | + .withName("test") |
| 126 | + .withHomepage("https://gitee.org/test") |
| 127 | + . withUrl( "[email protected]:test.git") |
| 128 | + . withGitSshUrl( "[email protected]:test.git") |
| 129 | + .withGitHttpUrl("https://gitee.org/test.git") |
| 130 | + .build()) |
| 131 | + .withProject(project() |
| 132 | + .withName("test-name") |
| 133 | + .withId(1) |
| 134 | + .withUrl("https://gitee.org/test") |
| 135 | + .withSshUrl("https://gitee.org/test") |
| 136 | + .withNamespace("test-namespace") |
| 137 | + .withWebUrl("https://gitee.org/test") |
| 138 | + .withGitHttpUrl("https://gitee.org/test") |
| 139 | + .build()) |
| 140 | + .withAfter(commit.name()) |
| 141 | + .withSender(user() |
| 142 | + .withId(1) |
| 143 | + .withName("test-user") |
| 144 | + .withUsername("test-user") |
| 145 | + |
| 146 | + .build()) |
| 147 | + .withRef("refs/heads/" + git.nameRev().add(head).call().get(head)) |
| 148 | + .build(), |
| 149 | + null, true, |
| 150 | + newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), |
| 151 | + PullRequestLabelFilterFactory.newPullRequestLabelFilter(null)); |
| 152 | + |
| 153 | + buildTriggered.block(10000); |
| 154 | + assertThat(buildTriggered.isSignaled(), is(true)); |
| 155 | + jenkins.assertBuildStatusSuccess(jenkins.waitForCompletion(buildHolder.get())); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void push_build2DifferentBranchesButSameCommit() throws Exception { |
| 160 | + Git.init().setDirectory(tmp).call(); |
| 161 | + File.createTempFile("test", null, tmp); |
| 162 | + Git git = Git.open(tmp); |
| 163 | + git.add().addFilepattern("test"); |
| 164 | + RevCommit commit = git.commit().setSign(false).setMessage("test").call(); |
| 165 | + ObjectId head = git.getRepository().resolve(Constants.HEAD); |
| 166 | + String repositoryUrl = tmp.toURI().toString(); |
| 167 | + |
| 168 | + final AtomicInteger buildCount = new AtomicInteger(0); |
| 169 | + |
| 170 | + final OneShotEvent buildTriggered = new OneShotEvent(); |
| 171 | + FreeStyleProject project = jenkins.createFreeStyleProject(); |
| 172 | + project.setConcurrentBuild(false); |
| 173 | + project.setScm(new GitSCM(repositoryUrl)); |
| 174 | + final AtomicReference<FreeStyleBuild> buildHolder = new AtomicReference<>(); |
| 175 | + project.getBuildersList().add(new TestBuilder() { |
| 176 | + @Override |
| 177 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { |
| 178 | + buildHolder.set((FreeStyleBuild) build); |
| 179 | + int count = buildCount.incrementAndGet(); |
| 180 | + if (count == 2) { |
| 181 | + buildTriggered.signal(); |
| 182 | + } |
| 183 | + return true; |
| 184 | + } |
| 185 | + }); |
| 186 | + project.setQuietPeriod(0); |
| 187 | + PushHookBuilder pushHookBuilder = pushHook() |
| 188 | + .withBefore("0000000000000000000000000000000000000000") |
| 189 | + .withUserName("test") |
| 190 | + .withObjectKind("push") |
| 191 | + .withRepository(repository() |
| 192 | + .withName("test") |
| 193 | + .withHomepage("https://gitee.org/test") |
| 194 | + . withUrl( "[email protected]:test.git") |
| 195 | + . withGitSshUrl( "[email protected]:test.git") |
| 196 | + .withGitHttpUrl("https://gitee.org/test.git") |
| 197 | + .build()) |
| 198 | + .withProject(project() |
| 199 | + .withName("test-name") |
| 200 | + .withUrl("https://gitee.org/test") |
| 201 | + .withId(1) |
| 202 | + .withSshUrl("https://gitee.org/test") |
| 203 | + .withNamespace("test-namespace") |
| 204 | + .withWebUrl("https://gitee.org/test") |
| 205 | + .withGitHttpUrl("https://gitee.org/test") |
| 206 | + .build()) |
| 207 | + .withSender(user() |
| 208 | + .withId(1) |
| 209 | + .withName("test-user") |
| 210 | + .withUsername("test-user") |
| 211 | + |
| 212 | + .build()) |
| 213 | + .withAfter(commit.name()) |
| 214 | + .withRef("refs/heads/" + git.nameRev().add(head).call().get(head)); |
| 215 | + pushHookTriggerHandler.handle( |
| 216 | + project, |
| 217 | + pushHookBuilder.build(), |
| 218 | + null, true, |
| 219 | + newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), |
| 220 | + PullRequestLabelFilterFactory.newPullRequestLabelFilter(null)); |
| 221 | + pushHookTriggerHandler.handle( |
| 222 | + project, |
| 223 | + pushHookBuilder |
| 224 | + .but() |
| 225 | + .withRef("refs/heads/" + git.nameRev().add(head).call().get(head) + "-2") |
| 226 | + .build(), |
| 227 | + null, true, |
| 228 | + newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), |
| 229 | + PullRequestLabelFilterFactory.newPullRequestLabelFilter(null)); |
| 230 | + buildTriggered.block(10000); |
| 231 | + assertThat(buildTriggered.isSignaled(), is(true)); |
| 232 | + assertThat(buildCount.intValue(), is(2)); |
| 233 | + jenkins.assertBuildStatusSuccess(jenkins.waitForCompletion(buildHolder.get())); |
| 234 | + } |
| 235 | +} |
0 commit comments