Skip to content

Commit

Permalink
Use truncated strings from checks api
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Jan 10, 2021
1 parent 0c6a43d commit c25245f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<configuration-as-code.version>1.46</configuration-as-code.version>
<checks-api.version>1.2.0</checks-api.version>
<checks-api.version>1.3.0-rc182.e92aa81c9bc8</checks-api.version>
</properties>
<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.checks.api.TruncatedString;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.kohsuke.accmod.Restricted;
Expand All @@ -23,8 +24,6 @@
public class JUnitChecksPublisher {
public static final String SEPARATOR = ", ";

// cap to avoid hitting check API message limit
private static final int MAX_MSG_SIZE_TO_CHECKS_API = 65535;
private final Run run;
private final String checksName;
private final TestResult result;
Expand Down Expand Up @@ -60,26 +59,17 @@ ChecksDetails extractChecksDetails() {
.build();
}

private String extractChecksText(String testsURL) {
StringBuilder builder = new StringBuilder();
private TruncatedString extractChecksText(String testsURL) {
TruncatedString.Builder builder = new TruncatedString.Builder()
.withTruncationText(String.format("%nmore test results are not shown here, view them on [Jenkins](%s)", testsURL));

if (summary.getFailCount() > 0) {
List<CaseResult> failedTests = result.getFailedTests();

for (CaseResult failedTest: failedTests) {
String testReport = mapFailedTestToTestReport(failedTest);
int messageSize = testReport.length() + builder.toString().length();
// to ensure text size is withing check API message limit
if (messageSize > (MAX_MSG_SIZE_TO_CHECKS_API - 1024)){
builder.append("\n")
.append("more test results are not shown here, view them on [Jenkins](")
.append(testsURL).append(")");
break;
}
builder.append(testReport);
}
result.getFailedTests().stream()
.map(this::mapFailedTestToTestReport)
.forEach(builder::addText);
}

return builder.toString();
return builder.build();
}

private String mapFailedTestToTestReport(CaseResult failedTest) {
Expand Down

0 comments on commit c25245f

Please sign in to comment.