diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/com/eternalcode/discordapp/review/GitHubReviewService.java b/src/main/java/com/eternalcode/discordapp/review/GitHubReviewService.java index 002dd57b..c58ea44b 100644 --- a/src/main/java/com/eternalcode/discordapp/review/GitHubReviewService.java +++ b/src/main/java/com/eternalcode/discordapp/review/GitHubReviewService.java @@ -92,7 +92,7 @@ public boolean checkPullRequestTitle(GitHubPullRequest url) throws IOException { public long createReviewForumPost(Guild guild, GitHubPullRequest pullRequest) throws IOException { this.waitForRateLimit(); - String pullRequestTitleFromUrl = + String pullRequestTitle = GitHubReviewUtil.getPullRequestTitleFromUrl(pullRequest, this.appConfig.githubToken); ForumChannel forumChannel = guild.getForumChannelById(this.appConfig.reviewSystem.reviewForumId); @@ -100,10 +100,9 @@ public long createReviewForumPost(Guild guild, GitHubPullRequest pullRequest) th throw new IOException("Forum channel not found with ID: " + this.appConfig.reviewSystem.reviewForumId); } - MessageCreateData createData = MessageCreateData.fromContent(pullRequestTitleFromUrl); + MessageCreateData createData = MessageCreateData.fromContent(pullRequest.toUrl()); - return forumChannel.createForumPost(pullRequestTitleFromUrl, createData) - .setName(pullRequest.toUrl()) + return forumChannel.createForumPost(pullRequestTitle, createData) .setTags(ForumTagSnowflake.fromId(this.appConfig.reviewSystem.inReviewForumTagId)) .complete() .getThreadChannel() @@ -225,6 +224,35 @@ private void sendDirectMessage(User user, String message) { ); } + private Result getPullRequestFromThread(ThreadChannel threadChannel) { + try { + // Try to get the first message in the thread which should contain the PR URL + List messages = threadChannel.getHistory() + .retrievePast(1) + .complete(); + + if (messages.isEmpty()) { + LOGGER.log(Level.WARNING, "No messages found in thread: " + threadChannel.getId()); + return Result.error(new IllegalArgumentException("No messages in thread")); + } + + String firstMessageContent = messages.get(0).getContentRaw(); + return GitHubPullRequest.fromUrl(firstMessageContent); + } + catch (net.dv8tion.jda.api.exceptions.ErrorResponseException exception) { + LOGGER.log(Level.WARNING, "Discord API error retrieving message from thread: " + threadChannel.getId(), exception); + return Result.error(new IllegalArgumentException("Discord API error: " + exception.getMeaning())); + } + catch (RateLimitedException exception) { + LOGGER.log(Level.WARNING, "Rate limited when retrieving message from thread: " + threadChannel.getId(), exception); + return Result.error(new IllegalArgumentException("Rate limited by Discord API")); + } + catch (Exception exception) { + LOGGER.log(Level.WARNING, "Unexpected error retrieving message from thread: " + threadChannel.getId(), exception); + return Result.error(new IllegalArgumentException("Unexpected error: " + exception.getMessage())); + } + } + public CompletableFuture mentionReviewersOnAllReviewChannels(JDA jda) { return CompletableFuture.runAsync(() -> { Guild guild = jda.getGuildById(this.appConfig.guildId); @@ -239,7 +267,7 @@ public CompletableFuture mentionReviewersOnAllReviewChannels(JDA jda) { for (ForumChannel forumChannel : guild.getForumChannels()) { for (ThreadChannel threadChannel : forumChannel.getThreadChannels()) { Result result = - GitHubPullRequest.fromUrl(threadChannel.getName()); + this.getPullRequestFromThread(threadChannel); if (result.isErr()) { continue; @@ -268,7 +296,10 @@ public boolean isReviewPostCreatedInGuild(Guild guild, String url) { try { for (ForumChannel forumChannel : guild.getForumChannels()) { for (ThreadChannel threadChannel : forumChannel.getThreadChannels()) { - if (url.equals(threadChannel.getName())) { + Result result = + this.getPullRequestFromThread(threadChannel); + + if (result.isOk() && url.equals(result.get().toUrl())) { return true; } } @@ -295,8 +326,7 @@ public CompletableFuture archiveMergedPullRequest(JDA jda) { for (ForumChannel forumChannel : guild.getForumChannels()) { for (ThreadChannel threadChannel : forumChannel.getThreadChannels()) { - String name = threadChannel.getName(); - Result result = GitHubPullRequest.fromUrl(name); + Result result = this.getPullRequestFromThread(threadChannel); if (result.isErr()) { continue;