Skip to content

Commit

Permalink
GhprbCancelBuildsOnUpdate loops through queue items
Browse files Browse the repository at this point in the history
  • Loading branch information
Maytee Chinavanichkit committed Apr 11, 2019
1 parent b972d3b commit a0edc11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
}

0 comments on commit a0edc11

Please sign in to comment.