Search in sources :

Example 6 with ParameterDefinition

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

the class Promotion method getParameterDefinitionsWithValue.

/**
     * Gets parameter definitions from the {@link ManualCondition}.
     * @return List of parameter definitions to be presented.
     *         May be empty if there is no {@link ManualCondition}.
     */
@Nonnull
public List<ParameterDefinition> getParameterDefinitionsWithValue() {
    List<ParameterDefinition> definitions = new ArrayList<ParameterDefinition>();
    ManualCondition manualCondition = (ManualCondition) getProject().getPromotionCondition(ManualCondition.class.getName());
    if (manualCondition == null) {
        return definitions;
    }
    for (ParameterValue pvalue : getParameterValues()) {
        ParameterDefinition pdef = manualCondition.getParameterDefinition(pvalue.getName());
        if (pdef == null) {
            // May happen only in the case of the wrong form submission
            continue;
        }
        definitions.add(pdef.copyWithDefaultValue(pvalue));
    }
    return definitions;
}
Also used : ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) ParameterValue(hudson.model.ParameterValue) ArrayList(java.util.ArrayList) ParameterDefinition(hudson.model.ParameterDefinition) Nonnull(javax.annotation.Nonnull)

Example 7 with ParameterDefinition

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

the class CascadingUtil method setParameterDefinitionProperties.

/**
     * Sets parameterDefinitionProperties for current job. This method is recursively executed for cascading children
     * for setting valid {@link ParametersDefinitionProperty#owner} value.
     *
     * @param job job.
     * @param key parameter key,
     * @param parameterDefinitionProperties new properties to set.
     */
@SuppressWarnings("unchecked")
public static void setParameterDefinitionProperties(Job job, String key, CopyOnWriteList<ParametersDefinitionProperty> parameterDefinitionProperties) {
    CopyOnWriteListProjectProperty projectProperty = getCopyOnWriteListProjectProperty(job, key);
    CopyOnWriteList<ParametersDefinitionProperty> pdProperties = new CopyOnWriteList<ParametersDefinitionProperty>();
    //Create new instance for each parameter in order to set owner and use in cascading children.
    for (ParametersDefinitionProperty pdp : parameterDefinitionProperties) {
        ParametersDefinitionProperty copiedDefinitionProperty = new ParametersDefinitionProperty(new ArrayList<ParameterDefinition>(pdp.getParameterDefinitions()));
        copiedDefinitionProperty.setOwner((AbstractProject) job);
        pdProperties.add(copiedDefinitionProperty);
    }
    projectProperty.setValue(pdProperties);
    Set<String> cascadingChildrenNames = job.getCascadingChildrenNames();
    //Iterate through cascading children and recursively update property for each child.
    if (null != cascadingChildrenNames) {
        for (String childName : cascadingChildrenNames) {
            Job childJob = (Job) Hudson.getInstance().getItem(childName);
            //Check only direct children in order to avoid deep checking for properties overridden properties.
            if (null != childJob && StringUtils.equals(job.getName(), childJob.getCascadingProjectName())) {
                CopyOnWriteListProjectProperty childProperty = getCopyOnWriteListProjectProperty(childJob, key);
                //If child value is equal to parent - mark this value as unmodified.
                if (!childProperty.allowOverrideValue(childProperty.getValue(), pdProperties)) {
                    childProperty.setOverridden(false);
                } else if (!childProperty.isOverridden()) {
                    //If child property was not overridden, update this property and cascading children if any.
                    setParameterDefinitionProperties(childJob, key, parameterDefinitionProperties);
                }
            }
        }
    }
}
Also used : ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) CopyOnWriteListProjectProperty(org.hudsonci.model.project.property.CopyOnWriteListProjectProperty) ICascadingJob(org.hudsonci.api.model.ICascadingJob) Job(hudson.model.Job) ParameterDefinition(hudson.model.ParameterDefinition)

Example 8 with ParameterDefinition

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

the class CascadingParameterDefinitionPropertiesTest method setUp.

