Skip to content

Commit 08d55ca

Browse files
authored
Implementing support for more accurate github embed colors (#1075)
* Added support for "complete" and "not planned" issue states * Changed the purple embed color * Enabled support for PR embed colors and their different states * Reduced cognitive complexity of getIssueState method * Added @SquidXTV changes * Added @ankitsmt211 changes
1 parent 92f0c5f commit 08d55ca

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

application/src/main/java/org/togetherjava/tjbot/features/github/GitHubReference.java

+37-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ public final class GitHubReference extends MessageReceiverAdapter {
4343
*/
4444
static final Pattern ISSUE_REFERENCE_PATTERN =
4545
Pattern.compile("#(?<%s>\\d{1,5})".formatted(ID_GROUP));
46-
private static final int ISSUE_OPEN = Color.green.getRGB();
47-
private static final int ISSUE_CLOSE = Color.red.getRGB();
46+
47+
// Representing different GitHub states of an Issue/PR
48+
private static final Color OPEN_STATE = Color.green;
49+
private static final Color CLOSE_STATE = Color.red;
50+
private static final Color MERGED_STATE = new Color(141, 106, 187);
51+
private static final Color NOT_PLANNED_STATE = new Color(72, 72, 72);
52+
private static final Color DRAFT_STATE = Color.gray;
53+
4854

4955
/**
5056
* 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 {
167173
String dateOfCreation = FORMATTER.format(createdAt);
168174

169175
String footer = "%s • %s • %s".formatted(labels, assignees, dateOfCreation);
170-
171-
return new EmbedBuilder()
172-
.setColor(issue.getState() == GHIssueState.OPEN ? ISSUE_OPEN : ISSUE_CLOSE)
176+
return new EmbedBuilder().setColor(getIssueStateColor(issue))
173177
.setTitle(title, titleUrl)
174178
.setDescription(description)
175179
.setAuthor(issue.getUser().getName(), null, issue.getUser().getAvatarUrl())
@@ -181,6 +185,26 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException {
181185
}
182186
}
183187

188+
/**
189+
* Returns the color based on the state of the issue/PR
190+
*/
191+
private Color getIssueStateColor(GHIssue issue) throws IOException {
192+
if (issue instanceof GHPullRequest pr) {
193+
if (pr.isMerged()) {
194+
return MERGED_STATE;
195+
} else if (pr.isDraft()) {
196+
return DRAFT_STATE;
197+
}
198+
} else {
199+
if (issue.getStateReason() == GHIssueStateReason.COMPLETED) {
200+
return MERGED_STATE;
201+
} else if (issue.getStateReason() == GHIssueStateReason.NOT_PLANNED) {
202+
return NOT_PLANNED_STATE;
203+
}
204+
}
205+
return issue.getState() == GHIssueState.OPEN ? OPEN_STATE : CLOSE_STATE;
206+
}
207+
184208
/**
185209
* Either properly gathers the name of a user or throws a UncheckedIOException.
186210
*/
@@ -199,6 +223,9 @@ Optional<GHIssue> findIssue(int id, String targetIssueTitle) {
199223
return repositories.stream().map(repository -> {
200224
try {
201225
GHIssue issue = repository.getIssue(id);
226+
if (issue.isPullRequest()) {
227+
issue = repository.getPullRequest(id);
228+
}
202229
if (issue.getTitle().equals(targetIssueTitle)) {
203230
return Optional.of(issue);
204231
}
@@ -216,7 +243,11 @@ Optional<GHIssue> findIssue(int id, long defaultRepoId) {
216243
.filter(repository -> repository.getId() == defaultRepoId)
217244
.map(repository -> {
218245
try {
219-
return Optional.of(repository.getIssue(id));
246+
GHIssue issue = repository.getIssue(id);
247+
if (issue.isPullRequest()) {
248+
issue = repository.getPullRequest(id);
249+
}
250+
return Optional.of(issue);
220251
} catch (FileNotFoundException ignored) {
221252
return Optional.<GHIssue>empty();
222253
} catch (IOException ex) {

0 commit comments

Comments
 (0)