Skip to content

Commit

Permalink
Always insert latest report in issue description
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Jul 15, 2024
1 parent 53ec14b commit 1440490
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
class GithubStatusReporter implements StatusReporter {

private static final String STATUS_REPORT_HEADER = "## search.quarkus.io indexing status: ";
private static final int GITHUB_MAX_COMMENT_LENGTH = 65536;

private final Clock clock;
private final ReportingConfig.GithubReporter config;
Expand All @@ -48,19 +47,21 @@ public void report(Status status, Map<FailureCollector.Level, List<Failure>> fai
// Update last indexing date:
issue.setTitle(toStatusSummary(clock, status, issue.getTitle()));

// Build a status report
StringBuilder newReportBuilder = new StringBuilder(STATUS_REPORT_HEADER)
.append(status).append('\n');
toStatusDetailsMarkdown(newReportBuilder, failures, true);
String newReport = newReportBuilder.toString();

// Update the issue description with the content of the latest comment,
// for convenience, and to have information available even when indexing is unstable (no issue comment).
// This must be done before the comment, so that notifications triggered by the comment are only sent
// when the issue is fully updated.
issue.setBody(StatusRenderer.insertMessageInIssueDescription(issue.getBody(), newReport));

// add comments if needed:
if (!Status.SUCCESS.equals(status)) {
StringBuilder newMessageBuilder = new StringBuilder(STATUS_REPORT_HEADER)
.append(status).append('\n');

toStatusDetailsMarkdown(newMessageBuilder, failures, true);
String newMessage = newMessageBuilder.toString();
if (newMessage.length() > GITHUB_MAX_COMMENT_LENGTH) {
newMessage = ("### Message truncated as it was too long\n" + newMessage).substring(
0,
GITHUB_MAX_COMMENT_LENGTH);
}

String reportComment = StatusRenderer.truncateForGitHubMaxLength(newReport, 0);
switch (status) {
case WARNING -> {
// When warning, only comment if we didn't comment the same thing recently.
Expand All @@ -69,18 +70,18 @@ public void report(Status status, Map<FailureCollector.Level, List<Failure>> fai
clock.instant().minus(config.warningRepeatDelay()))
.reduce(Streams.last());
if (lastRecentCommentByMe.isPresent()
&& lastRecentCommentByMe.get().getBody().contentEquals(newMessage)) {
&& lastRecentCommentByMe.get().getBody().contentEquals(reportComment)) {
Log.infof("Skipping the issue comment because the same message was sent recently.");
} else {
issue.comment(newMessage);
issue.comment(reportComment);
}
}
case UNSTABLE -> {
// When unstable, never comment: there'll be a retry, and we want to avoid unnecessary noise.
}
case CRITICAL ->
// When critical, always comment.
issue.comment(newMessage);
issue.comment(reportComment);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public abstract class StatusRenderer {
"uuuu-MM-dd'T'HH:mm:ssZZZZZ",
Locale.ROOT);

private static final int GITHUB_MAX_COMMENT_LENGTH = 65536;
private static final String INSERT_START_MARKER = "<!-- Automatic message start -->";
private static final String INSERT_END_MARKER = "<!-- Automatic message end -->";

public static String toStatusSummary(Clock clock, Status status, String previousSummary) {
String toInsert = TITLE_UPDATED_AND_STATUS_FORMAT.formatted(
formatStatus(status),
Expand Down Expand Up @@ -96,4 +100,41 @@ private static void formatException(StringBuilder sb, Exception exception) {

sb.append("\n ```\n </details>\n\n");
}

static String insertMessageInIssueDescription(String originalIssueDescription, String newMessage) {
StringBuilder result = new StringBuilder(originalIssueDescription != null ? originalIssueDescription : "");
int startMarkerIndex = result.indexOf(INSERT_START_MARKER);
int endMarkerIndex = startMarkerIndex < 0 ? -1 : result.indexOf(INSERT_END_MARKER);
if (startMarkerIndex >= 0 && endMarkerIndex >= 0) {
result.replace(startMarkerIndex + INSERT_START_MARKER.length(), endMarkerIndex, "\n");
} else {
result.append('\n');
startMarkerIndex = result.length();
result.append(INSERT_START_MARKER).append('\n').append(INSERT_END_MARKER);
}
int currentIndex = startMarkerIndex + INSERT_START_MARKER.length();

String quoteIntroMessage = "\n## Last update\n";
result.insert(currentIndex, quoteIntroMessage);
currentIndex += quoteIntroMessage.length();

String truncatedMessage = truncateForGitHubMaxLength(asMarkdownQuote(newMessage), result.length());
result.insert(currentIndex, truncatedMessage);

return result.toString();
}

static String truncateForGitHubMaxLength(String message, int reservedLength) {
int maxLength = GITHUB_MAX_COMMENT_LENGTH - reservedLength;
if (message.length() > maxLength) {
return ("### Message truncated as it was too long\n" + message)
.substring(0, maxLength);
} else {
return message;
}
}

private static String asMarkdownQuote(String string) {
return string.lines().map(s -> "> " + s).collect(Collectors.joining("\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,33 @@ void toStatusSummary(String expected, Status status, String currentTitle) {
.isEqualTo(expected);
}

@ParameterizedTest
@CsvSource(textBlock = """
'Original description
<!-- Automatic message start -->
## Last update
> Automatic message
> and some more
<!-- Automatic message end -->',\
'Original description',\
'Automatic message
and some more'
'Original description
<!-- Automatic message start -->
## Last update
> New automatic message
> and some more
<!-- Automatic message end -->',\
'Original description
<!-- Automatic message start -->
Random garbage
<!-- Automatic message end -->',\
'New automatic message
and some more'
""")
void insertMessageInIssueDescription(String expected, String originalIssueDescription, String newMessage) {
assertThat(StatusRenderer.insertMessageInIssueDescription(originalIssueDescription, newMessage))
.isEqualTo(expected);
}

}

0 comments on commit 1440490

Please sign in to comment.