Search in sources :

Example 1 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class PipelineEventListener method newMessage.

private Message newMessage(PipelineEventChannel.Event event, FlowNode flowNode, Collection<String> branch) {
    Message message = newMessage(event, flowNode.getExecution());
    message.set(PipelineEventChannel.EventProps.pipeline_step_flownode_id, flowNode.getId());
    message.set(PipelineEventChannel.EventProps.pipeline_context, toPath(branch));
    if (currentStageName != null) {
        message.set(PipelineEventChannel.EventProps.pipeline_step_stage_name, currentStageName.get(flowNode.getExecution()));
        message.set(PipelineEventChannel.EventProps.pipeline_step_stage_id, currentStageId.get(flowNode.getExecution()));
    }
    if (flowNode instanceof StepNode) {
        StepNode stepNode = (StepNode) flowNode;
        StepDescriptor stepDescriptor = stepNode.getDescriptor();
        if (stepDescriptor != null) {
            message.set(PipelineEventChannel.EventProps.pipeline_step_name, stepDescriptor.getFunctionName());
            // TODO: Better event choice, more granularity - like only firing when this results in a status change
            if (stepDescriptor instanceof ExecutorStep.DescriptorImpl) {
                Run<?, ?> run = runFor(flowNode.getExecution());
                if (run != null) {
                    publishJobEvent(run, Events.JobChannel.job_run_started);
                }
                if (flowNode.getPersistentAction(QueueItemAction.class) != null) {
                    // Needed because this is expected everywhere apparently.
                    message.set(PipelineEventChannel.EventProps.pipeline_step_is_paused, String.valueOf(false));
                }
            }
        }
    }
    if (flowNode instanceof StepAtomNode) {
        Run<?, ?> run = runFor(flowNode.getExecution());
        if (run != null) {
            boolean pausedForInputStep = PipelineNodeUtil.isPausedForInputStep((StepAtomNode) flowNode, run.getAction(InputAction.class));
            if (pausedForInputStep) {
                // Fire job event to tell we are paused
                // We will publish on the job channel
                publishJobEvent(run, Events.JobChannel.job_run_paused);
            }
            message.set(PipelineEventChannel.EventProps.pipeline_step_is_paused, String.valueOf(pausedForInputStep));
        }
    }
    return message;
}
Also used : StepNode(org.jenkinsci.plugins.workflow.graph.StepNode) SimpleMessage(org.jenkinsci.plugins.pubsub.SimpleMessage) RunMessage(org.jenkinsci.plugins.pubsub.RunMessage) Message(org.jenkinsci.plugins.pubsub.Message) QueueItemAction(org.jenkinsci.plugins.workflow.actions.QueueItemAction) InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) StepAtomNode(org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode)

Example 2 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class SseEventTest method jobRunEvents.

@Test
public void jobRunEvents() throws IOException, ExecutionException, InterruptedException {
    final OneShotEvent success = new OneShotEvent();
    final FreeStyleProject p = j.createFreeStyleProject("test1");
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/test1/", message.get(blueocean_job_rest_url));
            assertionHelper.isEquals(p.getName(), message.get(blueocean_job_pipeline_name));
            if (message.get(jenkins_event).equals(Events.JobChannel.job_run_queue_left.name())) {
                assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
            }
            assertionHelper.isEquals(p.getName(), message.get(job_name));
            assertionHelper.isEquals("job", message.get(jenkins_channel));
            assertionHelper.isEquals("jenkins", message.get(jenkins_org));
            assertionHelper.isNull(message.get(job_ismultibranch));
            assertionHelper.isNull(message.get(job_multibranch_indexing_result));
            assertionHelper.isNull(message.get(job_multibranch_indexing_status));
            assertionHelper.isNotNull(message.get(Job.job_run_queueId));
            assertionHelper.isNotNull(message.get(Job.job_run_status));
            if ("SUCCESS".equals(message.get(Job.job_run_status)) && "job_run_ended".equals(message.get(jenkins_event)))
                success.signal();
        }
    });
    con.subscribe("job");
    p.scheduleBuild2(0).get();
    // make sure we got the event we were looking for
    success.block(5000);
    con.close();
    if (assertionHelper.totalErrors() > 0) {
        fail("There were errors: " + assertionHelper.totalErrors());
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) FreeStyleProject(hudson.model.FreeStyleProject) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Example 3 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class SseEventTest method multiBranchJobEventsWithCustomOrg.

