|
| 1 | +package com.gitee.jenkins.trigger.handler; |
| 2 | + |
| 3 | +import static com.gitee.jenkins.gitee.hook.model.builder.generated.PullRequestObjectAttributesBuilder.pullRequestObjectAttributes; |
| 4 | +import static org.hamcrest.CoreMatchers.is; |
| 5 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | +import com.gitee.jenkins.trigger.GiteePushTrigger; |
| 7 | +import com.gitee.jenkins.cause.GiteeWebHookCause; |
| 8 | +import com.gitee.jenkins.connection.GiteeConnectionProperty; |
| 9 | +import com.gitee.jenkins.gitee.hook.model.Action; |
| 10 | +import com.gitee.jenkins.gitee.hook.model.ActionDesc; |
| 11 | +import com.gitee.jenkins.gitee.hook.model.PullRequestHook; |
| 12 | +import com.gitee.jenkins.gitee.hook.model.State; |
| 13 | +import com.gitee.jenkins.gitee.hook.model.builder.generated.BranchDataBuilder; |
| 14 | +import com.gitee.jenkins.gitee.hook.model.builder.generated.PullRequestHookBuilder; |
| 15 | +import com.gitee.jenkins.gitee.hook.model.builder.generated.ProjectBuilder; |
| 16 | +import com.gitee.jenkins.gitee.hook.model.builder.generated.UserBuilder; |
| 17 | +import com.gitee.jenkins.trigger.filter.BranchFilterType; |
| 18 | + |
| 19 | +import hudson.model.Cause; |
| 20 | +import hudson.model.FreeStyleProject; |
| 21 | +import hudson.model.Project; |
| 22 | +import hudson.model.Queue; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.jvnet.hudson.test.JenkinsRule; |
| 30 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 33 | + |
| 34 | +@WithJenkins |
| 35 | +@ExtendWith(MockitoExtension.class) |
| 36 | +class PendingBuildsHandlerTest { |
| 37 | + |
| 38 | + private static final String GITEE_BUILD_NAME = "Jenkins"; |
| 39 | + |
| 40 | + private static JenkinsRule jenkins; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private GiteeConnectionProperty giteeConnectionProperty; |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + static void setUp(JenkinsRule rule) { |
| 47 | + jenkins = rule; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @AfterEach |
| 52 | + void tearDown() { |
| 53 | + Queue queue = jenkins.getInstance().getQueue(); |
| 54 | + for (Queue.Item item : queue.getItems()) { |
| 55 | + queue.cancel(item); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void queuedMergeRequestBuildsCanBeCancelledOnMergeRequestUpdate() throws Exception { |
| 61 | + Project project = freestyleProject("project1"); |
| 62 | + |
| 63 | + GiteePushTrigger giteePushTrigger = giteePushTrigger(project); |
| 64 | + giteePushTrigger.setCancelPendingBuildsOnUpdate(true); |
| 65 | + |
| 66 | + assertThat(jenkins.getInstance().getQueue().getItems().length, is(0)); |
| 67 | + |
| 68 | + List<String> queuedCommits = List.of("commit3", "commit4", "commit5"); |
| 69 | + |
| 70 | + giteePushTrigger.onPost(pullRequestHook(1, "sourceBranch", "commit1")); // Will be cancelled |
| 71 | + giteePushTrigger.onPost(pullRequestHook(1, "sourceBranch", "commit2")); // Will be cancelled |
| 72 | + giteePushTrigger.onPost(pullRequestHook(1, "sourceBranch", "commit3")); |
| 73 | + giteePushTrigger.onPost(pullRequestHook(1, "anotherBranch", "commit4")); |
| 74 | + giteePushTrigger.onPost(pullRequestHook(2, "sourceBranch", "commit5")); |
| 75 | + |
| 76 | + |
| 77 | + for (Queue.Item i: jenkins.getInstance().getQueue().getItems()) { |
| 78 | + for (Cause c : i.getCauses()) { |
| 79 | + GiteeWebHookCause w = (GiteeWebHookCause) c; |
| 80 | + assertThat(queuedCommits.contains(w.getData().getLastCommit()), is(true)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + assertThat(jenkins.getInstance().getQueue().getItems().length, is(3)); |
| 85 | + } |
| 86 | + |
| 87 | + private GiteePushTrigger giteePushTrigger(Project project) throws Exception { |
| 88 | + GiteePushTrigger giteePushTrigger = giteePushTrigger(); |
| 89 | + project.addTrigger(giteePushTrigger); |
| 90 | + giteePushTrigger.start(project, true); |
| 91 | + return giteePushTrigger; |
| 92 | + } |
| 93 | + |
| 94 | + private GiteePushTrigger giteePushTrigger() { |
| 95 | + GiteePushTrigger giteePushTrigger = new GiteePushTrigger(); |
| 96 | + giteePushTrigger.setTriggerOnPush(true); |
| 97 | + giteePushTrigger.setTriggerOnAcceptedPullRequest(true); |
| 98 | + giteePushTrigger.setTriggerOnApprovedPullRequest(true); |
| 99 | + giteePushTrigger.setPendingBuildName(GITEE_BUILD_NAME); |
| 100 | + giteePushTrigger.setBranchFilterType(BranchFilterType.NameBasedFilter); |
| 101 | + giteePushTrigger.setBranchFilterName(""); |
| 102 | + return giteePushTrigger; |
| 103 | + } |
| 104 | + |
| 105 | + private PullRequestHook pullRequestHook(int projectId, String branch, String commitId) { |
| 106 | + return PullRequestHookBuilder.pullRequestHook() |
| 107 | + .withSender(UserBuilder.user() |
| 108 | + .withId(1) |
| 109 | + .withName("test-user") |
| 110 | + .withUsername("test") |
| 111 | + .build()) |
| 112 | + .withAction(Action.update) |
| 113 | + .withActionDesc(ActionDesc.target_branch_changed) |
| 114 | + .withState(State.updated) |
| 115 | + .withRepo(ProjectBuilder.project() |
| 116 | + .withId(1) |
| 117 | + .withName("test") |
| 118 | + .withNamespace("test-namespace") |
| 119 | + .withHomepage("https://gitee.com/test") |
| 120 | + . withUrl( "[email protected]:test.git") |
| 121 | + . withSshUrl( "[email protected]:test.git") |
| 122 | + .withGitHttpUrl("https://gitee.com/test.git") |
| 123 | + .withWebUrl("https://gitee.com/test.git") |
| 124 | + .build()) |
| 125 | + .withPullRequest(pullRequestObjectAttributes() |
| 126 | + .withMergeCommitSha(commitId) |
| 127 | + .withId(projectId) |
| 128 | + .withMergeReferenceName("testRefName") |
| 129 | + .withTitle("test") |
| 130 | + .withMergeable(true) |
| 131 | + .withBase(BranchDataBuilder.branchData() |
| 132 | + .withLabel(branch) |
| 133 | + .withRepo(ProjectBuilder.project() |
| 134 | + .withId(projectId) |
| 135 | + .withName("test") |
| 136 | + .withNamespace("test-namespace") |
| 137 | + .withHomepage("https://gitee.com/test") |
| 138 | + . withUrl( "[email protected]:test.git") |
| 139 | + . withSshUrl( "[email protected]:test.git") |
| 140 | + .withGitHttpUrl("https://gitee.com/test.git") |
| 141 | + .withWebUrl("https://gitee.com/test.git") |
| 142 | + .build()) |
| 143 | + .withRef("") |
| 144 | + .withUser(UserBuilder.user() |
| 145 | + .withId(1) |
| 146 | + .withName("test-user") |
| 147 | + .withUsername("test") |
| 148 | + .build()) |
| 149 | + .build()) |
| 150 | + .withHead(BranchDataBuilder.branchData() |
| 151 | + .withLabel(branch) |
| 152 | + .withRepo(ProjectBuilder.project() |
| 153 | + .withId(projectId) |
| 154 | + .withName("test") |
| 155 | + .withNamespace("test-namespace") |
| 156 | + .withHomepage("https://gitee.com/test") |
| 157 | + . withUrl( "[email protected]:test.git") |
| 158 | + . withSshUrl( "[email protected]:test.git") |
| 159 | + .withGitHttpUrl("https://gitee.com/test.git") |
| 160 | + .withWebUrl("https://gitee.com/test.git") |
| 161 | + .build()) |
| 162 | + .withRef(branch) |
| 163 | + .withUser(UserBuilder.user() |
| 164 | + .withId(1) |
| 165 | + .withName("test-user") |
| 166 | + .withUsername("test") |
| 167 | + .build()) |
| 168 | + .build()) |
| 169 | + .build()) |
| 170 | + .build(); |
| 171 | + } |
| 172 | + |
| 173 | + private Project freestyleProject(String name) |
| 174 | + throws Exception { |
| 175 | + FreeStyleProject project = jenkins.createFreeStyleProject(name); |
| 176 | + project.setQuietPeriod(5000); |
| 177 | + // project.getPublishersList().add(giteeCommitStatusPublisher); |
| 178 | + project.addProperty(giteeConnectionProperty); |
| 179 | + return project; |
| 180 | + } |
| 181 | +} |
| 182 | + |
0 commit comments