Search in sources :

Example 1 with ParameterDefinition

use of hudson.model.ParameterDefinition in project hudson-2.x by hudson.

the class BuildCommand method run.

protected int run() throws Exception {
    job.checkPermission(Item.BUILD);
    ParametersAction a = null;
    if (!parameters.isEmpty()) {
        ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
        if (pdp == null)
            throw new AbortException(job.getFullDisplayName() + " is not parameterized but the -p option was specified");
        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (Entry<String, String> e : parameters.entrySet()) {
            String name = e.getKey();
            ParameterDefinition pd = pdp.getParameterDefinition(name);
            if (pd == null)
                throw new AbortException(String.format("\'%s\' is not a valid parameter. Did you mean %s?", name, EditDistance.findNearest(name, pdp.getParameterDefinitionNames())));
            values.add(pd.createValue(this, e.getValue()));
        }
        for (ParameterDefinition pd : pdp.getParameterDefinitions()) {
            if (parameters.get(pd.getName()) == null) {
                values.add(pd.getDefaultParameterValue());
            }
        }
        a = new ParametersAction(values);
    }
    Future<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(), a);
    if (!sync)
        return 0;
    // wait for the completion
    AbstractBuild b = f.get();
    stdout.println("Completed " + b.getFullDisplayName() + " : " + b.getResult());
    return b.getResult().ordinal;
}
Also used : ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) ParameterValue(hudson.model.ParameterValue) AbstractBuild(hudson.model.AbstractBuild) ArrayList(java.util.ArrayList) ParametersAction(hudson.model.ParametersAction) AbortException(hudson.AbortException) ParameterDefinition(hudson.model.ParameterDefinition)

Example 2 with ParameterDefinition

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

the class PipelineStepImpl method parseValue.

private Object parseValue(InputStepExecution execution, JSONArray parameters, StaplerRequest request) throws IOException, InterruptedException {
    Map<String, Object> mapResult = new HashMap<String, Object>();
    InputStep input = execution.getInput();
    for (Object o : parameters) {
        JSONObject p = (JSONObject) o;
        String name = (String) p.get(NAME_ELEMENT);
        if (name == null) {
            throw new ServiceException.BadRequestExpception("name is required parameter element");
        }
        ParameterDefinition d = null;
        for (ParameterDefinition def : input.getParameters()) {
            if (def.getName().equals(name))
                d = def;
        }
        if (d == null)
            throw new ServiceException.BadRequestExpception("No such parameter definition: " + name);
        ParameterValue v = d.createValue(request, p);
        if (v == null) {
            continue;
        }
        mapResult.put(name, convert(name, v));
    }
    // If a destination value is specified, push the submitter to it.
    String valueName = input.getSubmitterParameter();
    if (valueName != null && !valueName.isEmpty()) {
        Authentication a = Jenkins.getAuthentication();
        mapResult.put(valueName, a.getName());
    }
    switch(mapResult.size()) {
        case 0:
            // no value if there's no parameter
            return null;
        case 1:
            return mapResult.values().iterator().next();
        default:
            return mapResult;
    }
}
Also used : JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) FileParameterValue(hudson.model.FileParameterValue) ParameterValue(hudson.model.ParameterValue) HashMap(java.util.HashMap) Authentication(org.acegisecurity.Authentication) InputStep(org.jenkinsci.plugins.workflow.support.steps.input.InputStep) BlueInputStep(io.jenkins.blueocean.rest.model.BlueInputStep) JSONObject(net.sf.json.JSONObject) ParameterDefinition(hudson.model.ParameterDefinition)

Example 3 with ParameterDefinition

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

the class Status method doBuild.

/**
     * Schedules a new build.
     * @param req Request
     * @param rsp Response
     * @throws IOException Functional error
     * @throws ServletException Request handling error
     */
