-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bbe9dc4
Checks API integration
timja 15451aa
Display url API and pom fixups
timja e660f66
Pom fixups
timja 3b2b910
Review feedback
timja f211120
Add jelly config
timja d7ab2dc
Add docs
timja b3f2963
Add tests
timja 0ab7d5d
Windows test fix maybe?
timja fd82be9
Fix windows
timja 24de3e7
Line endings too hard
timja dc8ab01
Re add test but don't check content
timja ca7f51b
Give up
timja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/io/jenkins/plugins/junit/checks/JUnitChecksPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
.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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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