|
| 1 | +package io.quarkus.bot; |
| 2 | + |
| 3 | +import io.quarkiverse.githubapp.event.IssueComment; |
| 4 | +import io.quarkus.bot.command.Command; |
| 5 | +import io.quarkus.bot.config.QuarkusBotConfig; |
| 6 | +import org.jboss.logging.Logger; |
| 7 | +import org.kohsuke.github.GHEventPayload; |
| 8 | +import org.kohsuke.github.GHIssue; |
| 9 | +import org.kohsuke.github.GHPermissionType; |
| 10 | +import org.kohsuke.github.GHPullRequest; |
| 11 | +import org.kohsuke.github.GHRepository; |
| 12 | +import org.kohsuke.github.GHUser; |
| 13 | +import org.kohsuke.github.ReactionContent; |
| 14 | + |
| 15 | +import javax.enterprise.inject.Instance; |
| 16 | +import javax.inject.Inject; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +public class PullRequestCommandHandler { |
| 23 | + |
| 24 | + private static final Logger LOG = Logger.getLogger(PullRequestCommandHandler.class); |
| 25 | + |
| 26 | + private static final String QUARKUS_BOT_NAME = "quarkus-bot[bot]"; |
| 27 | + private static final Pattern QUARKUS_BOT_MENTION = Pattern.compile("^@(?:quarkus-?)?bot\\s+([a-z _\\-]+)"); |
| 28 | + |
| 29 | + @Inject |
| 30 | + Instance<Command<GHPullRequest>> commands; |
| 31 | + |
| 32 | + @Inject |
| 33 | + QuarkusBotConfig quarkusBotConfig; |
| 34 | + |
| 35 | + public void onComment(@IssueComment.Created @IssueComment.Edited GHEventPayload.IssueComment commentPayload) |
| 36 | + throws IOException { |
| 37 | + GHUser user = commentPayload.getComment().getUser(); |
| 38 | + GHIssue issue = commentPayload.getIssue(); |
| 39 | + GHRepository repository = commentPayload.getRepository(); |
| 40 | + |
| 41 | + if (QUARKUS_BOT_NAME.equals(commentPayload.getComment().getUserName())) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (issue.isPullRequest()) { |
| 46 | + Optional<Command<GHPullRequest>> command = extractCommand(commentPayload.getComment().getBody()); |
| 47 | + if (command.isPresent() && canRunCommand(repository, user)) { |
| 48 | + GHPullRequest pullRequest = repository.getPullRequest(issue.getNumber()); |
| 49 | + ReactionContent reactionResult = command.get().run(pullRequest); |
| 50 | + postReaction(commentPayload, issue, reactionResult); |
| 51 | + } else { |
| 52 | + postReaction(commentPayload, issue, ReactionContent.MINUS_ONE); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void postReaction(GHEventPayload.IssueComment comment, GHIssue issue, ReactionContent reactionResult) throws IOException { |
| 58 | + if (!quarkusBotConfig.isDryRun()) { |
| 59 | + comment.getComment().createReaction(reactionResult); |
| 60 | + } else { |
| 61 | + LOG.info("Pull Request #" + issue.getNumber() + " - Add reaction: " + reactionResult.getContent()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private Optional<Command<GHPullRequest>> extractCommand(String comment) { |
| 66 | + Matcher matcher = QUARKUS_BOT_MENTION.matcher(comment); |
| 67 | + if (matcher.matches()) { |
| 68 | + String commandLabel = matcher.group(1); |
| 69 | + return commands.stream().filter(command -> command.labels().contains(commandLabel)).findFirst(); |
| 70 | + } |
| 71 | + return Optional.empty(); |
| 72 | + } |
| 73 | + |
| 74 | + private boolean canRunCommand(GHRepository repository, GHUser user) throws IOException { |
| 75 | + return repository.getPermission(user) == GHPermissionType.WRITE || repository.getPermission(user) == GHPermissionType.ADMIN; |
| 76 | + } |
| 77 | +} |
0 commit comments