Skip to content

Commit

Permalink
change the pr merge method from command to message
Browse files Browse the repository at this point in the history
jaeyeonling committed Jul 10, 2024
1 parent 38fc40a commit 5d841df
Showing 11 changed files with 163 additions and 133 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/cholog/discord/command/PingPongCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cholog.discord.command;

import cholog.discord.SlashCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.stereotype.Component;

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cholog.discord;
package cholog.discord.command;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cholog.discord;
package cholog.discord.command;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cholog.discord;
package cholog.discord.command;

import jakarta.annotation.PostConstruct;
import net.dv8tion.jda.api.entities.Guild;
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cholog.discord.message;

import cholog.github.GithubApi;
import cholog.github.PullRequest;
import cholog.github.PullRequestMergeRequest;
import cholog.github.PullRequestUrl;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

@Component
public final class GithubPullRequestMergeSubscription implements MessageSubscription {

private static final Logger log = LoggerFactory.getLogger(GithubPullRequestMergeSubscription.class);

private final MessagesProperties properties;

private final GithubApi githubApi;

public GithubPullRequestMergeSubscription(
final MessagesProperties properties,
final GithubApi githubApi
) {
this.properties = properties;
this.githubApi = githubApi;
}

@Override
public String channelId() {
return properties.prMergeChannelId();
}

@Override
public void onEvent(final MessageReceivedEvent event) {
final var message = event.getMessage();
final var urls = Arrays.stream(message.getContentRaw().split("\\s+"))
.map(String::trim)
.filter(it -> it.startsWith("http:"))
.toList();

final var fails = new HashMap<String, String>();
for (final String url : urls) {
final var pullRequestUrl = new PullRequestUrl(url);

if (properties.isDisallowOrganization(pullRequestUrl.owner())) {
fails.put(url, pullRequestUrl.owner() + "는 허용하지 않는 조직입니다.");
continue;
}
if (properties.isDisallowRepository(pullRequestUrl.repository())) {
fails.put(url, pullRequestUrl.repository() + "는 허용하지 않는 저장소입니다.");
continue;
}

final var pullRequest = githubApi.getPullRequest(
pullRequestUrl.owner(),
pullRequestUrl.repository(),
pullRequestUrl.pullNumber()
);
if (pullRequest.state() == PullRequest.PullRequestState.CLOSED) {
fails.put(url, "이미 닫힌 PR입니다.");
continue;
}

final var targetBranch = pullRequest.base().ref();
if (properties.isDisallowBranch(targetBranch)) {
fails.put(url, targetBranch + "브랜치에는 머지할 수 없습니다.");
return;
}

final var mergeRequest = new PullRequestMergeRequest(
"고생하셨습니다.",
":tada: PR 머지 완료! :tada:",
pullRequest.head().sha(),
PullRequestMergeRequest.MergeMethod.SQUASH
);
final var mergeResult = githubApi.mergePullRequest(
pullRequestUrl.owner(),
pullRequestUrl.repository(),
pullRequestUrl.pullNumber(),
mergeRequest
);
if (!mergeResult.merged()) {
fails.put(url, mergeResult.message());
}
}

final var doneEmoji = Objects.requireNonNull(message.getJDA().getEmojiById(":done:"));
message.addReaction(doneEmoji).queue();
if (!fails.isEmpty()) {
final var failMessages = fails.entrySet().stream()
.map(entry -> entry.getKey() + ": " + entry.getValue())
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
message.reply(failMessages).queue();
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/cholog/discord/message/MergeSubscriptionMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cholog.discord.message;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

@Component
public final class MergeSubscriptionMapper extends ListenerAdapter {

private static final Logger log = LoggerFactory.getLogger(MergeSubscriptionMapper.class);

private final Map<String, MessageSubscription> messageSubscriptions;

MergeSubscriptionMapper(final List<MessageSubscription> messageSubscriptions) {
this.messageSubscriptions = messageSubscriptions.stream()
.collect(toMap(MessageSubscription::channelId, command -> command));
}

@Override
public void onMessageReceived(@NonNull final MessageReceivedEvent event) {
if (event.getAuthor().isBot()) {
log.debug("Bot message");
return;
}

final var channelId = event.getChannel().getId();
final var messageSubscription = messageSubscriptions.get(channelId);
if (messageSubscription == null) {
log.debug("Message subscription not found [channelId={}, message={}]", channelId, event.getMessage());
return;
}
messageSubscription.onEvent(event);
}
}
10 changes: 10 additions & 0 deletions src/main/java/cholog/discord/message/MessageSubscription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cholog.discord.message;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

public interface MessageSubscription {

String channelId();

void onEvent(MessageReceivedEvent event);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cholog.discord.command;
package cholog.discord.message;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties(CommandsProperties.class)
@EnableConfigurationProperties(MessagesProperties.class)
@Configuration
public class CommandsConfiguration {
public class MessagesConfiguration {

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cholog.discord.command;
package cholog.discord.message;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties(prefix = "cholog.jda.commands")
public record CommandsProperties(
@ConfigurationProperties(prefix = "cholog.jda.subscriptions")
public record MessagesProperties(
PrMergeProperties prMerge
) {

2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ cholog:
jda:
token:
guild-id:
commands:
subscriptions:
pr-merge:
channel-id:
allow-organizations:

0 comments on commit 5d841df

Please sign in to comment.