From a0edc1140a17b3d79ed7e0ed5a4875827d46a45b Mon Sep 17 00:00:00 2001 From: Maytee Chinavanichkit Date: Wed, 10 Apr 2019 17:34:39 +0900 Subject: [PATCH] GhprbCancelBuildsOnUpdate loops through queue items --- .../build/GhprbCancelBuildsOnUpdate.java | 8 ++-- .../build/GhprbCancelBuildsOnUpdateTest.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdateTest.java diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java index db1e659ad..99938cfe1 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java @@ -40,7 +40,7 @@ public Boolean getOverrideGlobal() { return overrideGlobal == null ? Boolean.valueOf(false) : overrideGlobal; } - private void cancelCurrentBuilds(Job project, + protected void cancelCurrentBuilds(Job project, Integer prId) { if (getOverrideGlobal()) { return; @@ -53,8 +53,8 @@ private void cancelCurrentBuilds(Job project, ); Queue queue = Jenkins.getInstance().getQueue(); - Queue.Item queueItem = project.getQueueItem(); - while (queueItem != null) { + Queue.Item[] queueItems = queue.getItems(); + for (Queue.Item queueItem : queueItems) { GhprbCause qcause = null; for (Cause cause : queueItem.getCauses()) { @@ -75,8 +75,6 @@ private void cancelCurrentBuilds(Job project, LOGGER.log(Level.SEVERE, "Unable to cancel queued build", e); } } - - queueItem = project.getQueueItem(); } LOGGER.log(Level.FINER, "New build scheduled for " + project.getName() + " on PR # " + prId); diff --git a/src/test/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdateTest.java b/src/test/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdateTest.java new file mode 100644 index 000000000..90e94b9d1 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdateTest.java @@ -0,0 +1,45 @@ +package org.jenkinsci.plugins.ghprb.extensions.build; + +import hudson.model.FreeStyleProject; +import org.jenkinsci.plugins.ghprb.GhprbITBaseTestCase; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.jvnet.hudson.test.JenkinsRule; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.BDDMockito.given; + +@RunWith(MockitoJUnitRunner.class) +public class GhprbCancelBuildsOnUpdateTest extends GhprbITBaseTestCase { + + @Rule + public JenkinsRule jenkinsRule = new JenkinsRule(); + + private FreeStyleProject project; + + private GhprbCancelBuildsOnUpdate gcbou; + + @Before + public void setUp() throws Exception { + project = jenkinsRule.getInstance().createProject(FreeStyleProject.class, "FSPRJ"); + + Map config = new HashMap<>(1); + + super.beforeTest(config, null, project); + + given(ghprbPullRequest.getPullRequestAuthor()).willReturn(ghUser); + + gcbou = new GhprbCancelBuildsOnUpdate(false); + } + + @Test + public void testCancelCurrentBuilds() { + builds.build(ghprbPullRequest, ghUser, ""); + gcbou.cancelCurrentBuilds(project, 1); + } +}