Skip to content

Commit

Permalink
Add a label to keep a PR blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jan 12, 2025
1 parent c8bff53 commit ecb77f0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public void register(WebhookHandler web) {
case UNLABELED -> handler.onLabelRemoved(gitHub, payload.getSender(), payload.getPullRequest(), payload.getLabel());
}
}, GHAction.LABELED, GHAction.UNLABELED);

web.registerFilteredHandler(GitHubEvent.PULL_REQUEST, (gitHub, payload, action) -> {
var pr = payload.getPullRequest();

var config = Configuration.get(payload.getRepository()).labelHandlers();

for (GHLabel label : pr.getLabels()) {
var handler = config.get(label.getName());
if (handler != null) {
handler.onSynchronized(gitHub, pr, label);
}
}
}, GHAction.SYNCHRONIZE);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.neoforged.automation.webhook.label;

import org.kohsuke.github.GHCheckRun;
import org.kohsuke.github.GHCheckRunBuilder;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;

public record BlockPRHandler() implements LabelHandler {
private static final String NAME = "PR Block";

@Override
public void onLabelAdded(GitHub gitHub, GHUser actor, GHIssue issue, GHLabel label) throws Exception {
if (issue.getPullRequest() != null) {
block(issue.getRepository().getPullRequest(issue.getNumber()));
}
}

@Override
public void onLabelRemoved(GitHub gitHub, GHUser actor, GHIssue issue, GHLabel label) throws Exception {
if (issue.getPullRequest() != null) {
var pr = issue.getRepository().getPullRequest(issue.getNumber());
issue.getRepository()
.createCheckRun(NAME, pr.getHead().getSha())
.withConclusion(GHCheckRun.Conclusion.SUCCESS)
.add(new GHCheckRunBuilder.Output("PR block label removed", "PR block label removed"))
.create();
}
}

@Override
public void onSynchronized(GitHub gitHub, GHPullRequest pullRequest, GHLabel label) throws Exception {
block(pullRequest);
}

private void block(GHPullRequest pr) throws Exception {
pr.getRepository().createCheckRun(NAME, pr.getHead().getSha())
.withConclusion(GHCheckRun.Conclusion.FAILURE)
.add(new GHCheckRunBuilder.Output("PR blocked as requested through label", "PR blocked"))
.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;

Expand All @@ -17,7 +18,8 @@ public interface LabelHandler {
Map<String, Class<? extends LabelHandler>> TYPES = Map.of(
"lock", LockLabelHandler.class,
"merge", MergeLabelHandler.class,
"keep-rebased", KeepRebasedHandler.class
"keep-rebased", KeepRebasedHandler.class,
"block", BlockPRHandler.class
);

default void onLabelAdded(GitHub gitHub, GHUser actor, GHIssue issue, GHLabel label) throws Exception {
Expand All @@ -28,6 +30,10 @@ default void onLabelRemoved(GitHub gitHub, GHUser actor, GHIssue issue, GHLabel

}

default void onSynchronized(GitHub gitHub, GHPullRequest pullRequest, GHLabel label) throws Exception {

}

final class Deserializer extends JsonDeserializer<LabelHandler> {
@Override
public LabelHandler deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Expand Down

0 comments on commit ecb77f0

Please sign in to comment.