From a9687b6994b66f602bda9b16b7a3a71ad1a5f0ca Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Fri, 24 Jan 2025 14:18:44 +0100 Subject: [PATCH] Split guarded branches handling. Triage is triggered by too many events... --- .../bot/PullRequestGuardedBranches.java | 64 +++++++++++++++++++ .../io/quarkus/bot/TriagePullRequest.java | 33 ++++------ 2 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 src/main/java/io/quarkus/bot/PullRequestGuardedBranches.java diff --git a/src/main/java/io/quarkus/bot/PullRequestGuardedBranches.java b/src/main/java/io/quarkus/bot/PullRequestGuardedBranches.java new file mode 100644 index 0000000..3669fac --- /dev/null +++ b/src/main/java/io/quarkus/bot/PullRequestGuardedBranches.java @@ -0,0 +1,64 @@ +package io.quarkus.bot; + +import java.io.IOException; + +import jakarta.inject.Inject; + +import org.jboss.logging.Logger; +import org.kohsuke.github.GHEventPayload; +import org.kohsuke.github.GHPullRequest; + +import io.quarkiverse.githubapp.ConfigFile; +import io.quarkiverse.githubapp.event.PullRequest; +import io.quarkus.bot.config.Feature; +import io.quarkus.bot.config.QuarkusGitHubBotConfig; +import io.quarkus.bot.config.QuarkusGitHubBotConfigFile; +import io.quarkus.bot.config.QuarkusGitHubBotConfigFile.GuardedBranch; +import io.quarkus.bot.util.GHIssues; +import io.quarkus.bot.util.Mentions; +import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient; + +class PullRequestGuardedBranches { + + private static final Logger LOG = Logger.getLogger(PullRequestGuardedBranches.class); + + @Inject + QuarkusGitHubBotConfig quarkusBotConfig; + + void triagePullRequest( + @PullRequest.Opened GHEventPayload.PullRequest pullRequestPayload, + @ConfigFile("quarkus-github-bot.yml") QuarkusGitHubBotConfigFile quarkusBotConfigFile, + DynamicGraphQLClient gitHubGraphQLClient) throws IOException { + if (!Feature.TRIAGE_ISSUES_AND_PULL_REQUESTS.isEnabled(quarkusBotConfigFile)) { + return; + } + + GHPullRequest pullRequest = pullRequestPayload.getPullRequest(); + Mentions mentions = new Mentions(); + + for (GuardedBranch guardedBranch : quarkusBotConfigFile.triage.guardedBranches) { + if (guardedBranch.ref.equals(pullRequest.getBase().getRef())) { + for (String mention : guardedBranch.notify) { + mentions.add(mention, guardedBranch.ref); + } + } + } + + if (mentions.isEmpty()) { + return; + } + + mentions.removeAlreadyParticipating(GHIssues.getParticipatingUsers(pullRequest, gitHubGraphQLClient)); + + if (mentions.isEmpty()) { + return; + } + + String comment = "/cc " + mentions.getMentionsString(); + if (!quarkusBotConfig.isDryRun()) { + pullRequest.comment(comment); + } else { + LOG.info("Pull Request #" + pullRequest.getNumber() + " - Add comment: " + comment); + } + } +} diff --git a/src/main/java/io/quarkus/bot/TriagePullRequest.java b/src/main/java/io/quarkus/bot/TriagePullRequest.java index 58fabc2..be6e64f 100644 --- a/src/main/java/io/quarkus/bot/TriagePullRequest.java +++ b/src/main/java/io/quarkus/bot/TriagePullRequest.java @@ -19,7 +19,6 @@ import io.quarkus.bot.config.Feature; import io.quarkus.bot.config.QuarkusGitHubBotConfig; import io.quarkus.bot.config.QuarkusGitHubBotConfigFile; -import io.quarkus.bot.config.QuarkusGitHubBotConfigFile.GuardedBranch; import io.quarkus.bot.config.QuarkusGitHubBotConfigFile.TriageRule; import io.quarkus.bot.util.GHIssues; import io.quarkus.bot.util.Mentions; @@ -49,6 +48,10 @@ void triagePullRequest( return; } + if (quarkusBotConfigFile.triage.rules.isEmpty()) { + return; + } + GHPullRequest pullRequest = pullRequestPayload.getPullRequest(); Set labels = new TreeSet<>(); Mentions mentions = new Mentions(); @@ -75,32 +78,20 @@ void triagePullRequest( } } - for (GuardedBranch guardedBranch : quarkusBotConfigFile.triage.guardedBranches) { - if (guardedBranch.ref.equals(pullRequest.getBase().getRef())) { - for (String mention : guardedBranch.notify) { - mentions.add(mention, guardedBranch.ref); - } - } - } - // remove from the set the labels already present on the pull request - if (!labels.isEmpty()) { - pullRequest.getLabels().stream().map(GHLabel::getName).forEach(labels::remove); + pullRequest.getLabels().stream().map(GHLabel::getName).forEach(labels::remove); - if (!labels.isEmpty()) { - if (!quarkusBotConfig.isDryRun()) { - pullRequest.addLabels(limit(labels).toArray(new String[0])); - } else { - LOG.info("Pull Request #" + pullRequest.getNumber() + " - Add labels: " + String.join(", ", limit(labels))); - } + if (!labels.isEmpty()) { + if (!quarkusBotConfig.isDryRun()) { + pullRequest.addLabels(limit(labels).toArray(new String[0])); + } else { + LOG.info("Pull Request #" + pullRequest.getNumber() + " - Add labels: " + String.join(", ", limit(labels))); } } + mentions.removeAlreadyParticipating(GHIssues.getParticipatingUsers(pullRequest, gitHubGraphQLClient)); if (!mentions.isEmpty()) { - mentions.removeAlreadyParticipating(GHIssues.getParticipatingUsers(pullRequest, gitHubGraphQLClient)); - if (!mentions.isEmpty()) { - comments.add("/cc " + mentions.getMentionsString()); - } + comments.add("/cc " + mentions.getMentionsString()); } for (String comment : comments) {