-
Notifications
You must be signed in to change notification settings - Fork 340
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
Changes from 4 commits
bbe9dc4
15451aa
e660f66
3b2b910
f211120
d7ab2dc
b3f2963
0ab7d5d
fd82be9
24de3e7
dc8ab01
ca7f51b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)") | ||
.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(")"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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