@Test
public void multiBranchJobEventsWithCustomOrg() throws Exception {
    MockFolder folder = j.createFolder("TestOrgFolderName");
    assertNotNull(folder);
    setupScm();
    final OneShotEvent success = new OneShotEvent();
    final Boolean[] pipelineNameMatched = { null };
    final Boolean[] mbpPipelineNameMatched = { null };
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            if ("job".equals(message.get(jenkins_channel))) {
                if ("org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject".equals(message.get(jenkins_object_type))) {
                    if ("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        mbpPipelineNameMatched[0] = true;
                    } else {
                        mbpPipelineNameMatched[0] = false;
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                    if ("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        pipelineNameMatched[0] = true;
                    } else {
                        pipelineNameMatched[0] = false;
                    }
                }
            }
            if (pipelineNameMatched[0] != null && mbpPipelineNameMatched[0] != null) {
                success.signal();
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");
    final WorkflowMultiBranchProject mp = folder.createProject(WorkflowMultiBranchProject.class, "pipeline1");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    WorkflowJob p = mp.getItem("master");
    if (p == null) {
        mp.getIndexing().writeWholeLogTo(System.out);
        fail("master project not found");
    }
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(2, mp.getItems().size());
    success.block(5000);
    con.close();
    if (success.isSignaled()) {
        assertTrue(pipelineNameMatched[0]);
        assertTrue(mbpPipelineNameMatched[0]);
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) MockFolder(org.jvnet.hudson.test.MockFolder) BranchSource(jenkins.branch.BranchSource) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Example 4 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class SseEventTest method jobCreationEvents.

@Test
public void jobCreationEvents() throws IOException, ExecutionException, InterruptedException {
    final OneShotEvent success = new OneShotEvent();
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            assertionHelper.isEquals("job_crud_created", message.get(jenkins_event));
            assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/test1/", message.get(blueocean_job_rest_url));
            assertionHelper.isEquals("test1", message.get(blueocean_job_pipeline_name));
            assertionHelper.isEquals("test1", message.get(job_name));
            assertionHelper.isEquals("job", message.get(jenkins_channel));
            assertionHelper.isNull(message.get(job_ismultibranch));
            assertionHelper.isNull(message.get(job_multibranch_indexing_result));
            assertionHelper.isNull(message.get(job_multibranch_indexing_status));
            assertionHelper.isNull(message.get(Job.job_run_queueId));
            assertionHelper.isNull(message.get(Job.job_run_status));
            if ("job_crud_created".equals(message.get(jenkins_event)))
                success.signal();
        }
    });
    con.subscribe("job");
    j.createFreeStyleProject("test1");
    // make sure we got the event we were looking for
    success.block(5000);
    con.close();
    if (assertionHelper.totalErrors() > 0) {
        fail("There were errors: " + assertionHelper.totalErrors());
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Example 5 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class SseEventTest method multiBranchJobEvents.

@Test
public void multiBranchJobEvents() throws Exception {
    setupScm();
    final OneShotEvent success = new OneShotEvent();
    // index: 0 QUEUED, index 1: ALLOCATED, index: 2 INDEXING, index: 3 COMPLETED
    final boolean[] mbpStatus = { false, false, false, false };
    // index: 0 created, index: 1 QUEUED, index 2: ALLOCATED, index: 3 RUNNING, index: 4 SUCCESS
    final boolean[] masterBranchStatus = { false, false, false, false, false };
    final boolean[] feature1BranchStatus = { false, false, false, false, false };
    final boolean[] masterBranchPipelineEvent = { false };
    final boolean[] feature1BranchPipelineEvent = { false };
    final boolean[] masterBranchNodeBlockEvent = { false };
    final boolean[] feature1BranchNodeBlockEvent = { false };
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            if ("job".equals(message.get(jenkins_channel))) {
                if ("org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject".equals(message.get(jenkins_object_type))) {
                    assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                    // assertEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                    assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                    assertionHelper.isEquals("job", message.get(jenkins_channel));
                    assertionHelper.isEquals("jenkins", message.get(jenkins_org));
                    assertionHelper.isEquals("true", message.get(job_ismultibranch));
                    assertionHelper.isEquals("pipeline1", message.get(job_name));
                    assertionHelper.isNull(message.get(job_orgfolder_indexing_status));
                    assertionHelper.isNull(message.get(job_orgfolder_indexing_result));
                    if ("QUEUED".equals(message.get(job_run_status))) {
                        // queued
                        mbpStatus[0] = true;
                    }
                    if ("ALLOCATED".equals(message.get(job_run_status))) {
                        // allocated or left queue
                        mbpStatus[1] = true;
                    }
                    if ("INDEXING".equals(message.get(job_multibranch_indexing_status))) {
                        // indexing started
                        mbpStatus[2] = true;
                        assertionHelper.isNotNull(message.get(job_run_queueId));
                    }
                    if ("SUCCESS".equals(message.get(job_multibranch_indexing_result)) || ("COMPLETED".equals(message.get(job_multibranch_indexing_status)) && "job_run_queue_task_complete".equals(message.get(jenkins_event)))) {
                        // indexing completed
                        mbpStatus[3] = true;
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                    assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                    if ("pipeline1/master".equals(message.get(job_name))) {
                        System.out.println("job_run_status::::: " + message.get(job_run_status));
                        assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/master/", message.get(blueocean_job_rest_url));
                        assertionHelper.isEquals("master", message.get(blueocean_job_branch_name));
                        if ("job_crud_created".equals(message.get(jenkins_event))) {
                            masterBranchStatus[0] = true;
                        }
                        if ("QUEUED".equals(message.get(job_run_status))) {
                            masterBranchStatus[1] = true;
                        } else if ("ALLOCATED".equals(message.get(job_run_status))) {
                            masterBranchStatus[2] = true;
                        } else if ("RUNNING".equals(message.get(job_run_status))) {
                            System.out.println("in master running.....");
                            assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                            masterBranchStatus[3] = true;
                        }
                        if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
                            masterBranchStatus[4] = true;
                        }
                    } else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
                        assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/feature%252Fux-1/", message.get(blueocean_job_rest_url));
                        assertionHelper.isEquals("feature%2Fux-1", message.get(blueocean_job_branch_name));
                        if ("job_crud_created".equals(message.get(jenkins_event))) {
                            feature1BranchStatus[0] = true;
                        }
                        System.out.println("job_run_status::::: " + message.get(job_run_status));
                        if ("QUEUED".equals(message.get(job_run_status))) {
                            feature1BranchStatus[1] = true;
                        } else if ("ALLOCATED".equals(message.get(job_run_status))) {
                            feature1BranchStatus[2] = true;
                        }
                        if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
                            feature1BranchStatus[4] = true;
                        }
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowRun".equals(message.get(jenkins_object_type))) {
                    if ("pipeline1/master".equals(message.get(job_name))) {
                        if ("RUNNING".equals(message.get(job_run_status))) {
                            assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                            masterBranchStatus[3] = true;
                        }
                    } else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
                        if ("RUNNING".equals(message.get(job_run_status))) {
                            assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                            feature1BranchStatus[3] = true;
                        }
                    }
                }
                // all completed
                if (mbpStatus[3] && masterBranchStatus[4] && feature1BranchStatus[4]) {
                    success.signal();
                }
            } else if ("pipeline".equals(message.get(jenkins_channel))) {
                if ("pipeline1/master".equals(message.get(pipeline_job_name))) {
                    assertionHelper.isEquals("1", message.get(pipeline_run_id));
                    masterBranchPipelineEvent[0] = true;
                    if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
                        masterBranchNodeBlockEvent[0] = true;
                    }
                } else if ("pipeline1/feature%2Fux-1".equals(message.get(pipeline_job_name))) {
                    feature1BranchPipelineEvent[0] = true;
                    assertionHelper.isEquals("1", message.get(pipeline_run_id));
                    if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
                        feature1BranchNodeBlockEvent[0] = true;
                    }
                }
                if ("pipeline_stage".equals(message.get(jenkins_event))) {
                    assertionHelper.isNotNull(message.get(pipeline_step_stage_name));
                    assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
                    assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
                } else if ("pipeline_step".equals(message.get(jenkins_event))) {
                    assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
                    assertionHelper.isEquals("false", message.get(pipeline_step_is_paused));
                }
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");
    final WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "pipeline1");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    WorkflowJob p = mp.getItem("master");
    if (p == null) {
        mp.getIndexing().writeWholeLogTo(System.out);
        fail("master project not found");
    }
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(2, mp.getItems().size());
    success.block(5000);
    con.close();
    if (success.isSignaled()) {
        assertTrue(masterBranchPipelineEvent[0]);
        assertTrue(feature1BranchPipelineEvent[0]);
        assertTrue(masterBranchNodeBlockEvent[0]);
        assertTrue(feature1BranchNodeBlockEvent[0]);
        assertArrayEquals(mbpStatus, new boolean[] { true, true, true, true });
        assertArrayEquals(masterBranchStatus, new boolean[] { true, true, true, true, true });
        assertArrayEquals(feature1BranchStatus, new boolean[] { true, true, true, true, true });
    }
    if (assertionHelper.totalErrors() > 0) {
        fail("There were errors: " + assertionHelper.totalErrors());
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Aggregations

Message (org.jenkinsci.plugins.pubsub.Message)6 OneShotEvent (hudson.util.OneShotEvent)5 SSEConnection (io.jenkins.blueocean.events.sse.SSEConnection)5 ChannelSubscriber (org.jenkinsci.plugins.pubsub.ChannelSubscriber)5 Test (org.junit.Test)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)3 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)3 BranchSource (jenkins.branch.BranchSource)2 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)2 GitSCMSource (jenkins.plugins.git.GitSCMSource)2 SCMSource (jenkins.scm.api.SCMSource)2 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)2 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)2 FreeStyleProject (hudson.model.FreeStyleProject)1 RunMessage (org.jenkinsci.plugins.pubsub.RunMessage)1 SimpleMessage (org.jenkinsci.plugins.pubsub.SimpleMessage)1 QueueItemAction (org.jenkinsci.plugins.workflow.actions.QueueItemAction)1 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 StepAtomNode (org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode)1