Skip to content

Commit

Permalink
Bugfix/GitHub reference (#1061)
Browse files Browse the repository at this point in the history
* skip for non-text channels

* handling longer issue body for embed & invalid issue number

---------

Co-authored-by: Tanish Azad <[email protected]>
  • Loading branch information
ankitsmt211 and Taz03 authored Mar 20, 2024
1 parent 6c3cc81 commit b0d77ae
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class GitHubReference extends MessageReceiverAdapter {
* The pattern(#123) used to determine whether a message is referencing an issue.
*/
static final Pattern ISSUE_REFERENCE_PATTERN =
Pattern.compile("#(?<%s>\\d+)".formatted(ID_GROUP));
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();

Expand Down Expand Up @@ -108,8 +108,9 @@ public void onMessageReceived(MessageReceivedEvent event) {

while (matcher.find()) {
long defaultRepoId = config.getGitHubRepositories().get(0);
findIssue(Integer.parseInt(matcher.group(ID_GROUP)), defaultRepoId)
.ifPresent(issue -> embeds.add(generateReply(issue)));

int issueId = Integer.parseInt(matcher.group(ID_GROUP));
findIssue(issueId, defaultRepoId).ifPresent(issue -> embeds.add(generateReply(issue)));
}

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

if (description != null && description.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH) {
description = "too long for preview, visit Github";
}

String labels = issue.getLabels()
.stream()
.map(GHLabel::getName)
Expand Down Expand Up @@ -231,6 +236,10 @@ List<GHRepository> getRepositories() {
}

private boolean isAllowedChannelOrChildThread(MessageReceivedEvent event) {
if (event.getChannelType() != ChannelType.TEXT) {
return false;
}

if (event.getChannelType().isThread()) {
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
String rootChannel = threadChannel.getParentChannel().getName();
Expand Down

0 comments on commit b0d77ae

Please sign in to comment.