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 4 commits
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
41 changes: 34 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<jenkins.version>2.222.4</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<configuration-as-code.version>1.44</configuration-as-code.version>
</properties>
<licenses>
<license>
Expand Down Expand Up @@ -50,6 +51,34 @@
<artifactId>echarts-api</artifactId>
<version>4.8.0-2</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>checks-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>plugin-util-api</artifactId>
<version>1.3.0</version>
<exclusions>
<!-- TODO https://github.com/jenkinsci/analysis-pom-plugin/pull/153 -->
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>display-url-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>github-checks</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>bootstrap4-api</artifactId>
Expand Down Expand Up @@ -140,21 +169,19 @@
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
<!-- TODO https://github.com/jenkinsci/configuration-as-code-plugin/pull/1477 -->
<!-- TODO bump version in jcasc -->
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
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,148 @@
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 jnr.ffi.Struct;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
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 testsURL = DisplayURLProvider.get().getTestsURL(action.run);
String text = extractChecksText(testsURL);
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(testsURL)
.withOutput(output)
.build();
}

private String extractChecksText(String testsURL) {
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](")
.append(testsURL).append(")");

Choose a reason for hiding this comment

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

Does this testsURL links to the JUnit summary page? If so, I think we should use different links for here and the detailsURL, maybe just the build page links for the detailsURL?

Copy link
Member Author

Choose a reason for hiding this comment

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

It links to the same place, I don’t think there’s any need to link differently the only place one would go it the test report from this check

}
}

return builder.toString();
}

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

if (StringUtils.isNotBlank(failedTest.getErrorDetails())) {
builder.append(codeTextFencedBlock(failedTest.getErrorDetails()))
.append("\n");
}
if (StringUtils.isNotBlank(failedTest.getErrorStackTrace())) {
builder.append("<details><summary>Stack trace</summary>\n")
.append(codeTextFencedBlock(failedTest.getErrorStackTrace()))
.append("</details>\n");
}

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

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

private String codeTextFencedBlock(String body) {
return "\n```text\n" + body.trim() + "\n```\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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public class TestResultStorageJunitTest {
"<testcase classname='Klazz' name='test2' time='2.0'/>" +
"<testcase classname='other.Klazz' name='test3'><skipped message='Not actually run.'/></testcase>" +
"</testsuite>'''\n" +
" def s = junit 'x.xml'\n" +
" def s = junit testResults: 'x.xml', skipPublishingChecks: true\n" +
" echo(/summary: fail=$s.failCount skip=$s.skipCount pass=$s.passCount total=$s.totalCount/)\n" +
" writeFile file: 'x.xml', text: '''<testsuite name='supersweet'>" +
"<testcase classname='another.Klazz' name='test1'><error message='another failure'/></testcase>" +
"</testsuite>'''\n" +
" s = junit 'x.xml'\n" +
" s = junit testResults: 'x.xml', skipPublishingChecks: true\n" +
" echo(/next summary: fail=$s.failCount skip=$s.skipCount pass=$s.passCount total=$s.totalCount/)\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).get();
Expand Down