@Before
public void setUp() throws Exception {
    p1 = new FreeStyleProjectMock("p1");
    p2 = new FreeStyleProjectMock("p2");
    p3 = new FreeStyleProjectMock("p3");
    p2.setCascadingProject(p1);
    CascadingUtil.linkCascadingProjectsToChild(p1, "p2");
    p3.setCascadingProject(p2);
    CascadingUtil.linkCascadingProjectsToChild(p2, "p3");
    Hudson hudson = createMock(Hudson.class);
    expect(hudson.getItem("p2")).andReturn(p2).anyTimes();
    expect(hudson.getItem("p3")).andReturn(p3).anyTimes();
    mockStatic(Hudson.class);
    expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
    replay(Hudson.class, hudson);
    propertyKey = Job.PARAMETERS_DEFINITION_JOB_PROPERTY_PROPERTY_NAME;
    List<ParameterDefinition> parameterDefinitionList = new ArrayList<ParameterDefinition>();
    parameterDefinitionList.add(new BooleanParameterDefinition("name", true, "d1"));
    ParametersDefinitionProperty property = new ParametersDefinitionProperty(parameterDefinitionList);
    copyOnWriteList = new CopyOnWriteList<ParametersDefinitionProperty>();
    copyOnWriteList.add(property);
}
Also used : FreeStyleProjectMock(hudson.model.FreeStyleProjectMock) BooleanParameterDefinition(hudson.model.BooleanParameterDefinition) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) Hudson(hudson.model.Hudson) ArrayList(java.util.ArrayList) BooleanParameterDefinition(hudson.model.BooleanParameterDefinition) ParameterDefinition(hudson.model.ParameterDefinition) Before(org.junit.Before)

Example 9 with ParameterDefinition

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

the class ManualConditionInheritanceTest method testManualPromotionProcessViaWebClient.

@Test
public void testManualPromotionProcessViaWebClient() throws Exception {
    InheritanceProjectsPair inheritanceProjectsPair = j.createInheritanceProjectDerivedWithBase();
    ExtensionList<Descriptor> list = Jenkins.getInstance().getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(inheritanceProjectsPair.getBase());
    inheritanceProjectsPair.getDerived().addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    InheritanceBuild b1 = j.assertBuildStatusSuccess(inheritanceProjectsPair.getDerived().scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = j.createWebClient().getPage(b1, "promotion");
    //Approve Promotion
    List<HtmlForm> forms = getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertTrue(forms.size() == 1);
    HtmlForm form = forms.get(0);
    List<HtmlElement> parameters = getFormParameters(form);
    assertTrue(parameters.size() == condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "1");
    }
    j.submit(forms.get(0));
    j.waitUntilNoActivity();
    //We cannot assume that the process will contain builds because the process added to base project is different to the one in derived. 
    final JobPropertyImpl jobProperty = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                because that version of the plugin uses inheritance only for certain predefined cases: 
                -specific methods on the call stack
                -url paths.
                This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
               */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty);
    final PromotionProcess fooDerived = jobProperty.getItem("foo");
    ManualApproval approval = b1.getAction(ManualApproval.class);
    assertNotNull(approval);
    SortedMap<Integer, Promotion> builds = fooDerived.getBuildsAsMap();
    assertNotNull(builds);
    assertTrue(builds.size() == 1);
    //Re-Execute approved promotion
    page = j.createWebClient().getPage(b1, "promotion");
    forms = getFormsByName(page, "build");
    assertFalse(forms.isEmpty());
    assertTrue(forms.size() == 1);
    form = forms.get(0);
    parameters = getFormParameters(form);
    assertTrue(parameters.size() == condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "2");
    }
    j.submit(form);
    j.waitUntilNoActivity();
    final JobPropertyImpl jobProperty2 = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                  because that version of the plugin uses inheritance only for certain predefined cases: 
                  -specific methods on the call stack
                  -url paths.
                  This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
                 */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty2);
    final PromotionProcess fooDerived2 = jobProperty2.getItem("foo");
    builds = fooDerived2.getBuildsAsMap();
    assertTrue(builds.size() == 2);
    List<ManualApproval> actions = b1.getActions(ManualApproval.class);
    assertTrue(actions.size() == 1);
    PromotedBuildAction buildActions = b1.getAction(PromotedBuildAction.class);
    int buildIndex = 1;
    String valueSufix = "1";
    List<Promotion> promotions = new ArrayList<Promotion>();
    promotions.addAll(builds.values());
    Collections.reverse(promotions);
    for (Promotion build : promotions) {
        List<ParameterDefinition> values = build.getParameterDefinitionsWithValue();
        assertTrue(values.size() == condition.getParameterDefinitions().size());
        for (ParameterDefinition v : values) {
            assertTrue(v instanceof StringParameterDefinition);
            String pvalue = ((StringParameterDefinition) v).getDefaultValue();
            assertTrue(pvalue.endsWith(valueSufix));
        }
        buildIndex++;
        valueSufix += buildIndex;
    }
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) Promotion(hudson.plugins.promoted_builds.Promotion) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Descriptor(hudson.model.Descriptor) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Example 10 with ParameterDefinition

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

