diff --git a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubReference.java b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubReference.java index c2cdb7b2ad..5f9ae9a4bb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubReference.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubReference.java @@ -43,8 +43,14 @@ public final class GitHubReference extends MessageReceiverAdapter { */ static final Pattern ISSUE_REFERENCE_PATTERN = Pattern.compile("#(?<%s>\\d{1,5})".formatted(ID_GROUP)); - private static final int ISSUE_OPEN = Color.green.getRGB(); - private static final int ISSUE_CLOSE = Color.red.getRGB(); + + // Representing different GitHub states of an Issue/PR + private static final Color OPEN_STATE = Color.green; + private static final Color CLOSE_STATE = Color.red; + private static final Color MERGED_STATE = new Color(141, 106, 187); + private static final Color NOT_PLANNED_STATE = new Color(72, 72, 72); + private static final Color DRAFT_STATE = Color.gray; + /** * A constant representing the date and time formatter used for formatting the creation date of @@ -167,9 +173,7 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException { String dateOfCreation = FORMATTER.format(createdAt); String footer = "%s • %s • %s".formatted(labels, assignees, dateOfCreation); - - return new EmbedBuilder() - .setColor(issue.getState() == GHIssueState.OPEN ? ISSUE_OPEN : ISSUE_CLOSE) + return new EmbedBuilder().setColor(getIssueStateColor(issue)) .setTitle(title, titleUrl) .setDescription(description) .setAuthor(issue.getUser().getName(), null, issue.getUser().getAvatarUrl()) @@ -181,6 +185,26 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException { } } + /** + * Returns the color based on the state of the issue/PR + */ + private Color getIssueStateColor(GHIssue issue) throws IOException { + if (issue instanceof GHPullRequest pr) { + if (pr.isMerged()) { + return MERGED_STATE; + } else if (pr.isDraft()) { + return DRAFT_STATE; + } + } else { + if (issue.getStateReason() == GHIssueStateReason.COMPLETED) { + return MERGED_STATE; + } else if (issue.getStateReason() == GHIssueStateReason.NOT_PLANNED) { + return NOT_PLANNED_STATE; + } + } + return issue.getState() == GHIssueState.OPEN ? OPEN_STATE : CLOSE_STATE; + } + /** * Either properly gathers the name of a user or throws a UncheckedIOException. */ @@ -199,6 +223,9 @@ Optional findIssue(int id, String targetIssueTitle) { return repositories.stream().map(repository -> { try { GHIssue issue = repository.getIssue(id); + if (issue.isPullRequest()) { + issue = repository.getPullRequest(id); + } if (issue.getTitle().equals(targetIssueTitle)) { return Optional.of(issue); } @@ -216,7 +243,11 @@ Optional findIssue(int id, long defaultRepoId) { .filter(repository -> repository.getId() == defaultRepoId) .map(repository -> { try { - return Optional.of(repository.getIssue(id)); + GHIssue issue = repository.getIssue(id); + if (issue.isPullRequest()) { + issue = repository.getPullRequest(id); + } + return Optional.of(issue); } catch (FileNotFoundException ignored) { return Optional.empty(); } catch (IOException ex) {