Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b0d77ae

Browse files
ankitsmt211Taz03
andauthoredMar 20, 2024
Bugfix/GitHub reference (#1061)
* skip for non-text channels * handling longer issue body for embed & invalid issue number --------- Co-authored-by: Tanish Azad <tanishazad03@gmail.com>
1 parent 6c3cc81 commit b0d77ae

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed
 

‎application/src/main/java/org/togetherjava/tjbot/commands/github/GitHubReference.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class GitHubReference extends MessageReceiverAdapter {
4242
* The pattern(#123) used to determine whether a message is referencing an issue.
4343
*/
4444
static final Pattern ISSUE_REFERENCE_PATTERN =
45-
Pattern.compile("#(?<%s>\\d+)".formatted(ID_GROUP));
45+
Pattern.compile("#(?<%s>\\d{1,5})".formatted(ID_GROUP));
4646
private static final int ISSUE_OPEN = Color.green.getRGB();
4747
private static final int ISSUE_CLOSE = Color.red.getRGB();
4848

@@ -108,8 +108,9 @@ public void onMessageReceived(MessageReceivedEvent event) {
108108

109109
while (matcher.find()) {
110110
long defaultRepoId = config.getGitHubRepositories().get(0);
111-
findIssue(Integer.parseInt(matcher.group(ID_GROUP)), defaultRepoId)
112-
.ifPresent(issue -> embeds.add(generateReply(issue)));
111+
112+
int issueId = Integer.parseInt(matcher.group(ID_GROUP));
113+
findIssue(issueId, defaultRepoId).ifPresent(issue -> embeds.add(generateReply(issue)));
113114
}
114115

115116
replyBatchEmbeds(embeds, message, false);
@@ -148,6 +149,10 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException {
148149
String titleUrl = issue.getHtmlUrl().toString();
149150
String description = issue.getBody();
150151

152+
if (description != null && description.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH) {
153+
description = "too long for preview, visit Github";
154+
}
155+
151156
String labels = issue.getLabels()
152157
.stream()
153158
.map(GHLabel::getName)
@@ -231,6 +236,10 @@ List<GHRepository> getRepositories() {
231236
}
232237

233238
private boolean isAllowedChannelOrChildThread(MessageReceivedEvent event) {
239+
if (event.getChannelType() != ChannelType.TEXT) {
240+
return false;
241+
}
242+
234243
if (event.getChannelType().isThread()) {
235244
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
236245
String rootChannel = threadChannel.getParentChannel().getName();

0 commit comments

Comments
 (0)
Please sign in to comment.