[FIXED JENKINS-36123] Add an extension point that allows plugins to veto polling

This commit is contained in:
Stephen Connolly committed 2016-06-21 23:02:37 +01:00
1 parent 1ea03a326f
commit 4ac67a4d80
8 files changed
+136 -1

No files matched your search

@@ -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;
}
@@ -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<P extends AbstractProject<P,R>,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) {
@@ -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<Item> {
if (job == null) {
return;
}
// we can pre-emtively check the SCMPollingDecisionHandler instances here
// note that job().poll(listener) should also check this
for (SCMPollingDecisionHandler handler : SCMPollingDecisionHandler.all()) {
if (!handler.shouldPoll(job)) {
LOGGER.log(Level.FINE, "Skipping polling for {0} due to veto from {1}",
new Object[]{job.getFullDisplayName(), handler}
);
return;
}
}
String threadName = Thread.currentThread().getName();
Thread.currentThread().setName("SCM polling for "+job);
@@ -616,7 +630,7 @@ public class SCMTrigger extends Trigger<Item> {
/**
* @deprecated
* Use {@link #SCMTrigger.SCMTriggerCause(String)}.
* Use {@link SCMTrigger.SCMTriggerCause#SCMTriggerCause(String)}.
*/
@Deprecated
public SCMTriggerCause() {
@@ -0,0 +1,54 @@
/*
* The MIT License
*
* Copyright (c) 2016, Stephen Connolly.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.scm;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Item;
/**
* Extension point for deciding if particular job should be polled or not.
*
* <p>
* 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<SCMPollingDecisionHandler> all() {
return ExtensionList.lookup(SCMPollingDecisionHandler.class);
}
}
@@ -40,6 +40,7 @@ import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.scm.SCMPollingDecisionHandler;
/**
* The item type accepted by {@link SCMTrigger}.
@@ -65,6 +66,9 @@ public interface SCMTriggerItem {
* <p>
* The implementation is responsible for ensuring mutual exclusion between polling and builds
* if necessary.
* <p>
* 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() {
@@ -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=\
@@ -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}
@@ -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<Item> blacklist = new HashSet<>();
@Override
public boolean shouldPoll(Item item) {
return !blacklist.contains(item);
}
}
}