Skip to content

Commit

Permalink
Handle abort differently from failures (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus authored Dec 24, 2020
1 parent 460f8e1 commit bb070c6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<version>${pipeline-stage-step.version}</version>
<scope>test</scope>
</dependency>
<!-- steps -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<scope>test></scope>
</dependency>
</dependencies>

<build>
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/io/jenkins/plugins/checks/steps/WithChecksStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.model.TaskListener;
import io.jenkins.plugins.checks.api.*;
import io.jenkins.plugins.util.PluginLogger;
import jenkins.model.CauseOfInterruption;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -16,6 +17,7 @@
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static hudson.Util.fixNull;

Expand Down Expand Up @@ -174,13 +176,29 @@ public void onSuccess(final StepContext context, final Object result) {

@Override
public void onFailure(final StepContext context, final Throwable t) {
publish(context, new ChecksDetails.ChecksDetailsBuilder()
ChecksDetails.ChecksDetailsBuilder builder = new ChecksDetails.ChecksDetailsBuilder()
.withName(info.getName())
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(ChecksConclusion.FAILURE)
.withOutput(new ChecksOutput.ChecksOutputBuilder()
.withSummary("occurred while executing withChecks step.")
.withText(t.toString()).build()));
.withStatus(ChecksStatus.COMPLETED);
ChecksOutput.ChecksOutputBuilder outputBuilder = new ChecksOutput.ChecksOutputBuilder()
.withSummary("occurred while executing withChecks step.");
if (t instanceof FlowInterruptedException) {
FlowInterruptedException fi = (FlowInterruptedException) t;
String summary = fi.getCauses()
.stream()
.map(CauseOfInterruption::getShortDescription)
.collect(Collectors.joining("\n\n"));
builder.withConclusion(ChecksConclusion.CANCELED)
.withOutput(outputBuilder
.withText(summary)
.build());

}
else {
builder.withConclusion(ChecksConclusion.FAILURE)
.withOutput(outputBuilder
.withText(t.toString()).build());
}
publish(context, builder);
context.onFailure(t);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.jenkins.plugins.checks.steps;

import hudson.model.Result;
import hudson.model.Run;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksStatus;
import io.jenkins.plugins.checks.util.CapturingChecksPublisher;
import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerTest;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.*;
import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution;
import org.junit.After;
import org.junit.Test;
import org.jvnet.hudson.test.TestExtension;
Expand Down Expand Up @@ -97,6 +100,50 @@ public void publishChecksShouldTakeNameFromWithChecksUnlessOverridden() {
assertThat(manualChecks.getConclusion()).isEqualTo(ChecksConclusion.SUCCESS);
}

/**
* Test that withChecks correctly reports failures.
*/
@Test
public void withChecksShouldDetectFailure() {
WorkflowJob job = createPipeline();
job.setDefinition(asStage("withChecks('test injection') { error 'oh no!' }"));

buildWithResult(job, Result.FAILURE);

assertThat(PUBLISHER_FACTORY.getPublishedChecks().size()).isEqualTo(2);
ChecksDetails failure = PUBLISHER_FACTORY.getPublishedChecks().get(1);

assertThat(failure.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(failure.getConclusion()).isEqualTo(ChecksConclusion.FAILURE);
assertThat(failure.getOutput()).isPresent();

assertThat(failure.getOutput().get().getText()).isPresent();
assertThat(failure.getOutput().get().getText().get()).contains("oh no!");
}

/**
* Test that withChecks correctly reports aborts.
*/
@Test
public void withChecksShouldDetectAbort() {
WorkflowJob job = createPipeline();
// Simulate a job cancellation.
job.setDefinition(new CpsFlowDefinition("withChecks('test injection') { throw new org.jenkinsci.plugins.workflow.steps.FlowInterruptedException("
+ "hudson.model.Result.ABORTED, new org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution.RemovedNodeCause()) }", false));

buildWithResult(job, Result.ABORTED);

assertThat(PUBLISHER_FACTORY.getPublishedChecks().size()).isEqualTo(2);
ChecksDetails abort = PUBLISHER_FACTORY.getPublishedChecks().get(1);

assertThat(abort.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(abort.getConclusion()).isEqualTo(ChecksConclusion.CANCELED);
assertThat(abort.getOutput()).isPresent();

assertThat(abort.getOutput().get().getText()).isPresent();
assertThat(abort.getOutput().get().getText().get()).isEqualTo(new ExecutorStepExecution.RemovedNodeCause().getShortDescription());
}

/**
* Assert that the injected {@link ChecksInfo} is as expected.
*/
Expand Down

0 comments on commit bb070c6

Please sign in to comment.