From 15ba345bd8a16e3682ca5a4b95fe764d2c456d61 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 25 Oct 2012 06:24:55 +0900 Subject: [PATCH] inject delay as a parameter to make the parsing logic reusable --- .../java/hudson/model/AbstractProject.java | 15 ++++-- .../model/ParametersDefinitionProperty.java | 15 ++++-- .../main/java/jenkins/util/TimeDuration.java | 52 +++++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/jenkins/util/TimeDuration.java diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java index e7304e9373..6dde9b70d9 100644 --- a/core/src/main/java/hudson/model/AbstractProject.java +++ b/core/src/main/java/hudson/model/AbstractProject.java @@ -88,6 +88,7 @@ import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction; import jenkins.scm.DefaultSCMCheckoutStrategyImpl; import jenkins.scm.SCMCheckoutStrategy; import jenkins.scm.SCMCheckoutStrategyDescriptor; +import jenkins.util.TimeDuration; import net.sf.json.JSONObject; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -1710,20 +1711,21 @@ public abstract class AbstractProject

,R extends A /** * Schedules a new build command. */ - public void doBuild( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException { + public void doBuild( StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay ) throws IOException, ServletException { + if (delay==null) delay=new TimeDuration(getQuietPeriod()); BuildAuthorizationToken.checkPermission(this, authToken, req, rsp); // if a build is parameterized, let that take over ParametersDefinitionProperty pp = getProperty(ParametersDefinitionProperty.class); if (pp != null) { - pp._doBuild(req,rsp); + pp._doBuild(req,rsp,delay); return; } if (!isBuildable()) throw HttpResponses.error(SC_INTERNAL_SERVER_ERROR,new IOException(getFullName()+" is not buildable")); - Jenkins.getInstance().getQueue().schedule(this, getDelay(req), getBuildCause(req)); + Jenkins.getInstance().getQueue().schedule(this, (int)delay.getTime(), getBuildCause(req)); rsp.forwardToPreviousPage(req); } @@ -1744,6 +1746,9 @@ public abstract class AbstractProject

,R extends A /** * Computes the delay by taking the default value and the override in the request parameter into the account. + * + * @deprecated as of 1.488 + * Inject {@link TimeDuration}. */ public int getDelay(StaplerRequest req) throws ServletException { String delay = req.getParameter("delay"); @@ -1763,12 +1768,12 @@ public abstract class AbstractProject

,R extends A * Supports build trigger with parameters via an HTTP GET or POST. * Currently only String parameters are supported. */ - public void doBuildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + public void doBuildWithParameters(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException { BuildAuthorizationToken.checkPermission(this, authToken, req, rsp); ParametersDefinitionProperty pp = getProperty(ParametersDefinitionProperty.class); if (pp != null) { - pp.buildWithParameters(req,rsp); + pp.buildWithParameters(req,rsp,delay); } else { throw new IllegalStateException("This build is not parameterized!"); } diff --git a/core/src/main/java/hudson/model/ParametersDefinitionProperty.java b/core/src/main/java/hudson/model/ParametersDefinitionProperty.java index 0bc967dd1b..0d58213c00 100644 --- a/core/src/main/java/hudson/model/ParametersDefinitionProperty.java +++ b/core/src/main/java/hudson/model/ParametersDefinitionProperty.java @@ -35,9 +35,11 @@ import java.util.AbstractList; import javax.servlet.ServletException; import jenkins.model.Jenkins; +import jenkins.util.TimeDuration; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; import org.kohsuke.stapler.export.Exported; @@ -104,14 +106,16 @@ public class ParametersDefinitionProperty extends JobProperty - * This method is supposed to be invoked from {@link AbstractProject#doBuild(StaplerRequest, StaplerResponse)}. + * This method is supposed to be invoked from {@link AbstractProject#doBuild(StaplerRequest, StaplerResponse, TimeDuration)}. */ - public void _doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + public void _doBuild(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException { if(!req.getMethod().equals("POST")) { // show the parameter entry form. req.getView(this,"index.jelly").forward(req,rsp); return; } + if (delay==null) delay=new TimeDuration(owner.getQuietPeriod()); + List values = new ArrayList(); @@ -130,13 +134,13 @@ public class ParametersDefinitionProperty extends JobProperty values = new ArrayList(); for (ParameterDefinition d: parameterDefinitions) { ParameterValue value = d.createValue(req); @@ -144,9 +148,10 @@ public class ParametersDefinitionProperty extends JobProperty