Skip to content

Commit

Permalink
PublishChecks step: The conclusion should be none when status is not …
Browse files Browse the repository at this point in the history
…completed (#82)

* Changed conclusion status based on ChecksStatus

* Refactoring the implementation

* Fix PMD issues.

* Update src/main/java/io/jenkins/plugins/checks/steps/PublishChecksStep.java

Co-authored-by: Kezhi Xiong <[email protected]>

* Refactor unit tests

Co-authored-by: Kezhi Xiong <[email protected]>
  • Loading branch information
ThusithaDJ and XiongKezhi authored Feb 18, 2021
1 parent fd8e148 commit 24dcafe
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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;
}
}

0 comments on commit 24dcafe

Please sign in to comment.