Skip to content

Commit

Permalink
alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Jan 1, 2021
1 parent 0287144 commit 1d9b03d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 27 deletions.
46 changes: 19 additions & 27 deletions src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
public class ChecksOutput {
private final String title;
private final List<String> summary;
private final List<String> text;
private final TruncatedString summary;
private final TruncatedString text;
private final List<ChecksAnnotation> annotations;
private final List<ChecksImage> images;

private ChecksOutput(final String title, final List<String> summary, final List<String> text,
private ChecksOutput(final String title, final TruncatedString summary, final TruncatedString text,
final List<ChecksAnnotation> annotations, final List<ChecksImage> images) {
this.title = title;
this.summary = summary;
Expand All @@ -37,8 +37,8 @@ private ChecksOutput(final String title, final List<String> summary, final List<
*/
public ChecksOutput(final ChecksOutput that) {
this(that.getTitle().orElse(null),
that.getSummary().map(Collections::singletonList).orElse(null),
that.getText().map(Collections::singletonList).orElse(null),
that.getSummary().map(TruncatedString::fromString).orElse(null),
that.getText().map(TruncatedString::fromString).orElse(null),
that.getChecksAnnotations(), that.getChecksImages());
}

Expand All @@ -60,19 +60,19 @@ private static String truncateText(List<String> chunks, int maxLength, String tr
}

public Optional<String> getSummary() {
return Optional.ofNullable(summary).map(StringUtils::join);
return Optional.ofNullable(summary).map(TruncatedString::build);
}

public Optional<String> getSummary(int maxLength, String truncateText) {
return Optional.ofNullable(summary).map(s -> truncateText(s, maxLength, truncateText));
public Optional<String> getSummary(TruncatedString.Truncator truncator) {
return Optional.ofNullable(summary).map(truncator::truncate);
}

public Optional<String> getText() {
return Optional.ofNullable(text).map(StringUtils::join);
return Optional.ofNullable(text).map(TruncatedString::build);
}

public Optional<String> getText(int maxLength, String truncateText) {
return Optional.ofNullable(text).map(s -> truncateText(s, maxLength, truncateText));
public Optional<String> getText(TruncatedString.Truncator truncator) {
return Optional.ofNullable(text).map(truncator::truncate);
}

public List<ChecksAnnotation> getChecksAnnotations() {
Expand All @@ -99,8 +99,8 @@ public String toString() {
*/
public static class ChecksOutputBuilder {
private String title;
private List<String> summary;
private List<String> text;
private TruncatedString summary;
private TruncatedString text;
private List<ChecksAnnotation> annotations;
private List<ChecksImage> images;

Expand Down Expand Up @@ -139,15 +139,11 @@ public ChecksOutputBuilder withTitle(final String title) {
*/
@SuppressWarnings("HiddenField") // builder pattern
public ChecksOutputBuilder withSummary(final String summary) {
this.summary = new ArrayList<>();
return appendSummary(summary);
return withSummary(new TruncatedString.Builder().addText(summary).build());
}

public ChecksOutputBuilder appendSummary(final String summary) {
if (this.summary == null) {
this.summary = new ArrayList<>();
}
this.summary.add(requireNonNull(summary));
public ChecksOutputBuilder withSummary(final TruncatedString summary) {
this.summary = requireNonNull(summary);
return this;
}

Expand All @@ -164,15 +160,11 @@ public ChecksOutputBuilder appendSummary(final String summary) {
*/
@SuppressWarnings("HiddenField") // builder pattern
public ChecksOutputBuilder withText(final String text) {
this.text = new ArrayList<>();
return appendText(text);
return withText(new TruncatedString.Builder().addText(text).build());
}

public ChecksOutputBuilder appendText(final String text) {
if (this.text == null) {
this.text = new ArrayList<>();
}
this.text.add(requireNonNull(text));
public ChecksOutputBuilder withText(final TruncatedString text) {
this.text = requireNonNull(text);
return this;
}

Expand Down
93 changes: 93 additions & 0 deletions src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.jenkins.plugins.checks.api;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class TruncatedString {

private final List<CharSequence> chunks;
private final String readMoreLink;

/**
* Create a {@link TruncatedString} with the provided limit and truncation message.
*
* @param truncatedMessage the message to be appended should maxSize be exceeded, e.g.
* "Some output is not shown here, see more on [Jenkins](url)."
*/
private TruncatedString(final String readMoreLink, final List<CharSequence> chunks) {
this.readMoreLink = readMoreLink;
this.chunks = chunks;
}

static TruncatedString fromString(String string) {
return new TruncatedString.Builder().addText(string).build();
}

public static class Builder {
private String readMoreLink;
private final List<CharSequence> chunks = new ArrayList<>();

public TruncatedString build() {
return new TruncatedString(readMoreLink, chunks);
}

public Builder withReadMoreLink(String readMoreLink) {
this.readMoreLink = readMoreLink;
return this;
}

public Builder addText(CharSequence chunk) {
this.chunks.add(chunk);
return this;
}

}

public static abstract class Truncator {

protected final int maxSize;

public Truncator(int maxSize) {
this.maxSize = maxSize;
}

public abstract String getReadMoreText(TruncatedString truncatedString);

public String truncate(TruncatedString truncatedString) {
String readMoreText = getReadMoreText(truncatedString);
if (truncatedString.chunks.size() == 0) {
return null;
}
StringBuilder builder = new StringBuilder();
for (CharSequence chunk: truncatedString.chunks) {
if (builder.length() + chunk.length() + readMoreText.length() < maxSize) {
builder.append(chunk);
} else {
builder.append(readMoreText);
break;
}
}
return builder.toString();
}
}

/* example implementation */
public static class GitHubTruncator extends Truncator {

public GitHubTruncator() {
super(65536);
}

@Override
public String getReadMoreText(TruncatedString truncatedString) {
return String.format("%n%nOutput truncated - [more information](%s)", truncatedString.readMoreLink);
}
}

public String build() {
return StringUtils.join(chunks);
}

}

0 comments on commit 1d9b03d

Please sign in to comment.