From 0287144cee4e75d38ee1645605cfb039edea83bf Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Fri, 1 Jan 2021 13:16:02 +0000 Subject: [PATCH] Allow for moving truncation into the implementing API --- .../plugins/checks/api/ChecksOutput.java | 63 +++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java b/src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java index addd96bf..4c6131e3 100644 --- a/src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java +++ b/src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java @@ -1,11 +1,13 @@ package io.jenkins.plugins.checks.api; +import org.apache.commons.lang3.StringUtils; + import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; -import static java.util.Objects.*; +import static java.util.Objects.requireNonNull; /** * An output of a check. The output usually contains the most useful information like summary, description, @@ -13,12 +15,12 @@ */ public class ChecksOutput { private final String title; - private final String summary; - private final String text; + private final List summary; + private final List text; private final List annotations; private final List images; - private ChecksOutput(final String title, final String summary, final String text, + private ChecksOutput(final String title, final List summary, final List text, final List annotations, final List images) { this.title = title; this.summary = summary; @@ -34,7 +36,9 @@ private ChecksOutput(final String title, final String summary, final String text * the source to copy from */ public ChecksOutput(final ChecksOutput that) { - this(that.getTitle().orElse(null), that.getSummary().orElse(null), that.getText().orElse(null), + this(that.getTitle().orElse(null), + that.getSummary().map(Collections::singletonList).orElse(null), + that.getText().map(Collections::singletonList).orElse(null), that.getChecksAnnotations(), that.getChecksImages()); } @@ -42,12 +46,33 @@ public Optional getTitle() { return Optional.ofNullable(title); } + private static String truncateText(List chunks, int maxLength, String truncateText) { + StringBuilder builder = new StringBuilder(); + for (String chunk: chunks) { + if (builder.length() + chunk.length() + truncateText.length() < maxLength) { + builder.append(chunk); + } else { + builder.append(truncateText); + break; + } + } + return builder.toString(); + } + public Optional getSummary() { - return Optional.ofNullable(summary); + return Optional.ofNullable(summary).map(StringUtils::join); + } + + public Optional getSummary(int maxLength, String truncateText) { + return Optional.ofNullable(summary).map(s -> truncateText(s, maxLength, truncateText)); } public Optional getText() { - return Optional.ofNullable(text); + return Optional.ofNullable(text).map(StringUtils::join); + } + + public Optional getText(int maxLength, String truncateText) { + return Optional.ofNullable(text).map(s -> truncateText(s, maxLength, truncateText)); } public List getChecksAnnotations() { @@ -74,8 +99,8 @@ public String toString() { */ public static class ChecksOutputBuilder { private String title; - private String summary; - private String text; + private List summary; + private List text; private List annotations; private List images; @@ -114,7 +139,15 @@ public ChecksOutputBuilder withTitle(final String title) { */ @SuppressWarnings("HiddenField") // builder pattern public ChecksOutputBuilder withSummary(final String summary) { - this.summary = requireNonNull(summary); + this.summary = new ArrayList<>(); + return appendSummary(summary); + } + + public ChecksOutputBuilder appendSummary(final String summary) { + if (this.summary == null) { + this.summary = new ArrayList<>(); + } + this.summary.add(requireNonNull(summary)); return this; } @@ -131,7 +164,15 @@ public ChecksOutputBuilder withSummary(final String summary) { */ @SuppressWarnings("HiddenField") // builder pattern public ChecksOutputBuilder withText(final String text) { - this.text = requireNonNull(text); + this.text = new ArrayList<>(); + return appendText(text); + } + + public ChecksOutputBuilder appendText(final String text) { + if (this.text == null) { + this.text = new ArrayList<>(); + } + this.text.add(requireNonNull(text)); return this; }