Skip to content

Commit

Permalink
Allow for moving truncation into the implementing API
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Jan 1, 2021
1 parent 460f8e1 commit 0287144
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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,
* annotations, etc.
*/
public class ChecksOutput {
private final String title;
private final String summary;
private final String text;
private final List<String> summary;
private final List<String> text;
private final List<ChecksAnnotation> annotations;
private final List<ChecksImage> images;

private ChecksOutput(final String title, final String summary, final String text,
private ChecksOutput(final String title, final List<String> summary, final List<String> text,
final List<ChecksAnnotation> annotations, final List<ChecksImage> images) {
this.title = title;
this.summary = summary;
Expand All @@ -34,20 +36,43 @@ 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());
}

public Optional<String> getTitle() {
return Optional.ofNullable(title);
}

private static String truncateText(List<String> 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<String> getSummary() {
return Optional.ofNullable(summary);
return Optional.ofNullable(summary).map(StringUtils::join);
}

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

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

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

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

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 0287144

Please sign in to comment.