org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner Java Examples

The following examples show how to use org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner. 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: GithubBuildStatusGraphListenerTest.java    From github-autostatus-plugin with MIT License 6 votes vote down vote up
@Test
public void testStageNode() throws IOException {
    StepStartNode stageNode = mock(StepStartNode.class);
    StageAction stageAction = mock(StageAction.class);
    FlowExecution execution = mock(FlowExecution.class);
    when(stageNode.getAction(StageAction.class)).thenReturn(stageAction);
    when(stageNode.getExecution()).thenReturn(execution);
    FlowExecutionOwner owner = mock(FlowExecutionOwner.class);
    when(execution.getOwner()).thenReturn(owner);
    AbstractBuild build = mock(AbstractBuild.class);

    when(owner.getExecutable()).thenReturn(build);
    ExecutionModelAction executionModel = mock(ExecutionModelAction.class);
    when(build.getAction(ExecutionModelAction.class)).thenReturn(executionModel);

    ModelASTStages stages = new ModelASTStages(null);
    when(executionModel.getStages()).thenReturn(stages);

    GithubBuildStatusGraphListener instance = new GithubBuildStatusGraphListener();
    instance.onNewHead(stageNode);
    verify(build).addAction(any(BuildStatusAction.class));
}
 
Example #2
Source File: GithubBuildStatusGraphListenerTest.java    From github-autostatus-plugin with MIT License 6 votes vote down vote up
@Test
public void testAtomNode() throws IOException {
    ErrorAction error = mock(ErrorAction.class);
    CpsFlowExecution execution = mock(CpsFlowExecution.class);

    StepAtomNode stageNode = new StepAtomNode(execution, null, mock(FlowNode.class));
    stageNode.addAction(error);

    FlowExecutionOwner owner = mock(FlowExecutionOwner.class);
    when(execution.getOwner()).thenReturn(owner);

    Executable exec = mock(Executable.class);
    when(owner.getExecutable()).thenReturn(exec);

    AbstractBuild build = mock(AbstractBuild.class);
    when(owner.getExecutable()).thenReturn(build);
    when(build.getAction(ExecutionModelAction.class)).thenReturn(null); // not declarative

    BuildStatusAction buildStatusAction = mock(BuildStatusAction.class);
    when(build.getAction(BuildStatusAction.class)).thenReturn(buildStatusAction);

    GithubBuildStatusGraphListener instance = new GithubBuildStatusGraphListener();
    instance.onNewHead(stageNode);
    verify(buildStatusAction).sendNonStageError(any());
}
 
Example #3
Source File: GithubBuildStatusGraphListenerTest.java    From github-autostatus-plugin with MIT License 6 votes vote down vote up
/**
 * Verifies onNewHead adds the build action for an atom node if there's an error (used for sending errors that occur
 * out of a stage
 */
@Test
public void testAtomNodeAddsAction() throws IOException {
    ErrorAction error = mock(ErrorAction.class);
    CpsFlowExecution execution = mock(CpsFlowExecution.class);
    StepAtomNode stageNode = new StepAtomNode(execution, null, mock(FlowNode.class));
    stageNode.addAction(error);

    FlowExecutionOwner owner = mock(FlowExecutionOwner.class);
    when(execution.getOwner()).thenReturn(owner);
    AbstractBuild build = mock(AbstractBuild.class);
    when(owner.getExecutable()).thenReturn(build);
    when(build.getAction(ExecutionModelAction.class)).thenReturn(null); // not declarative

    GithubBuildStatusGraphListener instance = new GithubBuildStatusGraphListener();
    instance.onNewHead(stageNode);
    verify(build).addAction(any(BuildStatusAction.class));
}
 
Example #4
Source File: DefaultsBinder.java    From pipeline-multibranch-defaults-plugin with MIT License 6 votes vote down vote up
@Override
public FlowExecution create(FlowExecutionOwner handle, TaskListener listener, List<? extends Action> actions) throws Exception {
    Jenkins jenkins = Jenkins.getInstance();
    if (jenkins == null) {
        throw new IllegalStateException("inappropriate context");
    }
    Queue.Executable exec = handle.getExecutable();
    if (!(exec instanceof WorkflowRun)) {
        throw new IllegalStateException("inappropriate context");
    }

    ConfigFileStore store = GlobalConfigFiles.get();
    if (store != null) {
        Config config = store.getById(PipelineBranchDefaultsProjectFactory.SCRIPT);
        if (config != null) {
            return new CpsFlowDefinition(config.content, false).create(handle, listener, actions);
        }
    }
    throw new IllegalArgumentException("Default " + PipelineBranchDefaultsProjectFactory.SCRIPT + " not found. Check configuration.");
}
 
