-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change the pr merge method from command to message
1 parent
38fc40a
commit 5d841df
Showing
11 changed files
with
163 additions
and
133 deletions.
There are no files selected for viewing
122 changes: 0 additions & 122 deletions
122
src/main/java/cholog/discord/command/GithubPullRequestMergeCommand.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain/java/cholog/discord/SlashCommand.java → .../cholog/discord/command/SlashCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...g/discord/SlashCommandListenerMapper.java → ...d/command/SlashCommandListenerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../cholog/discord/SlashCommandRegistry.java → ...discord/command/SlashCommandRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/cholog/discord/message/GithubPullRequestMergeSubscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/cholog/discord/message/MergeSubscriptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/main/java/cholog/discord/message/MessageSubscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
6 changes: 3 additions & 3 deletions
6
...iscord/command/CommandsConfiguration.java → ...iscord/message/MessagesConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...g/discord/command/CommandsProperties.java → ...g/discord/message/MessagesProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|