Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks API integration #180

Merged
merged 12 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@
<artifactId>echarts-api</artifactId>
<version>4.8.0-2</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>checks-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>github-checks</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<!-- TODO remove after https://github.com/jenkinsci/bom/pull/308 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>branch-api</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>bootstrap4-api</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/hudson/tasks/junit/JUnitResultArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.tasks.junit.TestResultAction.Data;
import io.jenkins.plugins.junit.checks.JUnitChecksPublisher;
import io.jenkins.plugins.junit.storage.FileJunitTestResultStorage;
import io.jenkins.plugins.junit.storage.JunitTestResultStorage;
import hudson.tasks.test.PipelineTestDetails;
Expand Down Expand Up @@ -91,6 +92,7 @@ public class JUnitResultArchiver extends Recorder implements SimpleBuildStep, JU
* If true, don't throw exception on missing test results or no files found.
*/
private boolean allowEmptyResults;
private boolean skipPublishingChecks;

@DataBoundConstructor
public JUnitResultArchiver(String testResults) {
Expand Down Expand Up @@ -278,6 +280,10 @@ public static TestResultSummary parseAndSummarize(@Nonnull JUnitTask task, Pipel
build.addAction(action);
}

if (!task.isSkipPublishingChecks()) {
new JUnitChecksPublisher(action, summary).publishChecks(listener);
}

return summary;
}
}
Expand Down Expand Up @@ -355,6 +361,21 @@ public boolean isAllowEmptyResults() {
return allowEmptyResults;
}

/**
* Should we skip publishing checks to the checks API plugin.
*
* @return if publishing checks should be skipped, {@code false} otherwise
*/
@Override
public boolean isSkipPublishingChecks() {
return skipPublishingChecks;
}

@DataBoundSetter
public void setSkipPublishingChecks(boolean skipPublishingChecks) {
this.skipPublishingChecks = skipPublishingChecks;
}

