diff --git a/src/main/java/io/jenkins/plugins/checks/steps/PublishChecksStep.java b/src/main/java/io/jenkins/plugins/checks/steps/PublishChecksStep.java index 22724010..82d4f479 100644 --- a/src/main/java/io/jenkins/plugins/checks/steps/PublishChecksStep.java +++ b/src/main/java/io/jenkins/plugins/checks/steps/PublishChecksStep.java @@ -65,9 +65,20 @@ public void setDetailsURL(final String detailsURL) { this.detailsURL = detailsURL; } + /** + * Change the status of the check. + * When the {@code status} is {@link ChecksStatus#QUEUED} or {@link ChecksStatus#IN_PROGRESS}, + * the conclusion will be reset to {@link ChecksConclusion#NONE} + * + * @param status + * the status to be set + */ @DataBoundSetter public void setStatus(final ChecksStatus status) { this.status = status; + if (status == ChecksStatus.QUEUED || status == ChecksStatus.IN_PROGRESS) { + this.conclusion = ChecksConclusion.NONE; + } } @DataBoundSetter diff --git a/src/test/java/io/jenkins/plugins/checks/steps/PublishChecksStepTest.java b/src/test/java/io/jenkins/plugins/checks/steps/PublishChecksStepTest.java index c23381a1..78d5ad89 100644 --- a/src/test/java/io/jenkins/plugins/checks/steps/PublishChecksStepTest.java +++ b/src/test/java/io/jenkins/plugins/checks/steps/PublishChecksStepTest.java @@ -9,23 +9,28 @@ import org.apache.commons.lang3.StringUtils; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; import static io.jenkins.plugins.checks.assertions.Assertions.assertThat; import static org.mockito.Mockito.*; class PublishChecksStepTest { - @Test - void shouldPublishCheckWithDefaultValues() throws IOException, InterruptedException { - StepContext context = mock(StepContext.class); + StepContext getStepContext() throws IOException, InterruptedException { + StepContext context = mock(StepContext.class); when(context.get(Run.class)).thenReturn(mock(Run.class)); when(context.get(TaskListener.class)).thenReturn(TaskListener.NULL); + return context; + } - StepExecution execution = new PublishChecksStep().start(context); + @Test + void shouldPublishCheckWithDefaultValues() throws IOException, InterruptedException { + StepExecution execution = new PublishChecksStep().start(getStepContext()); assertThat(execution).isInstanceOf(PublishChecksStep.PublishChecksStepExecution.class); assertThat(((PublishChecksStep.PublishChecksStepExecution)execution).extractChecksDetails()) .usingRecursiveComparison() @@ -43,21 +48,55 @@ void shouldPublishCheckWithDefaultValues() throws IOException, InterruptedExcept } @Test - void shouldPublishCheckWithSetValues() throws IOException, InterruptedException { - PublishChecksStep step = new PublishChecksStep(); - step.setName("Jenkins"); - step.setSummary("a check made by Jenkins"); - step.setTitle("Jenkins Build"); - step.setText("a failed build"); - step.setStatus(ChecksStatus.IN_PROGRESS); - step.setConclusion(ChecksConclusion.FAILURE); - step.setDetailsURL("http://ci.jenkins.io"); + void shouldPublishCheckWithStatusInProgress() throws IOException, InterruptedException { + PublishChecksStep step = getModifiedPublishChecksStepObject("an in progress build", + ChecksStatus.IN_PROGRESS, null); - StepContext context = mock(StepContext.class); - when(context.get(Run.class)).thenReturn(mock(Run.class)); - when(context.get(TaskListener.class)).thenReturn(TaskListener.NULL); + StepExecution execution = step.start(getStepContext()); + assertThat(execution).isInstanceOf(PublishChecksStep.PublishChecksStepExecution.class); + assertThat(((PublishChecksStep.PublishChecksStepExecution)execution).extractChecksDetails()) + .usingRecursiveComparison() + .isEqualTo(new ChecksDetails.ChecksDetailsBuilder() + .withName("Jenkins") + .withStatus(ChecksStatus.IN_PROGRESS) + .withConclusion(ChecksConclusion.NONE) + .withDetailsURL("http://ci.jenkins.io") + .withOutput(new ChecksOutput.ChecksOutputBuilder() + .withTitle("Jenkins Build") + .withSummary("a check made by Jenkins") + .withText("an in progress build") + .build()) + .build()); + } + + @Test + void shouldPublishCheckWithStatusQueue() throws IOException, InterruptedException { + PublishChecksStep step = getModifiedPublishChecksStepObject("a queued build", + ChecksStatus.QUEUED, null); - StepExecution execution = step.start(context); + StepExecution execution = step.start(getStepContext()); + assertThat(execution).isInstanceOf(PublishChecksStep.PublishChecksStepExecution.class); + assertThat(((PublishChecksStep.PublishChecksStepExecution)execution).extractChecksDetails()) + .usingRecursiveComparison() + .isEqualTo(new ChecksDetails.ChecksDetailsBuilder() + .withName("Jenkins") + .withStatus(ChecksStatus.QUEUED) + .withConclusion(ChecksConclusion.NONE) + .withDetailsURL("http://ci.jenkins.io") + .withOutput(new ChecksOutput.ChecksOutputBuilder() + .withTitle("Jenkins Build") + .withSummary("a check made by Jenkins") + .withText("a queued build") + .build()) + .build()); + } + + @Test + void shouldPublishCheckWithSetValues() throws IOException, InterruptedException { + PublishChecksStep step = getModifiedPublishChecksStepObject("a failed build", + ChecksStatus.IN_PROGRESS, ChecksConclusion.FAILURE); + + StepExecution execution = step.start(getStepContext()); assertThat(execution).isInstanceOf(PublishChecksStep.PublishChecksStepExecution.class); assertThat(((PublishChecksStep.PublishChecksStepExecution)execution).extractChecksDetails()) .usingRecursiveComparison() @@ -81,4 +120,22 @@ void shouldDefinePublishChecksStepDescriptorCorrectly() { assertThat(descriptor.getDisplayName()).isEqualTo("Publish customized checks to SCM platforms"); assertThat(descriptor.getRequiredContext().toArray()).containsExactlyInAnyOrder(Run.class, TaskListener.class); } + + private PublishChecksStep getModifiedPublishChecksStepObject(final String stepText, final ChecksStatus status, + final ChecksConclusion conclusion) { + PublishChecksStep step = new PublishChecksStep(); + step.setName("Jenkins"); + step.setSummary("a check made by Jenkins"); + step.setTitle("Jenkins Build"); + step.setText(stepText); + if (Objects.nonNull(status)) { + step.setStatus(status); + } + if (Objects.nonNull(conclusion)) { + step.setConclusion(conclusion); + } + step.setDetailsURL("http://ci.jenkins.io"); + + return step; + } }