Example #5
Source File: DeclarativeDockerUtils.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
private static Run<?,?> currentRun() {
    try {
        CpsThread t = CpsThread.current();
        if (t != null) {
            CpsFlowExecution e = t.getExecution();
            if (e != null) {
                FlowExecutionOwner o = e.getOwner();
                if (o != null && o.getExecutable() instanceof Run) {
                    return (Run)o.getExecutable();
                }
            }
        }

        return null;
    } catch (IOException i) {
        return null;
    }
}
 
Example #6
Source File: GithubBuildStatusGraphListenerTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
public void testComplexPipeline() throws IOException {
    StepStartNode stageNode = mock(StepStartNode.class);
    StageAction stageAction = mock(StageAction.class);
    FlowExecution execution = mock(FlowExecution.class);
    when(stageNode.getAction(StageAction.class)).thenReturn(stageAction);
    when(stageNode.getExecution()).thenReturn(execution);
    FlowExecutionOwner owner = mock(FlowExecutionOwner.class);
    when(execution.getOwner()).thenReturn(owner);
    AbstractBuild build = mock(AbstractBuild.class);

    when(owner.getExecutable()).thenReturn(build);
    ExecutionModelAction executionModel = mock(ExecutionModelAction.class);
    when(build.getAction(ExecutionModelAction.class)).thenReturn(executionModel);

    // Construct a complex pipeline model
    ModelASTStages stages = createStages("Outer Stage 1", "Outer Stage 2");
    ModelASTStages innerStages = createStages("Inner Stage 1", "Inner Stage 2", "Inner Stage 3");
    ModelASTStages innerInnerStages = createStages("Inner Inner Stage 1");
    ModelASTStages parallelStages = createStages("Parallel Stage 1", "Parallel Stage 2");
    stages.getStages().get(0).setStages(innerStages);
    innerStages.getStages().get(2).setStages(innerInnerStages);
    stages.getStages().get(1).setParallelContent(parallelStages.getStages());
    // Create a linear list of the pipeline stages for comparison
    List<String> fullStageList = Arrays.asList(new String[]{"Outer Stage 1", "Inner Stage 1", "Inner Stage 2", "Inner Stage 3", "Inner Inner Stage 1", "Outer Stage 2", "Parallel Stage 1", "Parallel Stage 2"});

    when(executionModel.getStages()).thenReturn(stages);

    GithubBuildStatusGraphListener instance = new GithubBuildStatusGraphListener();
    instance.onNewHead(stageNode);
    verify(build).addAction(any(BuildStatusAction.class));
    // Check that the pipeline stages found match the list of expected stages
    assertTrue(GithubBuildStatusGraphListener.getDeclarativeStages(build).equals(fullStageList));
}
 
Example #7
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);
}
 
Example #8
Source File: GithubBuildStatusGraphListenerTest.java    From github-autostatus-plugin with MIT License 4 votes vote down vote up
@Test
public void testStepEndNode() throws Exception {
    long time = 12345L;

    // Mocked objects
    CpsFlowExecution execution = mock(CpsFlowExecution.class);
    StepStartNode stageStartNode = mock(StepStartNode.class);
    StepEndNode stageEndNode = new StepEndNode(execution, stageStartNode, mock(FlowNode.class));

    ErrorAction error = mock(ErrorAction.class);
    stageEndNode.addAction(error);

    TimingAction startTime = mock(TimingAction.class);
    TimingAction endTime = mock(TimingAction.class);
    stageEndNode.addAction(endTime);
    when(startTime.getStartTime()).thenReturn(0L);
    when(endTime.getStartTime()).thenReturn(time);

    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    FlowExecutionOwner owner = mock(FlowExecutionOwner.class);
    AbstractBuild build = mock(AbstractBuild.class);

    // get BuildStatusAction from StepEndNode
    when(execution.getOwner()).thenReturn(owner);
    when(owner.getExecutable()).thenReturn(build);
    when(build.getAction(BuildStatusAction.class)).thenReturn(buildStatus);

    // get StepStartNode from StepEndNode
    String startId = "15";
    when(stageStartNode.getId()).thenReturn(startId);
    when(execution.getNode(startId)).thenReturn(stageStartNode);

    // get time from StepStartNode to StepEndNode
    when(stageStartNode.getAction(TimingAction.class)).thenReturn(startTime);
    LabelAction labelAction = new LabelAction("some label");
    when(stageStartNode.getAction(LabelAction.class)).thenReturn(labelAction);
    when(stageStartNode.getAction(StageAction.class)).thenReturn(mock(StageAction.class));

    GithubBuildStatusGraphListener instance = new GithubBuildStatusGraphListener();
    instance.onNewHead(stageEndNode);
    verify(buildStatus).updateBuildStatusForStage(eq("some label"), eq(BuildStage.State.CompletedError), eq(time));
}