Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split guarded branches handling. #516

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading