Skip to content

Commit

Permalink
Release comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Aug 6, 2024
1 parent e98f7eb commit 65b8f9e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'application'
id 'com.apollographql.apollo' version '2.5.9'
id 'com.apollographql.apollo' version '2.5.14'
id 'com.github.johnrengelman.shadow' version '7.+'
}

Expand Down
4 changes: 4 additions & 0 deletions run/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ gitHubAppId=962689
gitHubAppKey=file:P:/Keys/neoforged-automation.2024-08-06.private-key.pem
gitHubAppOrganization=neoforged

releasesGitHubAppId=962891
releasesGitHubAppKey=file:P:/Keys/neoforged-releases.2024-08-06.private-key.pem
releasesGitHubAppOrganization=neoforged

configurationLocation=neoforged/actions:reactionable.yml@main
6 changes: 5 additions & 1 deletion src/main/graphql/com/github/api/fragments.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fragment IssueInfo on Issue {
number
}

fragment PullRequestInfo on PullRequest {
mergeable
number
Expand All @@ -11,7 +15,7 @@ fragment PullRequestInfo on PullRequest {
}
closingIssuesReferences(first: 100) {
nodes {
number
...IssueInfo
}
}
projectItems(first: 100) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/net/neoforged/automation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.neoforged.automation.webhook.handler.ConfigurationUpdateHandler;
import net.neoforged.automation.webhook.handler.LabelLockHandler;
import net.neoforged.automation.webhook.handler.MergeConflictCheckHandler;
import net.neoforged.automation.webhook.handler.ReleaseMessageHandler;
import net.neoforged.automation.webhook.impl.GitHubEvent;
import net.neoforged.automation.webhook.impl.WebhookHandler;
import org.kohsuke.github.GitHubBuilder;
Expand All @@ -29,7 +30,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept

var location = Configuration.load(gitHub, startupConfig);

var webhook = setupWebhookHandlers(new WebhookHandler(startupConfig, gitHub), location);
var webhook = setupWebhookHandlers(startupConfig, new WebhookHandler(startupConfig, gitHub), location);

var app = Javalin.create(cfg -> {
cfg.useVirtualThreads = true;
Expand All @@ -38,10 +39,17 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
.start(startupConfig.getInt("port", 8080));
}

public static WebhookHandler setupWebhookHandlers(WebhookHandler handler, Configuration.RepoLocation location) {
public static WebhookHandler setupWebhookHandlers(StartupConfiguration startupConfig, WebhookHandler handler, Configuration.RepoLocation location) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
return handler
.register(new MergeConflictCheckHandler())
.registerHandler(GitHubEvent.PUSH, new ConfigurationUpdateHandler(location))
.registerHandler(GitHubEvent.STATUS, new ReleaseMessageHandler(new GitHubBuilder()
.withAuthorizationProvider(AuthUtil.githubApp(
startupConfig.get("releasesGitHubAppId", ""),
AuthUtil.parsePKCS8(startupConfig.get("releasesGitHubAppKey", "")),
ghApp -> ghApp.getInstallationByOrganization(startupConfig.get("releasesGitHubAppOrganization", ""))
))
.build()))
.registerFilteredHandler(GitHubEvent.ISSUES, new LabelLockHandler(), GHAction.LABELED, GHAction.UNLABELED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.neoforged.automation.webhook.handler;

import com.github.api.GetPullRequestQuery;
import net.neoforged.automation.webhook.impl.EventHandler;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubAccessor;

import java.util.HashSet;
import java.util.regex.Pattern;

public record ReleaseMessageHandler(GitHub releasesApp) implements EventHandler<GHEventPayload.Status> {
private static final Pattern CLOSE_REFERENCE = Pattern.compile("(?mi)(?<type>(?:close|fix|resolve)(?:s|d|es|ed)?) #(?<number>\\d+)");

@Override
public void handle(GitHub gitHub, GHEventPayload.Status payload) throws Exception {
if (payload.getState() == GHCommitState.SUCCESS && payload.getContext().equals("Publishing") && payload.getDescription().startsWith("Version: ")) {
var version = payload.getDescription().replace("Version: ", "");

var closedIssues = new HashSet<Integer>();
var baseIssueComment = "\uD83D\uDE80 This issue has been resolved in " + payload.getRepository().getName() + " version `" + version + "`.";

for (var pr : payload.getCommit().listPullRequests()) {
if (pr.getMergedAt() == null) return;

var repo = releasesApp.getRepository(payload.getRepository().getFullName());

baseIssueComment = "\uD83D\uDE80 This issue has been resolved in " + repo.getName() + " version `"
+ version + "`, as part of #" + pr.getNumber() + ".";

repo.getPullRequest(pr.getNumber())
.comment("\uD83D\uDE80 This PR has been released as " + repo.getName() + " version `" + version + "`.");

for (var issueNode : GitHubAccessor.graphQl(gitHub, GetPullRequestQuery.builder()
.name(payload.getRepository().getName())
.owner(payload.getRepository().getOwnerName())
.number(pr.getNumber())
.build())
.repository()
.pullRequest()
.fragments()
.pullRequestInfo()
.closingIssuesReferences()
.nodes()) {
repo.getIssue(issueNode.fragments().issueInfo().number())
.comment(baseIssueComment);
closedIssues.add(issueNode.fragments().issueInfo().number());
}
}

var matcher = CLOSE_REFERENCE.matcher(payload.getCommit().getCommitShortInfo().getMessage());
while (matcher.find()) {
var number = Integer.parseInt(matcher.group("number"));
if (closedIssues.add(number)) {
var repo = releasesApp.getRepository(payload.getRepository().getFullName());
repo.getIssue(number).comment(baseIssueComment);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class GitHubEvent<T extends GHEventPayload> {
public static final GitHubEvent<GHEventPayload.PullRequest> PULL_REQUEST = create("pull_request", GHEventPayload.PullRequest.class);
public static final GitHubEvent<GHEventPayload.IssueComment> ISSUE_COMMENT = create("issue_comment", GHEventPayload.IssueComment.class);
public static final GitHubEvent<GHEventPayload.Push> PUSH = create("push", GHEventPayload.Push.class);
public static final GitHubEvent<GHEventPayload.Status> STATUS = create("status", GHEventPayload.Status.class);

private final Class<T> type;
private GitHubEvent(Class<T> type) {
Expand Down

0 comments on commit 65b8f9e

Please sign in to comment.