diff --git a/core/src/main/java/hudson/cli/BuildCommand.java b/core/src/main/java/hudson/cli/BuildCommand.java index 61332903df..0ebde8fa5c 100644 --- a/core/src/main/java/hudson/cli/BuildCommand.java +++ b/core/src/main/java/hudson/cli/BuildCommand.java @@ -46,6 +46,8 @@ import hudson.scm.PollingResult.Change; import hudson.util.EditDistance; import hudson.util.StreamTaskListener; +import java.util.logging.Level; +import jenkins.scm.SCMPollingDecisionHandler; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.Option; @@ -148,6 +150,12 @@ public class BuildCommand extends CLICommand { SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job); if (item == null) throw new AbortException(job.getFullDisplayName()+" has no SCM trigger, but checkSCM was specified"); + // pre-emtively check for a polling veto + for (SCMPollingDecisionHandler handler : SCMPollingDecisionHandler.all()) { + if (!handler.shouldPoll(job)) { + return 0; + } + } if (!item.poll(new StreamTaskListener(stdout, getClientCharset())).hasChanges()) return 0; } diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java index 8ebf64026b..1ee9660433 100644 --- a/core/src/main/java/hudson/model/AbstractProject.java +++ b/core/src/main/java/hudson/model/AbstractProject.java @@ -114,6 +114,7 @@ import jenkins.model.lazy.LazyBuildMixIn; import jenkins.scm.DefaultSCMCheckoutStrategyImpl; import jenkins.scm.SCMCheckoutStrategy; import jenkins.scm.SCMCheckoutStrategyDescriptor; +import jenkins.scm.SCMPollingDecisionHandler; import jenkins.util.TimeDuration; import net.sf.json.JSONObject; import org.acegisecurity.Authentication; @@ -1322,6 +1323,12 @@ public abstract class AbstractProject
,R extends A
listener.getLogger().println(Messages.AbstractProject_Disabled());
return NO_CHANGES;
}
+ for (SCMPollingDecisionHandler handler : SCMPollingDecisionHandler.all()) {
+ if (!handler.shouldPoll(this)) {
+ listener.getLogger().println(Messages.AbstractProject_PollingVetoed(handler));
+ return NO_CHANGES;
+ }
+ }
R lb = getLastBuild();
if (lb==null) {
diff --git a/core/src/main/java/hudson/triggers/SCMTrigger.java b/core/src/main/java/hudson/triggers/SCMTrigger.java
index 5c17e49193..26f8fe4ddc 100644
--- a/core/src/main/java/hudson/triggers/SCMTrigger.java
+++ b/core/src/main/java/hudson/triggers/SCMTrigger.java
@@ -27,6 +27,8 @@ package hudson.triggers;
import antlr.ANTLRException;
import com.google.common.base.Preconditions;
import hudson.Extension;
+import hudson.ExtensionList;
+import hudson.ExtensionPoint;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.model.AbstractBuild;
@@ -37,6 +39,7 @@ import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Item;
import hudson.model.Run;
+import hudson.model.listeners.SCMPollListener;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.util.FlushProofOutputStream;
@@ -46,6 +49,7 @@ import hudson.util.NamingThreadFactory;
import hudson.util.SequentialExecutionQueue;
import hudson.util.StreamTaskListener;
import hudson.util.TimeUnit2;
+import jenkins.scm.SCMPollingDecisionHandler;
import org.apache.commons.io.FileUtils;
import org.apache.commons.jelly.XMLOutput;
import org.jenkinsci.Symbol;
@@ -549,6 +553,16 @@ public class SCMTrigger extends Trigger
+ * This handler is consulted every time someone tries to run a polling of an {@link Item}.
+ * If any of the registered handlers returns false, the {@link Item} will not be polled.
+ *
+ * @since TODO
+ */
+public abstract class SCMPollingDecisionHandler implements ExtensionPoint {
+ /**
+ * Returns whether the new item should be polled.
+ *
+ * @param item The item.
+ */
+ public abstract boolean shouldPoll(Item item);
+
+ /**
+ * All registered {@link SCMPollingDecisionHandler}s
+ */
+ public static ExtensionList
* The implementation is responsible for ensuring mutual exclusion between polling and builds
* if necessary.
+ *
+ * The implementation is responsible for checking the {@link SCMPollingDecisionHandler} before proceeding
+ * with the actual polling.
*/
@Nonnull PollingResult poll(@Nonnull TaskListener listener);
@@ -116,6 +120,12 @@ public interface SCMTriggerItem {
return delegate.asProject().scheduleBuild2(quietPeriod, null, actions);
}
@Override public PollingResult poll(TaskListener listener) {
+ for (SCMPollingDecisionHandler handler : SCMPollingDecisionHandler.all()) {
+ if (!handler.shouldPoll(asItem())) {
+ listener.getLogger().println(Messages.SCMTriggerItem_PollingVetoed(handler));
+ return PollingResult.NO_CHANGES;
+ }
+ }
return delegate.poll(listener);
}
@Override public SCMTrigger getSCMTrigger() {
diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties
index bdda893e28..961c1e61ae 100644
--- a/core/src/main/resources/hudson/model/Messages.properties
+++ b/core/src/main/resources/hudson/model/Messages.properties
@@ -46,6 +46,7 @@ AbstractProject.NoWorkspace=No workspace is available, so can\u2019t check for u
AbstractProject.WorkspaceTitle=Workspace of {0}
AbstractProject.WorkspaceTitleOnComputer=Workspace of {0} on {1}
AbstractProject.PollingABorted=SCM polling aborted
+AbstractProject.PollingVetoed=SCM polling vetoed by {0}
AbstractProject.ScmAborted=SCM check out aborted
AbstractProject.WorkspaceOffline=Workspace is offline.
AbstractProject.BuildPermission.Description=\
diff --git a/core/src/main/resources/jenkins/triggers/Messages.properties b/core/src/main/resources/jenkins/triggers/Messages.properties
index 9ba056bf6f..9f71ea4895 100644
--- a/core/src/main/resources/jenkins/triggers/Messages.properties
+++ b/core/src/main/resources/jenkins/triggers/Messages.properties
@@ -22,3 +22,4 @@
ReverseBuildTrigger.build_after_other_projects_are_built=Build after other projects are built
ReverseBuildTrigger.running_as_cannot_even_see_for_trigger_f=Running as {0} cannot even see {1} for trigger from {2}
+SCMTriggerItem.PollingVetoed=SCM polling vetoed by {0}
diff --git a/test/src/test/java/hudson/triggers/SCMTriggerTest.java b/test/src/test/java/hudson/triggers/SCMTriggerTest.java
index 7df7eeda50..79f8502e63 100644
--- a/test/src/test/java/hudson/triggers/SCMTriggerTest.java
+++ b/test/src/test/java/hudson/triggers/SCMTriggerTest.java
@@ -27,8 +27,10 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import hudson.ExtensionList;
import hudson.FilePath;
import hudson.Launcher;
+import hudson.model.Item;
import hudson.util.OneShotEvent;
import hudson.util.StreamTaskListener;
import hudson.model.AbstractBuild;
@@ -42,6 +44,9 @@ import hudson.model.Cause.UserCause;
import hudson.scm.NullSCM;
import hudson.triggers.SCMTrigger.SCMTriggerCause;
import hudson.triggers.SCMTrigger.BuildAction;
+import java.util.HashSet;
+import java.util.Set;
+import jenkins.scm.SCMPollingDecisionHandler;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
@@ -52,6 +57,7 @@ import java.io.File;
import java.io.IOException;
import java.util.concurrent.Future;
import java.util.List;
+import org.jvnet.hudson.test.TestExtension;
/**
* @author Alan Harder
@@ -81,6 +87,29 @@ public class SCMTriggerTest {
build.get(); // let mock build finish
}
+ /**
+ * Make sure that SCMTrigger doesn't trigger another build when a build has just started,
+ * but not yet completed its SCM update.
+ */
+ @Test
+ @Issue("JENKINS-36123")
+ public void pollingExcludedByExtensionPoint() throws Exception {
+ FreeStyleProject p = j.createFreeStyleProject();
+
+ PollDecisionHandlerImpl handler =
+ ExtensionList.lookup(SCMPollingDecisionHandler.class).get(PollDecisionHandlerImpl.class);
+ handler.blacklist.add(p);
+
+ // used to coordinate polling and check out
+ final OneShotEvent checkoutStarted = new OneShotEvent();
+
+ p.setScm(new TestSCM(checkoutStarted));
+
+ assertFalse("SCM-poll with blacklist should report no changes", p.pollSCMChanges(StreamTaskListener.fromStdout()));
+ handler.blacklist.remove(p);
+ assertTrue("SCM-poll with blacklist removed should report changes", p.pollSCMChanges(StreamTaskListener.fromStdout()));
+ }
+
private static class TestSCM extends NullSCM {
private volatile int myRev = 1;
private final OneShotEvent checkoutStarted;
@@ -144,4 +173,15 @@ public class SCMTriggerTest {
assertFalse("There should only be one BuildAction.", ba.size()!=1);
}
+
+ @TestExtension
+ public static class PollDecisionHandlerImpl extends SCMPollingDecisionHandler {
+
+ Set