Java Code Examples for org.jenkinsci.plugins.workflow.job.WorkflowRun#getParent()

The following examples show how to use org.jenkinsci.plugins.workflow.job.WorkflowRun#getParent() . 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: PipelineNodeContainerImpl.java    From blueocean-plugin with MIT License 6 votes vote down vote up
public PipelineNodeContainerImpl(WorkflowRun run, Link parentLink) {
    this.run = run;
    this.self = parentLink.rel("nodes");

    WorkflowJob job = run.getParent();
    NodeGraphBuilder graphBuilder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(run);

    //If build either failed or is in progress then return union with last successful pipeline run
    if (run.getResult() != Result.SUCCESS
        && job.getLastSuccessfulBuild() != null
        && Integer.parseInt(job.getLastSuccessfulBuild().getId()) < Integer.parseInt(run.getId())) {

        NodeGraphBuilder pastBuildGraph = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(job.getLastSuccessfulBuild());
        this.nodes = graphBuilder.union(pastBuildGraph.getPipelineNodes(), getLink());
    } else {
        this.nodes = graphBuilder.getPipelineNodes(getLink());
    }
    for (BluePipelineNode node : nodes) {
        nodeMap.put(node.getId(), node);
    }
}
 
Example 2
Source File: YamlFlowDefinition.java    From simple-pull-request-job-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public FlowExecution create(FlowExecutionOwner owner, TaskListener listener,
                            List<? extends Action> actions) throws Exception {
    Queue.Executable exec = owner.getExecutable();
    if (!(exec instanceof WorkflowRun)) {
        throw new IllegalStateException("inappropriate context");
    }

    WorkflowRun build = (WorkflowRun) exec;
    WorkflowJob job = build.getParent();
    BranchJobProperty property = job.getProperty(BranchJobProperty.class);

    Branch branch = property.getBranch();
    ItemGroup<?> parent = job.getParent();

    if (!(parent instanceof WorkflowMultiBranchProject)) {
        throw new IllegalStateException("inappropriate context");
    }

    SCMSource scmSource = ((WorkflowMultiBranchProject) parent).getSCMSource(branch.getSourceId());

    if (scmSource == null) {
        throw new IllegalStateException(branch.getSourceId() + " not found");
    }

    GitConfig gitConfig = new GitConfig();

    SCMHead head = branch.getHead();

    if ("Pull Request".equals(head.getPronoun())) {
        ChangeRequestSCMHead2 changeRequestSCMHead2 = (ChangeRequestSCMHead2) branch.getHead();
        head = changeRequestSCMHead2.getTarget();
    }

    SCMRevision tip = scmSource.fetch(head, listener);

    if (tip == null) {
        throw new IllegalStateException("Cannot determine the revision.");
    }

    SCMRevision rev = scmSource.getTrustedRevision(tip, listener);
    GitSCM gitSCM = (GitSCM) scmSource.build(head, rev);

    gitConfig.setGitUrl(gitSCM.getUserRemoteConfigs().get(0).getUrl());
    gitConfig.setCredentialsId(gitSCM.getUserRemoteConfigs().get(0).getCredentialsId());
    gitConfig.setGitBranch(head.getName());

    String script;
    try (SCMFileSystem fs = SCMFileSystem.of(scmSource, head, rev)) {
        if (fs != null) {
            InputStream yamlInputStream = fs.child(scriptPath).content();
            listener.getLogger().println("Path of yaml/yml config file: " + fs.child(scriptPath).getPath());
            YamlToPipeline y = new YamlToPipeline();
            script = y.generatePipeline(yamlInputStream, gitConfig, listener);
        } else {
            throw new IOException("SCM not supported");
            // FIXME implement full checkout
        }
    }

    listener.getLogger().println(script);
    return new CpsFlowExecution(script, false, owner);
}