Search in sources :

Example 11 with ParameterDefinition

use of hudson.model.ParameterDefinition in project promoted-builds-plugin by jenkinsci.

the class PromotionProcessTest method testIsVisibleResolvesDefaultParameterValue.

public void testIsVisibleResolvesDefaultParameterValue() throws Exception {
    FreeStyleProject project = createFreeStyleProject("project");
    final List<ParameterDefinition> parameters = new ArrayList<ParameterDefinition>();
    ParametersDefinitionProperty parametersProperty = new ParametersDefinitionProperty(parameters);
    parameters.add(new StringParameterDefinition("Visibility", "false"));
    project.addProperty(parametersProperty);
    JobPropertyImpl jobProperty = new JobPropertyImpl(project);
    project.addProperty(jobProperty);
    PromotionProcess promotionProcess = jobProperty.addProcess("Promotion");
    promotionProcess.isVisible = "${Visibility}";
    assertFalse(promotionProcess.isVisible());
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) ArrayList(java.util.ArrayList) FreeStyleProject(hudson.model.FreeStyleProject) StringParameterDefinition(hudson.model.StringParameterDefinition) ParameterDefinition(hudson.model.ParameterDefinition)

Example 12 with ParameterDefinition

use of hudson.model.ParameterDefinition in project blueocean-plugin by jenkinsci.

the class AbstractPipelineImpl method getParameterDefinitions.

public static List<Object> getParameterDefinitions(Job job) {
    ParametersDefinitionProperty pp = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
    List<Object> pds = new ArrayList<>();
    if (pp != null) {
        for (ParameterDefinition pd : pp.getParameterDefinitions()) {
            pds.add(pd);
        }
    }
    return pds;
}
Also used : ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) ArrayList(java.util.ArrayList) ParameterDefinition(hudson.model.ParameterDefinition)

Example 13 with ParameterDefinition

use of hudson.model.ParameterDefinition in project blueocean-plugin by jenkinsci.

the class RunContainerImpl method getParameterValue.

private List<ParameterValue> getParameterValue(@Nonnull StaplerRequest request) {
    List<ParameterValue> values = new ArrayList<>();
    List<ParameterDefinition> pdsInRequest = new ArrayList<>();
    ParametersDefinitionProperty pp = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
    if (pp == null) {
        return values;
    }
    try {
        JSONObject body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
        if (body.get("parameters") == null && pp.getParameterDefinitions().size() > 0) {
            throw new ServiceException.BadRequestExpception("This is parameterized job, requires parameters");
        }
        if (body.get("parameters") != null) {
            JSONArray pds = JSONArray.fromObject(body.get("parameters"));
            for (Object o : pds) {
                JSONObject p = (JSONObject) o;
                String name = (String) p.get("name");
                if (name == null) {
                    throw new ServiceException.BadRequestExpception("parameters.name is required element");
                }
                ParameterDefinition pd = pp.getParameterDefinition(name);
                if (pd == null) {
                    throw new ServiceException.BadRequestExpception("No such parameter definition: " + name);
                }
                ParameterValue parameterValue = pd.createValue(request, p);
                if (parameterValue != null) {
                    values.add(parameterValue);
                    pdsInRequest.add(pd);
                } else {
                    throw new ServiceException.BadRequestExpception("Invalid value. Cannot retrieve the parameter value: " + name);
                }
            }
            //now check for missing parameters without default values
            if (pdsInRequest.size() != pp.getParameterDefinitions().size()) {
                for (ParameterDefinition pd : pp.getParameterDefinitions()) {
                    if (!pdsInRequest.contains(pd)) {
                        ParameterValue v = pd.getDefaultParameterValue();
                        if (v == null || v.getValue() == null) {
                            throw new ServiceException.BadRequestExpception("Missing parameter: " + pd.getName());
                        }
                        values.add(v);
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
    return values;
}
Also used : ParameterValue(hudson.model.ParameterValue) ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) IOException(java.io.IOException) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) JSONObject(net.sf.json.JSONObject) ParameterDefinition(hudson.model.ParameterDefinition)

Example 14 with ParameterDefinition

use of hudson.model.ParameterDefinition in project promoted-builds-plugin by jenkinsci.

the class PromotionProcess method getDefaultParameterValuesAsEnvVars.

private static EnvVars getDefaultParameterValuesAsEnvVars(AbstractProject owner) {
    EnvVars envVars = null;
    ParametersDefinitionProperty parametersDefinitionProperty = (ParametersDefinitionProperty) owner.getProperty(ParametersDefinitionProperty.class);
    if (parametersDefinitionProperty != null) {
        envVars = new EnvVars();
        for (ParameterDefinition parameterDefinition : parametersDefinitionProperty.getParameterDefinitions()) {
            ParameterValue defaultParameterValue = parameterDefinition.getDefaultParameterValue();
            if (defaultParameterValue != null) {
                if (defaultParameterValue instanceof StringParameterValue) {
                    envVars.put(parameterDefinition.getName(), ((StringParameterValue) defaultParameterValue).value);
                }
            }
        }
        EnvVars.resolve(envVars);
    }
    return envVars;
}
Also used : EnvVars(hudson.EnvVars) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) StringParameterValue(hudson.model.StringParameterValue) ParameterValue(hudson.model.ParameterValue) StringParameterValue(hudson.model.StringParameterValue) ParameterDefinition(hudson.model.ParameterDefinition)

Example 15 with ParameterDefinition

use of hudson.model.ParameterDefinition in project promoted-builds-plugin by jenkinsci.

the class ManualCondition method doApprove.

/**
     * Web method to handle the approval action submitted by the user.
     */
public void doApprove(StaplerRequest req, StaplerResponse rsp, @AncestorInPath PromotionProcess promotionProcess, @AncestorInPath AbstractBuild<?, ?> build) throws IOException, ServletException {
    JSONObject formData = req.getSubmittedForm();
    if (canApprove(promotionProcess, build)) {
        List<ParameterValue> paramValues = new ArrayList<ParameterValue>();
        if (parameterDefinitions != null && !parameterDefinitions.isEmpty()) {
            JSONArray a = JSONArray.fromObject(formData.get("parameter"));
            for (Object o : a) {
                JSONObject jo = (JSONObject) o;
                String name = jo.getString("name");
                ParameterDefinition d = getParameterDefinition(name);
                if (d == null)
                    throw new IllegalArgumentException("No such parameter definition: " + name);
                paramValues.add(d.createValue(req, jo));
            }
        }
        approve(build, promotionProcess, paramValues);
    }
    rsp.sendRedirect2("../../../..");
}
Also used : JSONObject(net.sf.json.JSONObject) ParameterValue(hudson.model.ParameterValue) ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) JSONObject(net.sf.json.JSONObject) SimpleParameterDefinition(hudson.model.SimpleParameterDefinition) ParameterDefinition(hudson.model.ParameterDefinition)

Aggregations

ParameterDefinition (hudson.model.ParameterDefinition)15 ArrayList (java.util.ArrayList)10 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)8 ParameterValue (hudson.model.ParameterValue)7 StringParameterDefinition (hudson.model.StringParameterDefinition)5 FreeStyleProject (hudson.model.FreeStyleProject)4 ManualCondition (hudson.plugins.promoted_builds.conditions.ManualCondition)4 JSONObject (net.sf.json.JSONObject)4 Descriptor (hudson.model.Descriptor)3 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)3 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)3 Promotion (hudson.plugins.promoted_builds.Promotion)3 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)3 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)3 JSONArray (net.sf.json.JSONArray)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 AbortException (hudson.AbortException)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2