Skip to content

Commit

Permalink
Merge pull request #516 from gsmet/split-guarded-branches
Browse files Browse the repository at this point in the history
Split guarded branches handling.
  • Loading branch information
gsmet authored Jan 24, 2025
2 parents 86b08d9 + a9687b6 commit 0b8144e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
64 changes: 64 additions & 0 deletions src/main/java/io/quarkus/bot/PullRequestGuardedBranches.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 12 additions & 21 deletions src/main/java/io/quarkus/bot/TriagePullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +48,10 @@ void triagePullRequest(
return;
}

if (quarkusBotConfigFile.triage.rules.isEmpty()) {
return;
}

GHPullRequest pullRequest = pullRequestPayload.getPullRequest();
Set<String> labels = new TreeSet<>();
Mentions mentions = new Mentions();
Expand All @@ -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) {
Expand Down

0 comments on commit 0b8144e

Please sign in to comment.