Skip to content

Commit

Permalink
Merge pull request #741 from mayt/fix-ghprbcancelbuildsorupdate
Browse files Browse the repository at this point in the history
GhprbCancelBuildsOnUpdate loops through queue items
  • Loading branch information
bjoernhaeuser authored Jul 2, 2019
2 parents 0832b94 + d63d51b commit f4eb4bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jenkinsci.plugins.ghprb.extensions.GhprbProjectExtension;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -40,7 +41,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,30 +54,30 @@ private void cancelCurrentBuilds(Job<?, ?> project,
);

Queue queue = Jenkins.getInstance().getQueue();
Queue.Item queueItem = project.getQueueItem();
while (queueItem != null) {
GhprbCause qcause = null;

for (Cause cause : queueItem.getCauses()) {
if (cause instanceof GhprbCause) {
qcause = (GhprbCause) cause;
Queue.Item item = project.getQueueItem();
if (item != null) {
List<Queue.Item> queueItems = queue.getItems(item.task);
for (Queue.Item queueItem : queueItems) {
GhprbCause qcause = null;

for (Cause cause : queueItem.getCauses()) {
if (cause instanceof GhprbCause) {
qcause = (GhprbCause) cause;
}
}
}

if (qcause != null && qcause.getPullID() == prId) {
try {
LOGGER.log(
Level.FINER,
"Cancelling queued build of " + project.getName() + " for PR # "
+ qcause.getPullID() + ", checking for queued items to cancel."
);
queue.cancel(queueItem);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Unable to cancel queued build", e);
if (qcause != null && qcause.getPullID() == prId) {
try {
LOGGER.log(
Level.FINER,
"Cancelling queued build of " + project.getName() + " for PR # "
+ qcause.getPullID() + ", checking for queued items to cancel."
);
queue.cancel(queueItem);
} catch (Exception e) {
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 f4eb4bc

Please sign in to comment.