Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,17 @@ 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);

if (forumChannel == null) {
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of .complete() makes this a blocking network call. While it's executed within a CompletableFuture, relying on blocking operations can lead to performance issues and thread pool exhaustion under high load. It would be more robust and scalable to use JDA's asynchronous queue() or submit() method and refactor the method to return a CompletableFuture<Long>. This would make the call non-blocking and more aligned with a reactive programming style.

.getThreadChannel()
Expand Down Expand Up @@ -225,6 +224,35 @@ private void sendDirectMessage(User user, String message) {
);
}

private Result<GitHubPullRequest, IllegalArgumentException> getPullRequestFromThread(ThreadChannel threadChannel) {
try {
// Try to get the first message in the thread which should contain the PR URL
List<net.dv8tion.jda.api.entities.Message> 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<Void> mentionReviewersOnAllReviewChannels(JDA jda) {
return CompletableFuture.runAsync(() -> {
Guild guild = jda.getGuildById(this.appConfig.guildId);
Expand All @@ -239,7 +267,7 @@ public CompletableFuture<Void> mentionReviewersOnAllReviewChannels(JDA jda) {
for (ForumChannel forumChannel : guild.getForumChannels()) {
for (ThreadChannel threadChannel : forumChannel.getThreadChannels()) {
Result<GitHubPullRequest, IllegalArgumentException> result =
GitHubPullRequest.fromUrl(threadChannel.getName());
this.getPullRequestFromThread(threadChannel);

if (result.isErr()) {
continue;
Expand Down Expand Up @@ -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<GitHubPullRequest, IllegalArgumentException> result =
this.getPullRequestFromThread(threadChannel);

if (result.isOk() && url.equals(result.get().toUrl())) {
return true;
}
}
Expand All @@ -295,8 +326,7 @@ public CompletableFuture<Void> archiveMergedPullRequest(JDA jda) {

for (ForumChannel forumChannel : guild.getForumChannels()) {
for (ThreadChannel threadChannel : forumChannel.getThreadChannels()) {
String name = threadChannel.getName();
Result<GitHubPullRequest, IllegalArgumentException> result = GitHubPullRequest.fromUrl(name);
Result<GitHubPullRequest, IllegalArgumentException> result = this.getPullRequestFromThread(threadChannel);

if (result.isErr()) {
continue;
Expand Down
Loading