Search in sources :

Example 16 with ParameterValue

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

the class ManualConditionTest method testManualPromotionProcessWithInvalidParam.

@Issue("SECURITY-170")
@Test
public /**
 * Verify that the plugin is tolerant against SECURITY-170 in Manual conditions
 */
void testManualPromotionProcessWithInvalidParam() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject();
    ExtensionList<Descriptor> list = j.jenkins.getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(p);
    p.addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("FOO", "BAR", "Test parameter"));
    foo.conditions.add(condition);
    FreeStyleBuild b1 = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    // Promote a build. Also add one invalid parameter
    List<ParameterValue> paramValues = condition.createDefaultValues();
    paramValues.add(new StringParameterValue("INVALID_PARAM", "hacked!"));
    j.assertBuildStatusSuccess(condition.approve(b1, foo, paramValues));
    ManualApproval manualApproval = b1.getAction(ManualApproval.class);
    assertNotNull(manualApproval);
    List<ParameterValue> parameterValues = manualApproval.badge.getParameterValues();
    // Verify that the build succeeds && has no INVALID_PARAM
    PromotedBuildAction statuses = b1.getAction(PromotedBuildAction.class);
    assertNotNull(statuses);
    assertNotNull(statuses.getPromotions());
    assertFalse(statuses.getPromotions().isEmpty());
    Promotion pb = base.getItem("foo").getBuildByNumber(1);
    assertNotNull("INVALID_PARAM should not be injected into the environment", pb.getEnvironment(TaskListener.NULL).get("INVALID_PARAM", null));
}
Also used : ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) ParameterValue(hudson.model.ParameterValue) StringParameterValue(hudson.model.StringParameterValue) 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) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 17 with ParameterValue

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

the class PromotionTargetActionTest method test1.

/**
 * When a project is created, built, and renamed, then the old build is created,
 * that results in NPE.
 */
@Test
public void test1() throws Exception {
    FreeStyleProject up = j.createFreeStyleProject("up");
    up.setCustomWorkspace(j.createTmpDir().getPath());
    // promote if the downstream passes
    JobPropertyImpl promotion = new JobPropertyImpl(up);
    up.addProperty(promotion);
    PromotionProcess proc = promotion.addProcess("promo");
    proc.conditions.add(new ManualCondition());
    FreeStyleBuild b = j.assertBuildStatusSuccess(up.scheduleBuild2(0));
    b.addAction(new ManualApproval(proc.getName(), Collections.<ParameterValue>emptyList()));
    b.save();
    // check for promotion
    Promotion p = j.assertBuildStatusSuccess(proc.considerPromotion2(b));
    up.renameTo("up2");
    assertSame(b, p.getTargetBuildOrFail());
}
Also used : ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) ParameterValue(hudson.model.ParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) Test(org.junit.Test)

Example 18 with ParameterValue

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

the class ManualCondition method doApprove.

/**
 * Web method to handle the approval action submitted by the user.
 */
@POST
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) ParameterDefinition(hudson.model.ParameterDefinition) POST(org.kohsuke.stapler.verb.POST)

Example 19 with ParameterValue

use of hudson.model.ParameterValue 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(), (String) defaultParameterValue.getValue());
                }
            }
        }
        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 20 with ParameterValue

use of hudson.model.ParameterValue 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.BadRequestException("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.BadRequestException("parameters.name is required element");
                }
                ParameterDefinition pd = pp.getParameterDefinition(name);
                if (pd == null) {
                    throw new ServiceException.BadRequestException("No such parameter definition: " + name);
                }
                ParameterValue parameterValue = pd.createValue(request, p);
                if (parameterValue != null) {
                    values.add(parameterValue);
                    pdsInRequest.add(pd);
                } else {
                    throw new ServiceException.BadRequestException("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.BadRequestException("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)

Aggregations

ParameterValue (hudson.model.ParameterValue)20 ParameterDefinition (hudson.model.ParameterDefinition)9 ManualCondition (hudson.plugins.promoted_builds.conditions.ManualCondition)7 ArrayList (java.util.ArrayList)7 ParametersAction (hudson.model.ParametersAction)6 StringParameterValue (hudson.model.StringParameterValue)6 Test (org.junit.Test)6 FreeStyleBuild (hudson.model.FreeStyleBuild)5 FreeStyleProject (hudson.model.FreeStyleProject)5 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)5 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)5 EnvVars (hudson.EnvVars)4 StringParameterDefinition (hudson.model.StringParameterDefinition)4 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)4 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)4 JSONObject (net.sf.json.JSONObject)4 Action (hudson.model.Action)3 Descriptor (hudson.model.Descriptor)3 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)3 JSONArray (net.sf.json.JSONArray)3