@DataBoundSetter public final void setAllowEmptyResults(boolean allowEmptyResults) {
this.allowEmptyResults = allowEmptyResults;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/tasks/junit/JUnitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface JUnitTask {
boolean isKeepLongStdio();

boolean isAllowEmptyResults();

boolean isSkipPublishingChecks();
}
16 changes: 16 additions & 0 deletions src/main/java/hudson/tasks/junit/pipeline/JUnitResultsStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class JUnitResultsStep extends Step implements JUnitTask {
* If true, don't throw exception on missing test results or no files found.
*/
private boolean allowEmptyResults;
private boolean skipPublishingChecks;

@DataBoundConstructor
public JUnitResultsStep(String testResults) {
Expand Down Expand Up @@ -115,6 +116,21 @@ public boolean isAllowEmptyResults() {
return allowEmptyResults;
}

/**
* Should we skip publishing checks to the checks API plugin.
*
* @return if publishing checks should be skipped, {@code false} otherwise
*/
@Override
public boolean isSkipPublishingChecks() {
return skipPublishingChecks;
}

@DataBoundSetter
public void setSkipPublishingChecks(boolean skipPublishingChecks) {
this.skipPublishingChecks = skipPublishingChecks;
}

@DataBoundSetter public final void setAllowEmptyResults(boolean allowEmptyResults) {
this.allowEmptyResults = allowEmptyResults;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package io.jenkins.plugins.junit.checks;

import edu.hm.hafner.util.VisibleForTesting;
import hudson.model.TaskListener;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.junit.TestResultSummary;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksOutput;
import io.jenkins.plugins.checks.api.ChecksPublisher;
import io.jenkins.plugins.checks.api.ChecksPublisherFactory;
import io.jenkins.plugins.checks.api.ChecksStatus;
import io.jenkins.plugins.util.JenkinsFacade;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public class JUnitChecksPublisher {
public static final String SEPARATOR = ", ";
private final TestResultAction action;
private final JenkinsFacade jenkinsFacade;
private final TestResultSummary summary;

public JUnitChecksPublisher(final TestResultAction action, final TestResultSummary summary) {
this(action, summary, new JenkinsFacade());
}

@VisibleForTesting
JUnitChecksPublisher(final TestResultAction action, final TestResultSummary summary, final JenkinsFacade jenkinsFacade) {
this.jenkinsFacade = jenkinsFacade;
this.action = action;
this.summary = summary;
}

public void publishChecks(TaskListener listener) {
ChecksPublisher publisher = ChecksPublisherFactory.fromRun(action.run, listener);
publisher.publish(extractChecksDetails());
}

@VisibleForTesting
ChecksDetails extractChecksDetails() {
String text = extractChecksText();
System.out.println(text);
ChecksOutput output = new ChecksOutput.ChecksOutputBuilder()
.withTitle(extractChecksTitle())
.withSummary("<sub>Send us [feedback](https://github.com/jenkinsci/junit-plugin/issues)")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about maybe having a Feedback <- survey, and a bug report link, thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever works best for you. Just GitHub issues might be enough for developers, but a survey is better for common users

.withText(text)
.build();

return new ChecksDetails.ChecksDetailsBuilder()
.withName("Tests")
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(summary.getFailCount() > 0 ? ChecksConclusion.FAILURE : ChecksConclusion.SUCCESS)
.withDetailsURL(jenkinsFacade.getAbsoluteUrl(action.run.getUrl(), action.getUrlName()))
.withOutput(output)
.build();
}

private String extractChecksText() {
StringBuilder builder = new StringBuilder();
if (summary.getFailCount() > 0) {
List<CaseResult> failedTests = action.getResult().getFailedTests();

if (failedTests.size() > 10) {
failedTests = failedTests.subList(0, 9);
}

failedTests.forEach(failedTest -> mapFailedTestToTestReport(builder, failedTest));
if (summary.getFailCount() > 10) {
builder.append("\n")
.append(summary.getFailCount() - 10).append(" more test results are not shown here, view them on Jenkins");
timja marked this conversation as resolved.
Show resolved Hide resolved
}
}

return builder.toString();
}

private void mapFailedTestToTestReport(StringBuilder builder, CaseResult failedTest) {
builder.append("## <code>").append(failedTest.getTransformedFullDisplayName().trim()).append("</code>")
.append("\n");

if (StringUtils.isNotBlank(failedTest.getErrorDetails())) {
builder.append("<pre><code>").append(failedTest.getErrorDetails().trim()).append("</code></pre>")
timja marked this conversation as resolved.
Show resolved Hide resolved
.append("\n");
}
if (StringUtils.isNotBlank(failedTest.getErrorStackTrace())) {
builder.append("<details><summary>Stack trace</summary>").append("\n")
.append("<pre><code>").append(failedTest.getErrorStackTrace().trim()).append("</code></pre>")
.append("</details>")
.append("\n");
}

if (StringUtils.isNotBlank(failedTest.getStderr())) {
builder.append("<details><summary>Standard error</summary>").append("\n")
.append("<pre><code>").append(failedTest.getStderr().trim()).append("</code></pre>")
.append("</details>")
.append("\n");
}

if (StringUtils.isNotBlank(failedTest.getStdout())) {
builder.append("<details><summary>Standard out</summary>").append("\n")
.append("<pre><code>").append(failedTest.getStdout().trim()).append("</code></pre>")
.append("</details>")
.append("\n");
}
builder.append("\n");
}

private String extractChecksTitle() {
StringBuilder builder = new StringBuilder();

if (summary.getFailCount() == 1) {
CaseResult failedTest = action.getResult().getFailedTests().get(0);
builder.append(failedTest.getTransformedFullDisplayName()).append(" failed");
return builder.toString();
}

if (summary.getFailCount() > 0) {
builder.append("failed: ").append(summary.getFailCount());
if (summary.getSkipCount() > 0 || summary.getPassCount() > 0) {
builder.append(SEPARATOR);
}
}

if (summary.getSkipCount() > 0) {
builder.append("skipped: ").append(summary.getSkipCount());

if (summary.getPassCount() > 0) {
builder.append(SEPARATOR);
}
}

if (summary.getPassCount() > 0) {
builder.append("passed: ").append(summary.getPassCount());
}


return builder.toString();
}
}