org.jenkinsci.plugins.structs.describable.DescribableModel Java Examples

The following examples show how to use org.jenkinsci.plugins.structs.describable.DescribableModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PipelineMetadataService.java    From blueocean-plugin with MIT License 6 votes vote down vote up
/**
 * Function to return all {@link DeclarativeAgent}s present in the system when accessed through the REST API
 */
@GET
@TreeResponse
public ExportedDescribableModel[] doAgentMetadata() {
    List<ExportedDescribableModel> models = new ArrayList<>();

    for (DeclarativeAgentDescriptor d : DeclarativeAgentDescriptor.all()) {
        try {
            DescribableModel<? extends DeclarativeAgent> model = new DescribableModel<>(d.clazz);

            String symbol = symbolForObject(d);
            if ("label".equals(symbol)) { // Label has 2 symbols, but we need "node"
                symbol = "node";
            }
            models.add(new ExportedDescribableModel(model, symbol));
        } catch (NoStaplerConstructorException e) {
            // Ignore!
        }
    }
    return models.toArray(new ExportedDescribableModel[models.size()]);
}
 
Example #2
Source File: PipelineMetadataService.java    From blueocean-plugin with MIT License 6 votes vote down vote up
private @CheckForNull ExportedPipelineStep getStepMetadata(StepDescriptor d) {
    try {
        DescribableModel<? extends Step> model = new DescribableModel<>(d.clazz);

        ExportedPipelineStep step = new ExportedPipelineStep(model, d.getFunctionName(), d);

        // Let any decorators adjust the step properties
        for (ExportedDescribableParameterDecorator decorator : ExtensionList.lookup(ExportedDescribableParameterDecorator.class)) {
            decorator.decorate(step, step.getParameters());
        }

        return step;
    } catch (NoStaplerConstructorException e) {
        // not a normal step?
        return null;
    }
}
 
Example #3
Source File: DockerNodeStepTest.java    From docker-plugin with MIT License 6 votes vote down vote up
@Test
public void defaults() {
    story.then(r -> {
        DockerNodeStep s = new DockerNodeStep("foo");
        s.setCredentialsId("");
        s.setDockerHost("");
        s.setRemoteFs("");
        UninstantiatedDescribable uninstantiated = new DescribableModel<>(DockerNodeStep.class).uninstantiate2(s);
        assertEquals(uninstantiated.toString(), Collections.singleton("image"), uninstantiated.getArguments().keySet());
        r.jenkins.clouds.add(new DockerCloud("whatever", new DockerAPI(new DockerServerEndpoint("unix:///var/run/docker.sock", null)), Collections.emptyList()));
        WorkflowJob j = r.createProject(WorkflowJob.class, "p");
        j.setDefinition(new CpsFlowDefinition(
            dockerNodeWithImage("openjdk:8") + " {\n" +
            "  sh 'java -version && whoami && pwd && touch stuff && ls -lat . ..'\n" +
            "}\n", true));
        r.buildAndAssertSuccess(j);
    });
}
 
Example #4
Source File: PipelineMetadataService.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private @CheckForNull ExportedPipelineFunction getStepMetadata(Descriptor<?> d) {
    String symbol = symbolForObject(d);

    if (symbol != null) {
        ExportedPipelineFunction f = new ExportedPipelineFunction(new DescribableModel<>(d.clazz), symbol);
        // Let any decorators adjust the step properties
        for (ExportedDescribableParameterDecorator decorator : ExtensionList.lookup(ExportedDescribableParameterDecorator.class)) {
            decorator.decorate(f, f.getParameters());
        }

        return f;
    } else {
        return null;
    }
}
 
Example #5
Source File: DockerDirectiveGeneratorTest.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
private JSONObject staplerJsonForDescr(Describable d) {
    DescribableModel<?> m = DescribableModel.of(d.getClass());

    JSONObject o = new JSONObject();
    o.accumulate("stapler-class", d.getClass().getName());
    o.accumulate("$class", d.getClass().getName());
    if (d instanceof OptionalJobProperty) {
        o.accumulate("specified", true);
    }
    for (DescribableParameter param : m.getParameters()) {
        Object v = getValue(param, d);
        if (v != null) {
            if (v instanceof Describable) {
                o.accumulate(param.getName(), staplerJsonForDescr((Describable)v));
            } else if (v instanceof List && !((List) v).isEmpty()) {
                JSONArray a = new JSONArray();
                for (Object obj : (List) v) {
                    if (obj instanceof Describable) {
                        a.add(staplerJsonForDescr((Describable) obj));
                    } else if (obj instanceof Number) {
                        a.add(obj.toString());
                    } else {
                        a.add(obj);
                    }
                }
                o.accumulate(param.getName(), a);
            } else if (v instanceof Number) {
                o.accumulate(param.getName(), v.toString());
            } else {
                o.accumulate(param.getName(), v);
            }
        }
    }
    return o;
}
 