the class ManualConditionTest method testManualPromotionProcessViaWebClient.

public void testManualPromotionProcessViaWebClient() 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 foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    FreeStyleBuild b1 = assertBuildStatusSuccess(p.scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = createWebClient().getPage(b1, "promotion");
    //Approve Promotion
    List<HtmlForm> forms = getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertTrue(forms.size() == 1);
    HtmlForm form = forms.get(0);
    List<HtmlElement> parameters = getFormParameters(form);
    assertTrue(parameters.size() == condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "1");
    }
    submit(forms.get(0));
    ManualApproval approval = b1.getAction(ManualApproval.class);
    assertNotNull(approval);
    SortedMap<Integer, Promotion> builds = foo.getBuildsAsMap();
    assertNotNull(builds);
    assertTrue(builds.size() == 1);
    //Re-Execute approved promotion
    page = createWebClient().getPage(b1, "promotion");
    forms = getFormsByName(page, "build");
    assertFalse(forms.isEmpty());
    assertTrue(forms.size() == 1);
    form = forms.get(0);
    parameters = getFormParameters(form);
    assertTrue(parameters.size() == condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "2");
    }
    submit(form);
    builds = foo.getBuildsAsMap();
    assertTrue(builds.size() == 2);
    List<ManualApproval> actions = b1.getActions(ManualApproval.class);
    assertTrue(actions.size() == 1);
    PromotedBuildAction buildActions = b1.getAction(PromotedBuildAction.class);
    int buildIndex = 1;
    String valueSufix = "1";
    List<Promotion> promotions = new ArrayList<Promotion>();
    promotions.addAll(builds.values());
    Collections.reverse(promotions);
    for (Promotion build : promotions) {
        List<ParameterDefinition> values = build.getParameterDefinitionsWithValue();
        assertTrue(values.size() == condition.getParameterDefinitions().size());
        for (ParameterDefinition v : values) {
            assertTrue(v instanceof StringParameterDefinition);
            String pvalue = ((StringParameterDefinition) v).getDefaultValue();
            assertTrue(pvalue.endsWith(valueSufix));
        }
        buildIndex++;
        valueSufix += buildIndex;
    }
    for (Status status : buildActions.getPromotions()) {
        assertNotNull(status.getLast() != null);
        List<ParameterDefinition> values = status.getLast().getParameterDefinitionsWithValue();
        assertTrue(values.size() == condition.getParameterDefinitions().size());
    }
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) FreeStyleBuild(hudson.model.FreeStyleBuild) Promotion(hudson.plugins.promoted_builds.Promotion) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) DescriptorImpl(hudson.plugins.promoted_builds.JobPropertyImpl.DescriptorImpl) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Status(hudson.plugins.promoted_builds.Status) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Descriptor(hudson.model.Descriptor) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition)

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