提交 281487de 编写于 作者: H huybrechts

[HUDSON-2409] build parameters can be submitted with a GET or POST to...

[HUDSON-2409] build parameters can be submitted with a GET or POST to /job/<name>/buildWithParameters
Only String parameters are currently supported.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@14718 71c3de6d-444a-0410-be80-ed276b4c234a
上级 42d83b44
......@@ -969,6 +969,23 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
}
rsp.forwardToPreviousPage(req);
}
/**
* 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 {
BuildAuthorizationToken.checkPermission(this, authToken, req, rsp);
ParametersDefinitionProperty pp = getProperty(ParametersDefinitionProperty.class);
if (pp != null) {
pp.buildWithParameters(req,rsp);
return;
} else {
throw new IllegalStateException("This build is not parameterized!");
}
}
/**
* Schedules a new SCM polling command.
......
......@@ -46,4 +46,9 @@ public class FileParameterDefinition extends ParameterDefinition {
return "/help/parameter/file.html";
}
}
@Override
public ParameterValue createValue(StaplerRequest req) {
throw new UnsupportedOperationException();
}
}
......@@ -42,4 +42,9 @@ public class JobParameterDefinition extends ParameterDefinition {
return req.bindJSON(JobParameterValue.class, jo);
}
@Override
public ParameterValue createValue(StaplerRequest req) {
throw new UnsupportedOperationException();
}
}
......@@ -80,8 +80,22 @@ public abstract class ParameterDefinition implements
*/
public abstract ParameterDescriptor getDescriptor();
/**
* Create a parameter value from a form submission
* @param req
* @param jo
* @return
*/
public abstract ParameterValue createValue(StaplerRequest req, JSONObject jo);
/**
* Create a parameter value from a GET (with query string) or POST.
* If no value is available in the request, it returns a default value if possible, or null.
* @param req
* @return
*/
public abstract ParameterValue createValue(StaplerRequest req);
/**
* Returns default parameter value for this definition.
*
......
......@@ -3,6 +3,9 @@ package hudson.model;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
......@@ -10,8 +13,6 @@ import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.ServletException;
/**
* Keeps a list of the parameters defined for a project.
*
......@@ -59,7 +60,7 @@ public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?,
}
List<ParameterValue> values = new ArrayList<ParameterValue>();
JSONObject formData = req.getSubmittedForm();
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
......@@ -79,6 +80,24 @@ public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?,
// send the user back to the job top page.
rsp.sendRedirect(".");
}
public void buildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException {
List<ParameterValue> values = new ArrayList<ParameterValue>();
for (ParameterDefinition d: parameterDefinitions) {
ParameterValue value = d.createValue(req);
if (value != null) {
values.add(value);
} else {
throw new IllegalArgumentException("Parameter " + d.getName() + " was missing.");
}
}
Hudson.getInstance().getQueue().add(
new ParameterizedProjectTask(owner, values), 0);
// send the user back to the job top page.
rsp.sendRedirect(".");
}
/**
* Gets the {@link ParameterDefinition} of the given name, if any.
......
......@@ -42,4 +42,9 @@ public class RunParameterDefinition extends ParameterDefinition {
return req.bindJSON(RunParameterValue.class, jo);
}
@Override
public ParameterValue createValue(StaplerRequest req) {
throw new UnsupportedOperationException();
}
}
......@@ -59,4 +59,15 @@ public class StringParameterDefinition extends ParameterDefinition {
return req.bindJSON(StringParameterValue.class, jo);
}
@Override
public ParameterValue createValue(StaplerRequest req) {
String[] value = req.getParameterValues(getName());
if (value == null) {
return getDefaultParameterValue();
} else if (value.length != 1) {
throw new IllegalArgumentException("Illegal number of parameter values for " + getName() + ": " + value.length);
} else
return new StringParameterValue(getName(), value[0]);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册