Example #6
Source File: GitHubSCMSourceTraitsTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__configuredInstance__when__uninstantiating__then__deprecatedFieldsIgnored() throws Exception {
    GitHubSCMSource instance = new GitHubSCMSource("repo-owner", "repo", null, false);
    instance.setId("test");

    DescribableModel model = DescribableModel.of(GitHubSCMSource.class);
    UninstantiatedDescribable ud = model.uninstantiate2(instance);
    Map<String, Object> udMap = ud.toMap();
    GitHubSCMSource recreated = (GitHubSCMSource) model.instantiate(udMap);

    assertThat(DescribableModel.uninstantiate2_(recreated).toString(),
            is("@github(id=test,repoOwner=repo-owner,repository=repo)")
    );
    recreated.setBuildOriginBranch(true);
    recreated.setBuildOriginBranchWithPR(false);
    recreated.setBuildOriginPRHead(false);
    recreated.setBuildOriginPRMerge(true);
    recreated.setBuildForkPRHead(true);
    recreated.setBuildForkPRMerge(false);
    recreated.setIncludes("i*");
    recreated.setExcludes("production");
    recreated.setScanCredentialsId("foo");
    assertThat(DescribableModel.uninstantiate2_(recreated).toString(),
            is("@github("
                    + "credentialsId=foo,"
                    + "id=test,"
                    + "repoOwner=repo-owner,"
                    + "repository=repo,"
                    + "traits=["
                    + "@gitHubBranchDiscovery$org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait(strategyId=1), "
                    + "@gitHubPullRequestDiscovery$OriginPullRequestDiscoveryTrait(strategyId=1), "
                    + "@gitHubForkDiscovery$ForkPullRequestDiscoveryTrait("
                    + "strategyId=2,"
                    + "trust=@gitHubTrustPermissions$TrustPermission()), "
                    + "@headWildcardFilter$WildcardSCMHeadFilterTrait(excludes=production,includes=i*)])")
    );
}
 
Example #7
Source File: JUnitResultArchiverTest.java    From junit-plugin with MIT License 5 votes vote down vote up
@Issue("JENKINS-26535")
@Test
public void testDescribableRoundTrip() throws Exception {
    DescribableModel<JUnitResultArchiver> model = new DescribableModel<JUnitResultArchiver>(JUnitResultArchiver.class);
    Map<String,Object> args = new TreeMap<String,Object>();

    args.put("testResults", "**/TEST-*.xml");
    JUnitResultArchiver j = model.instantiate(args);
    assertEquals("**/TEST-*.xml", j.getTestResults());
    assertFalse(j.isAllowEmptyResults());
    assertFalse(j.isKeepLongStdio());
    assertEquals(1.0, j.getHealthScaleFactor(), 0);
    assertTrue(j.getTestDataPublishers().isEmpty());
    assertEquals(args, model.uninstantiate(model.instantiate(args)));

    // Test roundtripping from a Pipeline-style describing of the publisher.
    Map<String,Object> describedPublisher = new HashMap<String, Object>();
    describedPublisher.put("$class", "MockTestDataPublisher");
    describedPublisher.put("name", "test");
    args.put("testDataPublishers", Collections.singletonList(describedPublisher));

    Map<String,Object> described = model.uninstantiate(model.instantiate(args));
    JUnitResultArchiver j2 = model.instantiate(described);
    List<TestDataPublisher> testDataPublishers = j2.getTestDataPublishers();
    assertFalse(testDataPublishers.isEmpty());
    assertEquals(1, testDataPublishers.size());
    assertEquals(MockTestDataPublisher.class, testDataPublishers.get(0).getClass());
    assertEquals("test", ((MockTestDataPublisher)testDataPublishers.get(0)).getName());

    assertEquals(described, model.uninstantiate(model.instantiate(described)));
}
 
Example #8
Source File: ExportedPipelineFunction.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public ExportedPipelineFunction(DescribableModel<?> model, String functionName) {
    super(model);
    this.functionName = functionName;
}
 
Example #9
Source File: ExportedPipelineStep.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public ExportedPipelineStep(DescribableModel<? extends Step> model, String functionName,
                            StepDescriptor descriptor) {
    super(model, functionName);
    this.descriptor = descriptor;
}
 
Example #10
Source File: ExportedDescribableModel.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public ExportedDescribableModel(DescribableModel<?> model) {
    this(model, null);
}
 
Example #11
Source File: ExportedDescribableModel.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public ExportedDescribableModel(DescribableModel<?> model, String symbol) {
    this.model = model;
    this.symbol = symbol;
}