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

Use truncated strings from checks api #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
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