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
Show file tree
Hide file tree
Changes from 1 commit
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
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</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