Search in sources :

Example 6 with Descriptor

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

the class ManualConditionBug22005 method testPromotionProcessViaWebClient.

@Test
public void testPromotionProcessViaWebClient() 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);
    createPromotionProcess(base, "PROM0");
    createPromotionProcess(base, "PROM1");
    createPromotionProcess(base, "PROM2");
    FreeStyleBuild b1 = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = j.createWebClient().getPage(b1, "promotion");
    // Approve Promotion
    List<HtmlForm> forms = ManualConditionTest.getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertEquals(3, forms.size());
    for (HtmlForm form : forms) {
        submit(form);
    }
    // reload promotions page
    page = j.createWebClient().getPage(b1, "promotion");
    forms = ManualConditionTest.getFormsByName(page, "build");
    for (HtmlForm form : forms) {
        List<HtmlElement> parameters = ManualConditionTest.getFormParameters(form);
        assertEquals(2, parameters.size());
    }
}
Also used : ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Descriptor(hudson.model.Descriptor) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Test(org.junit.Test)

Example 7 with Descriptor

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

the class ManualConditionInheritanceTest method testManualPromotionProcess.

@Test
public void testManualPromotionProcess() throws Exception {
    InheritanceProjectsPair inheritanceProjectsPair = j.createInheritanceProjectDerivedWithBase();
    ExtensionList<Descriptor> list = Jenkins.get().getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(inheritanceProjectsPair.getBase());
    inheritanceProjectsPair.getBase().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));
    // promote a build
    List<ParameterValue> paramValues = condition.createDefaultValues();
    // try to add duplicate values
    paramValues.addAll(condition.createDefaultValues());
    // We cannot assume that the process will contain builds because the process added to base project is different to the one in derived.
    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);
    PromotionProcess fooDerived = jobProperty.getItem("foo");
    j.assertBuildStatusSuccess(condition.approve(b1, fooDerived, paramValues));
    ManualApproval manualApproval = b1.getAction(ManualApproval.class);
    assertNotNull(manualApproval);
    PromotedBuildAction statuses = b1.getAction(PromotedBuildAction.class);
    assertNotNull(statuses);
    assertNotNull(statuses.getPromotions());
    assertFalse(statuses.getPromotions().isEmpty());
}
Also used : InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) StringParameterDefinition(hudson.model.StringParameterDefinition) ParameterValue(hudson.model.ParameterValue) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Descriptor(hudson.model.Descriptor) Test(org.junit.Test)

Example 8 with Descriptor

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

the class DescribableListUtilTest method testConvertToProjectProperties3.

@Test
public void testConvertToProjectProperties3() throws IOException {
    Hudson hudson = createMock(Hudson.class);
    Mailer.DescriptorImpl descriptor = createMock(Mailer.DescriptorImpl.class);
    String mailerName = "hudson-tasks-Mailer";
    expect(descriptor.getJsonSafeClassName()).andReturn(mailerName);
    expect(hudson.getDescriptorOrDie(Mailer.class)).andReturn(descriptor);
    mockStatic(Hudson.class);
    expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
    prepareJob();
    DescribableList<Publisher, Descriptor<Publisher>> list = new DescribableList<Publisher, Descriptor<Publisher>>();
    list.add(new Mailer());
    Map<String, ExternalProjectProperty<Publisher>> map = DescribableListUtil.convertToProjectProperties(list, job);
    assertNotNull(map);
    assertEquals(map.size(), 1);
    assertNotNull(map.get(mailerName));
    assertEquals(map.get(mailerName).getValue().getClass(), Mailer.class);
}
Also used : Hudson(hudson.model.Hudson) ExternalProjectProperty(org.hudsonci.model.project.property.ExternalProjectProperty) Mailer(hudson.tasks.Mailer) Descriptor(hudson.model.Descriptor) Publisher(hudson.tasks.Publisher) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with Descriptor

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

the class SCM method _for.

/**
 * Returns the list of {@link SCMDescriptor}s that are applicable to the given project.
 */
public static List<SCMDescriptor<?>> _for(final AbstractProject project) {
    if (project == null)
        return all();
    final Descriptor pd = Hudson.getInstance().getDescriptor((Class) project.getClass());
    List<SCMDescriptor<?>> r = new ArrayList<SCMDescriptor<?>>();
    for (SCMDescriptor<?> scmDescriptor : all()) {
        if (!scmDescriptor.isApplicable(project))
            continue;
        if (pd instanceof AbstractProjectDescriptor) {
            AbstractProjectDescriptor apd = (AbstractProjectDescriptor) pd;
            if (!apd.isApplicable(scmDescriptor))
                continue;
        }
        r.add(scmDescriptor);
    }
    return r;
}
Also used : ArrayList(java.util.ArrayList) Descriptor(hudson.model.Descriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor)

