From d2630117b82b9c04726c544a1a6a96ff38f02384 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 9 Jun 2022 17:59:14 -0300 Subject: [PATCH] Affect `triage/needs-rebase` on Pull Requests with merge conflicts This adds the `triage/needs-rebase` label (or removes it) on opened pull-requests, making it easier to find pull-requests that require a rebase to be merged Apply suggestions from code review Co-authored-by: Guillaume Smet --- README.adoc | 5 ++ ...AffectNeedsRebaseToOpenedPullRequests.java | 51 +++++++++++++++++++ src/main/java/io/quarkus/bot/util/Labels.java | 1 + 3 files changed, 57 insertions(+) create mode 100644 src/main/java/io/quarkus/bot/AffectNeedsRebaseToOpenedPullRequests.java diff --git a/README.adoc b/README.adoc index 0f78f3f..51bd0fa 100644 --- a/README.adoc +++ b/README.adoc @@ -167,6 +167,11 @@ The bot will automatically remove these labels when they are outdated: The bot enforces a specific color for any label created that starts with `area/` so that all these labels are consistent. +=== Include `triage/needs-rebase` on unmergeable pull-requests + +Pull requests may require a rebase when conflicts appear. The bot adds a `triage/needs-rebase` label on pull requests that cannot be merged and removes it when the pull request is in a mergeable state again. + + == Contributing To participate to the development of this GitHub App, create a playground project in your own org and diff --git a/src/main/java/io/quarkus/bot/AffectNeedsRebaseToOpenedPullRequests.java b/src/main/java/io/quarkus/bot/AffectNeedsRebaseToOpenedPullRequests.java new file mode 100644 index 0000000..58700f8 --- /dev/null +++ b/src/main/java/io/quarkus/bot/AffectNeedsRebaseToOpenedPullRequests.java @@ -0,0 +1,51 @@ +package io.quarkus.bot; + +import io.quarkiverse.githubapp.event.PullRequest; +import io.quarkiverse.githubapp.event.Push; +import io.quarkus.bot.util.Labels; +import org.kohsuke.github.GHEventPayload; +import org.kohsuke.github.GHIssueState; +import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHRepository; + +import java.io.IOException; + +public class AffectNeedsRebaseToOpenedPullRequests { + + /** + * Affect triage/needs-rebase label on pull request events + */ + void triageNeedsRebaseOnPullRequest( + @PullRequest.Opened @PullRequest.Reopened @PullRequest.Synchronize GHEventPayload.PullRequest pullRequestPayload) + throws IOException { + affectNeedsRebaseLabel(pullRequestPayload.getPullRequest()); + } + + void triageNeedsRebaseOnRepositoryPush(@Push GHEventPayload.Push pushRequestPayload) throws IOException { + GHRepository repository = pushRequestPayload.getRepository(); + String defaultBranch = repository.getDefaultBranch(); + for (GHPullRequest pullRequest : repository.getPullRequests(GHIssueState.OPEN)) { + if (!defaultBranch.equals(pullRequest.getBase().getRef())) { + continue; + } + affectNeedsRebaseLabel(pullRequest); + } + } + + private void affectNeedsRebaseLabel(GHPullRequest pullRequest) throws IOException { + Boolean mergeable = pullRequest.getMergeable(); + boolean hasNeedsRebaseLabel = pullRequest.getLabels().stream() + .anyMatch(label -> Labels.TRIAGE_NEEDS_REBASE.equals(label.getName())); + if (mergeable != null) { + if (mergeable) { + if (hasNeedsRebaseLabel) { + pullRequest.removeLabels(Labels.TRIAGE_NEEDS_REBASE); + } + } else { + if (!hasNeedsRebaseLabel) { + pullRequest.addLabels(Labels.TRIAGE_NEEDS_REBASE); + } + } + } + } +} diff --git a/src/main/java/io/quarkus/bot/util/Labels.java b/src/main/java/io/quarkus/bot/util/Labels.java index 083cb45..bc71b8e 100644 --- a/src/main/java/io/quarkus/bot/util/Labels.java +++ b/src/main/java/io/quarkus/bot/util/Labels.java @@ -14,6 +14,7 @@ public class Labels { public static final String AREA_PREFIX = "area/"; public static final String AREA_INFRA = "area/infra"; public static final String TRIAGE_INVALID = "triage/invalid"; + public static final String TRIAGE_NEEDS_REBASE = "triage/needs-rebase"; public static final String TRIAGE_NEEDS_TRIAGE = "triage/needs-triage"; public static final String TRIAGE_WAITING_FOR_CI = "triage/waiting-for-ci"; public static final String TRIAGE_QE = "triage/qe?";