From 5473e86be1331e8fc859304ed6f4f8563e0f84a0 Mon Sep 17 00:00:00 2001 From: Guillaume Le Floch Date: Thu, 22 Apr 2021 21:17:32 +0200 Subject: [PATCH] Remove invalid label when issues or pull requests are reopen --- .../bot/RemoveInvalidLabelOnReopenAction.java | 47 +++++++++++++++++++ .../io/quarkus/bot/util/GHPullRequests.java | 16 +++++++ 2 files changed, 63 insertions(+) create mode 100644 src/main/java/io/quarkus/bot/RemoveInvalidLabelOnReopenAction.java create mode 100644 src/main/java/io/quarkus/bot/util/GHPullRequests.java diff --git a/src/main/java/io/quarkus/bot/RemoveInvalidLabelOnReopenAction.java b/src/main/java/io/quarkus/bot/RemoveInvalidLabelOnReopenAction.java new file mode 100644 index 0000000..d15c6f5 --- /dev/null +++ b/src/main/java/io/quarkus/bot/RemoveInvalidLabelOnReopenAction.java @@ -0,0 +1,47 @@ +package io.quarkus.bot; + +import io.quarkiverse.githubapp.event.Issue; +import io.quarkiverse.githubapp.event.PullRequest; +import io.quarkus.bot.config.QuarkusBotConfig; +import io.quarkus.bot.util.GHIssues; +import io.quarkus.bot.util.GHPullRequests; +import io.quarkus.bot.util.Labels; +import org.jboss.logging.Logger; +import org.kohsuke.github.GHEventPayload; +import org.kohsuke.github.GHIssue; +import org.kohsuke.github.GHPullRequest; + +import javax.inject.Inject; +import java.io.IOException; + +class RemoveInvalidLabelOnReopenAction { + private static final Logger LOG = Logger.getLogger(RemoveInvalidLabelOnReopenAction.class); + + @Inject + QuarkusBotConfig quarkusBotConfig; + + public void onIssueReopen(@Issue.Reopened GHEventPayload.Issue issuePayload) throws IOException { + GHIssue issue = issuePayload.getIssue(); + + if (GHIssues.hasLabel(issue, Labels.TRIAGE_INVALID)) { + if (!quarkusBotConfig.isDryRun()) { + issue.removeLabel(Labels.TRIAGE_INVALID); + } else { + LOG.info("Issue #" + issue.getNumber() + " - Remove label: " + Labels.TRIAGE_INVALID); + } + } + } + + public void onPullRequestReopen(@PullRequest.Reopened GHEventPayload.PullRequest pullRequestPayload) throws IOException { + GHPullRequest pullRequest = pullRequestPayload.getPullRequest(); + + if (GHPullRequests.hasLabel(pullRequest, Labels.TRIAGE_INVALID)) { + if (!quarkusBotConfig.isDryRun()) { + pullRequest.removeLabel(Labels.TRIAGE_INVALID); + } else { + LOG.info("Pull request #" + pullRequest.getNumber() + " - Remove label: " + Labels.TRIAGE_INVALID); + } + } + } + +} diff --git a/src/main/java/io/quarkus/bot/util/GHPullRequests.java b/src/main/java/io/quarkus/bot/util/GHPullRequests.java new file mode 100644 index 0000000..5b5ef80 --- /dev/null +++ b/src/main/java/io/quarkus/bot/util/GHPullRequests.java @@ -0,0 +1,16 @@ +package io.quarkus.bot.util; + +import org.kohsuke.github.GHLabel; +import org.kohsuke.github.GHPullRequest; + +public final class GHPullRequests { + + public static boolean hasLabel(GHPullRequest pullRequest, String labelName) { + for (GHLabel label : pullRequest.getLabels()) { + if (labelName.equals(label.getName())) { + return true; + } + } + return false; + } +}