Example 10 with Descriptor

use of hudson.model.Descriptor in project workflow-cps-plugin by jenkinsci.

the class DSL method invokeDescribable.

/**
 * When {@link #invokeMethod(String, Object)} is calling a generic {@link Descriptor}
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object invokeDescribable(String symbol, Object _args) {
    List<StepDescriptor> metaSteps = StepDescriptor.metaStepsOf(symbol);
    StepDescriptor metaStep = metaSteps.size() == 1 ? metaSteps.get(0) : null;
    boolean singleArgumentOnly = false;
    if (metaStep != null) {
        Descriptor symbolDescriptor = SymbolLookup.get().findDescriptor((Class) (metaStep.getMetaStepArgumentType()), symbol);
        DescribableModel<?> symbolModel = DescribableModel.of(symbolDescriptor.clazz);
        singleArgumentOnly = symbolModel.hasSingleRequiredParameter() && symbolModel.getParameters().size() == 1;
    }
    // The only time a closure is valid is when the resulting Describable is immediately executed via a meta-step
    NamedArgsAndClosure args = parseArgs(_args, metaStep != null && metaStep.takesImplicitBlockArgument(), UninstantiatedDescribable.ANONYMOUS_KEY, singleArgumentOnly);
    UninstantiatedDescribable ud = new UninstantiatedDescribable(symbol, null, args.namedArgs);
    if (metaStep == null) {
        // might be resolved with a specific type.
        return ud;
    } else {
        Descriptor d = SymbolLookup.get().findDescriptor((Class) (metaStep.getMetaStepArgumentType()), symbol);
        try {
            // execute this Describable through a meta-step
            // split args between MetaStep (represented by mm) and Describable (represented by dm)
            DescribableModel<?> mm = DescribableModel.of(metaStep.clazz);
            DescribableModel<?> dm = DescribableModel.of(d.clazz);
            DescribableParameter p = mm.getFirstRequiredParameter();
            if (p == null) {
                // meta-step not having a required parameter is a bug in this meta step
                throw new IllegalArgumentException("Attempted to use meta-step " + metaStep.getFunctionName() + " to process " + symbol + " but this meta-step is buggy; it has no mandatory parameter");
            }
            // order of preference:
            // 1. mandatory parameter in mm
            // 2. mandatory parameter in dm
            // 3. other parameters in mm
            // 4. other parameters in dm
            // mm is preferred over dm because that way at least the arguments that mm defines
            // act consistently
            Map<String, Object> margs = new TreeMap<>();
            Map<String, Object> dargs = new TreeMap<>();
            for (Entry<String, ?> e : ud.getArguments().entrySet()) {
                String n = e.getKey();
                Object v = e.getValue();
                DescribableParameter mp = mm.getParameter(n);
                DescribableParameter dp = dm.getParameter(n);
                if (mp != null && mp.isRequired()) {
                    margs.put(n, v);
                } else if (dp != null && dp.isRequired()) {
                    dargs.put(n, v);
                } else if (mp != null) {
                    margs.put(n, v);
                } else {
                    // dp might be null, but this error will be caught by UD.instantiate() later
                    dargs.put(n, v);
                }
            }
            ud = new UninstantiatedDescribable(symbol, null, dargs);
            margs.put(p.getName(), ud);
            return invokeStep(metaStep, new NamedArgsAndClosure(margs, args.body));
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to prepare " + symbol + " step", e);
        }
    }
}
Also used : UninstantiatedDescribable(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable) DescribableParameter(org.jenkinsci.plugins.structs.describable.DescribableParameter) GString(groovy.lang.GString) TreeMap(java.util.TreeMap) NoStaplerConstructorException(org.kohsuke.stapler.NoStaplerConstructorException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) MissingContextVariableException(org.jenkinsci.plugins.workflow.steps.MissingContextVariableException) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) ClassDescriptor(org.kohsuke.stapler.ClassDescriptor) Descriptor(hudson.model.Descriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) GroovyObject(groovy.lang.GroovyObject)

Aggregations

Descriptor (hudson.model.Descriptor)22 Test (org.junit.Test)9 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)8 ArrayList (java.util.ArrayList)8 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)7 StringParameterDefinition (hudson.model.StringParameterDefinition)6 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)6 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)6 FreeStyleBuild (hudson.model.FreeStyleBuild)5 FreeStyleProject (hudson.model.FreeStyleProject)5 Promotion (hudson.plugins.promoted_builds.Promotion)4 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)4 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 AbstractProjectDescriptor (hudson.model.AbstractProject.AbstractProjectDescriptor)3 ParameterDefinition (hudson.model.ParameterDefinition)3 ParameterValue (hudson.model.ParameterValue)3 Jenkins (jenkins.model.Jenkins)3 Describable (hudson.model.Describable)2