Skip to content

Commit

Permalink
Merge pull request #182 from The-Huginn/update-constants
Browse files Browse the repository at this point in the history
Rename constants and keep backwards compatibility for hiding comments
  • Loading branch information
gsmet authored Apr 10, 2024
2 parents c3fd68f + 48a2584 commit 8405037
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.quarkus.bot.buildreporter.githubactions.WorkflowUtils.getActiveStatusCommentMarker;
import static io.quarkus.bot.buildreporter.githubactions.WorkflowUtils.getHiddenStatusCommentMarker;
import static io.quarkus.bot.buildreporter.githubactions.WorkflowUtils.getOldActiveStatusCommentMarker;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -393,20 +394,19 @@ private static void hideOutdatedWorkflowRunResults(BuildReporterConfig buildRepo
List<GHIssueComment> comments = issue.getComments();

for (GHIssueComment comment : comments) {
if (!comment.getBody().contains(WorkflowConstants.MESSAGE_ID_ACTIVE) &&
!comment.getBody().contains(getActiveStatusCommentMarker(workflowName))) {
boolean noOldMarkersFound = !comment.getBody().contains(WorkflowConstants.OLD_MESSAGE_ID_ACTIVE) &&
!comment.getBody().contains(getOldActiveStatusCommentMarker(workflowName));
if ((!comment.getBody().contains(WorkflowConstants.MESSAGE_ID_ACTIVE) &&
!comment.getBody().contains(getActiveStatusCommentMarker(workflowName))) &&
noOldMarkersFound) {
continue;
}

StringBuilder updatedComment = new StringBuilder();
updatedComment.append(WorkflowConstants.HIDE_MESSAGE_PREFIX);
updatedComment.append(comment.getBody().replace(getActiveStatusCommentMarker(workflowName),
getHiddenStatusCommentMarker(workflowName))
.replace(WorkflowConstants.MESSAGE_ID_ACTIVE, getHiddenStatusCommentMarker(workflowName)));
String updatedComment = getUpdatedComment(workflowName, comment);

if (!buildReporterConfig.isDryRun()) {
try {
comment.update(updatedComment.toString());
comment.update(updatedComment);
} catch (IOException e) {
LOG.error(workflowContext.getLogContext() +
" - Unable to hide outdated workflow run status for comment " + comment.getId());
Expand All @@ -423,6 +423,22 @@ private static void hideOutdatedWorkflowRunResults(BuildReporterConfig buildRepo
}
}

private static String getUpdatedComment(String workflowName, GHIssueComment comment) {
StringBuilder updatedComment = new StringBuilder();
updatedComment.append(WorkflowConstants.HIDE_MESSAGE_PREFIX);
// Replace old markers with new ones for compatibility reasons
if (comment.getBody().contains(getOldActiveStatusCommentMarker(workflowName))) {
updatedComment.append(comment.getBody().replace(getOldActiveStatusCommentMarker(workflowName),
getHiddenStatusCommentMarker(workflowName))
.replace(WorkflowConstants.OLD_MESSAGE_ID_ACTIVE, getHiddenStatusCommentMarker(workflowName)));
} else {
updatedComment.append(comment.getBody().replace(getActiveStatusCommentMarker(workflowName),
getHiddenStatusCommentMarker(workflowName))
.replace(WorkflowConstants.MESSAGE_ID_ACTIVE, getHiddenStatusCommentMarker(workflowName)));
}
return updatedComment.toString();
}

private static void minimizeOutdatedComment(DynamicGraphQLClient gitHubGraphQLClient, GHIssueComment comment)
throws ExecutionException, InterruptedException {
Map<String, Object> variables = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ public class WorkflowConstants {

public static final String BUILD_SUMMARY_CHECK_RUN_PREFIX = "Build summary for ";

public static final String MESSAGE_ID_ACTIVE = "<!-- Quarkus-GitHub-Bot/msg-id:workflow-run-status-active -->";
public static final String MESSAGE_ID_ACTIVE_FOR_WORKFLOW = "<!-- Quarkus-GitHub-Bot/msg-id:workflow-run-status-active:%1$s -->";
public static final String MESSAGE_ID_HIDDEN_FOR_WORKFLOW = "<!-- Quarkus-GitHub-Bot/msg-id:workflow-run-status-hidden:%1$s -->";
public static final String WORKFLOW_RUN_ID_MARKER = "<!-- Quarkus-GitHub-Bot/workflow-run-id:%1$s -->";
public static final String BUILD_SCANS_CHECK_RUN_MARKER = "<!-- Quarkus-GitHub-Bot/build-scans-check-run -->";
public static final String MESSAGE_ID_ACTIVE = "<!-- Build-Reporter/msg-id:workflow-run-status-active -->";
public static final String MESSAGE_ID_ACTIVE_FOR_WORKFLOW = "<!-- Build-Reporter/msg-id:workflow-run-status-active:%1$s -->";
public static final String MESSAGE_ID_HIDDEN_FOR_WORKFLOW = "<!-- Build-Reporter/msg-id:workflow-run-status-hidden:%1$s -->";
public static final String WORKFLOW_RUN_ID_MARKER = "<!-- Build-Reporter/workflow-run-id:%1$s -->";
public static final String BUILD_SCANS_CHECK_RUN_MARKER = "<!-- Build-Reporter/build-scans-check-run -->";
public static final String HIDE_MESSAGE_PREFIX = """
---
> :waning_crescent_moon: **_This workflow status is outdated as a new workflow run has been triggered._**
---
""";

@Deprecated(forRemoval = true, since = "3.7.0")
public static final String OLD_MESSAGE_ID_ACTIVE = "<!-- Quarkus-GitHub-Bot/msg-id:workflow-run-status-active -->";
@Deprecated(forRemoval = true, since = "3.7.0")
public static final String OLD_MESSAGE_ID_ACTIVE_FOR_WORKFLOW = "<!-- Quarkus-GitHub-Bot/msg-id:workflow-run-status-active:%1$s -->";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static String getHiddenStatusCommentMarker(String workflowName) {
return String.format(WorkflowConstants.MESSAGE_ID_HIDDEN_FOR_WORKFLOW, workflowName);
}

@Deprecated(forRemoval = true, since = "3.7.0")
public static String getOldActiveStatusCommentMarker(String workflowName) {
return String.format(WorkflowConstants.OLD_MESSAGE_ID_ACTIVE_FOR_WORKFLOW, workflowName);
}

private WorkflowUtils() {
}
}

0 comments on commit 8405037

Please sign in to comment.