提交 15ba345b 编写于 作者: K Kohsuke Kawaguchi

inject delay as a parameter to make the parsing logic reusable

上级 9429452b
......@@ -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<P extends AbstractProject<P,R>,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<P extends AbstractProject<P,R>,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<P extends AbstractProject<P,R>,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!");
}
......
......@@ -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<AbstractProject<?,
* Interprets the form submission and schedules a build for a parameterized job.
*
* <p>
* 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<ParameterValue> values = new ArrayList<ParameterValue>();
......@@ -130,13 +134,13 @@ public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?,
}
Jenkins.getInstance().getQueue().schedule(
owner, owner.getDelay(req), new ParametersAction(values), new CauseAction(new Cause.UserIdCause()));
owner, delay.getTime(), new ParametersAction(values), new CauseAction(new Cause.UserIdCause()));
// send the user back to the job top page.
rsp.sendRedirect(".");
}
public void buildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
public void buildWithParameters(StaplerRequest req, StaplerResponse rsp, TimeDuration delay) throws IOException, ServletException {
List<ParameterValue> values = new ArrayList<ParameterValue>();
for (ParameterDefinition d: parameterDefinitions) {
ParameterValue value = d.createValue(req);
......@@ -144,9 +148,10 @@ public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?,
values.add(value);
}
}
if (delay==null) delay=new TimeDuration(owner.getQuietPeriod());
Jenkins.getInstance().getQueue().schedule(
owner, owner.getDelay(req), new ParametersAction(values), owner.getBuildCause(req));
owner, delay.getTime(), new ParametersAction(values), owner.getBuildCause(req));
if (requestWantsJson(req)) {
rsp.setContentType("application/json");
......
package jenkins.util;
import org.apache.commons.beanutils.Converter;
import java.util.concurrent.TimeUnit;
/**
* Represents a length of something, like "3 seconds"
*
* This supports parameter injection, such as via {@link QueryParameter}.
*
* @author Kohsuke Kawaguchi
* @since 1.489
*/
public class TimeDuration {
private final long millis;
public TimeDuration(long millis) {
this.millis = millis;
}
public int getTime() {
return (int)millis;
}
public long getTimeInMillis() {
return millis;
}
public long as(TimeUnit t) {
return t.convert(millis,TimeUnit.MILLISECONDS);
}
public static class StaplerConverterImpl implements Converter {
public Object convert(Class type, Object value) {
if (value==null)
return null;
if (value instanceof String) {
String delay = (String)value;
try {
// TODO: more unit handling
if(delay.endsWith("sec")) delay=delay.substring(0,delay.length()-3);
if(delay.endsWith("secs")) delay=delay.substring(0,delay.length()-4);
return new TimeDuration(Long.parseLong(delay));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid time duration value: "+delay);
}
}
throw new UnsupportedOperationException();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册