public void doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    final PromotionProcess process = getProcess();
    if (process == null) {
        throw new AbortException("Cannot retrieve the promotion process");
    }
    AbstractBuild<?, ?> target = getTarget();
    if (target == null) {
        throw new AbortException("Cannot get the target build to be promoted");
    }
    ManualCondition manualCondition = (ManualCondition) process.getPromotionCondition(ManualCondition.class.getName());
    if (!target.hasPermission(Promotion.PROMOTE)) {
        if (manualCondition == null || (!manualCondition.getUsersAsSet().isEmpty() && !manualCondition.isInUsersList() && !manualCondition.isInGroupList()))
            return;
    }
    JSONObject formData = req.getSubmittedForm();
    List<ParameterValue> paramValues = null;
    if (formData != null) {
        paramValues = new ArrayList<ParameterValue>();
        if (manualCondition != null) {
            List<ParameterDefinition> parameterDefinitions = manualCondition.getParameterDefinitions();
            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 = manualCondition.getParameterDefinition(name);
                    if (d == null)
                        throw new IllegalArgumentException("No such parameter definition: " + name);
                    paramValues.add(d.createValue(req, jo));
                }
            }
        }
    }
    if (paramValues == null) {
        paramValues = new ArrayList<ParameterValue>();
    }
    Future<Promotion> f = process.scheduleBuild2(target, new UserCause(), paramValues);
    if (f == null)
        LOGGER.warning("Failing to schedule the promotion of " + target);
    // TODO: we need better visual feed back so that the user knows that the build happened.
    rsp.forwardToPreviousPage(req);
}
Also used : ParameterValue(hudson.model.ParameterValue) UserCause(hudson.model.Cause.UserCause) JSONArray(net.sf.json.JSONArray) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) JSONObject(net.sf.json.JSONObject) JSONObject(net.sf.json.JSONObject) AbortException(hudson.AbortException) ParameterDefinition(hudson.model.ParameterDefinition)

Example 4 with ParameterDefinition

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

the class PromotionProcessTest method testIsVisibleResolvesDefaultParameterValueIndirectly.

public void testIsVisibleResolvesDefaultParameterValueIndirectly() throws Exception {
    FreeStyleProject project = createFreeStyleProject("project");
    final List<ParameterDefinition> parameters = new ArrayList<ParameterDefinition>();
    ParametersDefinitionProperty parametersProperty = new ParametersDefinitionProperty(parameters);
    parameters.add(new StringParameterDefinition("IndirectVisibility", "false"));
    parameters.add(new StringParameterDefinition("Visibility", "${IndirectVisibility}"));
    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 5 with ParameterDefinition

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

the class ManualConditionBug22005 method testPromotionProcess.

public void testPromotionProcess() throws Exception {
    FreeStyleProject p = createFreeStyleProject();
    ExtensionList<Descriptor> list = hudson.getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(p);
    p.addProperty(base);
    PromotionProcess prom0 = createPromotionProcess(base, "PROM0");
    ManualCondition prom0Condition = prom0.conditions.get(ManualCondition.class);
    PromotionProcess prom1 = createPromotionProcess(base, "PROM1");
    ManualCondition prom1Condition = prom1.conditions.get(ManualCondition.class);
    PromotionProcess prom2 = createPromotionProcess(base, "PROM2");
    ManualCondition prom2Condition = prom2.conditions.get(ManualCondition.class);
    FreeStyleBuild b1 = assertBuildStatusSuccess(p.scheduleBuild2(0));
    Promotion p0b1 = assertBuildStatusSuccess(prom0Condition.approve(b1, prom0));
    assertEquals(2, p0b1.getParameterValues().size());
    assertEquals(2, p0b1.getParameterDefinitionsWithValue().size());
    Promotion p1b1 = assertBuildStatusSuccess(prom1Condition.approve(b1, prom1));
    assertEquals(2, p1b1.getParameterValues().size());
    assertEquals(2, p1b1.getParameterDefinitionsWithValue().size());
    Promotion p2b1 = assertBuildStatusSuccess(prom2Condition.approve(b1, prom2));
    assertEquals(2, p2b1.getParameterValues().size());
    assertEquals(2, p2b1.getParameterDefinitionsWithValue().size());
    List<ManualApproval> approvals = b1.getActions(ManualApproval.class);
    assertEquals(3, approvals.size());
    PromotedBuildAction promBuildAction = b1.getAction(PromotedBuildAction.class);
    List<Status> statuses = promBuildAction.getPromotions();
    assertEquals(3, statuses.size());
    for (Status status : statuses) {
        Promotion lastBuild = status.getLast();
        List<ParameterDefinition> lastBuildParameters = lastBuild.getParameterDefinitionsWithValue();
        assertEquals(2, lastBuildParameters.size());
    }
}
Also used : Status(hudson.plugins.promoted_builds.Status) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) FreeStyleBuild(hudson.model.FreeStyleBuild) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Promotion(hudson.plugins.promoted_builds.Promotion) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Descriptor(hudson.model.Descriptor) StringParameterDefinition(hudson.model.StringParameterDefinition) 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