-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15996fa
commit 087578f
Showing
20 changed files
with
634 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mutation MinimizeComment($reason: ReportedContentClassifiers!, $comment: ID!) { | ||
minimizeComment(input: {classifier: $reason, subjectId: $comment}) { | ||
minimizedComment { | ||
isMinimized | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/net/neoforged/automation/command/Commands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.neoforged.automation.command; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | ||
import net.neoforged.automation.Configuration; | ||
import net.neoforged.automation.command.api.GHCommandContext; | ||
import net.neoforged.automation.util.FunctionalInterfaces; | ||
|
||
public class Commands { | ||
public static CommandDispatcher<GHCommandContext> register(CommandDispatcher<GHCommandContext> dispatcher) { | ||
dispatcher.register(literal("applyFormatting") | ||
.requires(Requirement.IS_PR.and(Requirement.IS_MAINTAINER.or(Requirement.IS_PR))) | ||
.executes(FunctionalInterfaces.throwingCommand(context -> { | ||
var pr = context.getSource().pullRequest(); | ||
var config = Configuration.get(); | ||
var repoConfig = Configuration.get(context.getSource().repository()); | ||
if (!repoConfig.formattingTasks().isEmpty()) { | ||
var comment = pr.comment("Applying formatting..."); | ||
FormattingCommand.run( | ||
context.getSource().gitHub(), pr, | ||
config.prActions(), repoConfig, | ||
err -> { | ||
context.getSource().onError().run(); | ||
try { | ||
context.getSource().issue() | ||
.comment("Workflow failed: " + err.getHtmlUrl()); | ||
} catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
}, () -> { | ||
context.getSource().onSuccess().run(); | ||
FunctionalInterfaces.ignoreExceptions(comment::delete); | ||
} | ||
); | ||
} | ||
return GHCommandContext.DEFERRED_RESPONSE; | ||
}))); | ||
|
||
return dispatcher; | ||
} | ||
|
||
private static ExtendedLiteralArgumentBuilder<GHCommandContext> literal(String name) { | ||
return new ExtendedLiteralArgumentBuilder<>(name); | ||
} | ||
|
||
private static <T> RequiredArgumentBuilder<GHCommandContext, T> argument(String name, ArgumentType<T> type) { | ||
return RequiredArgumentBuilder.argument(name, type); | ||
} | ||
|
||
private static class ExtendedLiteralArgumentBuilder<T> extends LiteralArgumentBuilder<T> { | ||
|
||
protected ExtendedLiteralArgumentBuilder(String literal) { | ||
super(literal); | ||
} | ||
|
||
public LiteralArgumentBuilder<T> requires(FunctionalInterfaces.PredException<T> requirement) { | ||
return this.requires(FunctionalInterfaces.wrapPred(requirement)); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/net/neoforged/automation/command/FormattingCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.neoforged.automation.command; | ||
|
||
import net.neoforged.automation.Configuration; | ||
import net.neoforged.automation.runner.PRActionRunner; | ||
import net.neoforged.automation.runner.PRRunUtils; | ||
import org.eclipse.jgit.transport.RefSpec; | ||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.kohsuke.github.GHWorkflowRun; | ||
import org.kohsuke.github.GitHub; | ||
import org.kohsuke.github.GitHubAccessor; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.function.Consumer; | ||
import java.util.zip.ZipFile; | ||
|
||
public class FormattingCommand { | ||
|
||
public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions actions, Configuration.RepoConfiguration repoConfiguration, Consumer<GHWorkflowRun> onFailure, Runnable onSuccess) throws IOException { | ||
PRActionRunner.builder(pr) | ||
.upload("src*/main/java/") | ||
.command(repoConfiguration.formattingTasks()) | ||
.onFailed((gitHub, run) -> onFailure.accept(run)) | ||
.onFinished((gitHub, run, artifact) -> { | ||
PRRunUtils.setupPR(pr, (dir, git) -> { | ||
try (var file = new ZipFile(artifact.toFile())) { | ||
var enm = file.entries(); | ||
while (enm.hasMoreElements()) { | ||
var entry = enm.nextElement(); | ||
Files.write(dir.resolve(entry.getName()), file.getInputStream(entry).readAllBytes()); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
git.add().addFilepattern(".").call(); | ||
|
||
var botName = gitHub.getApp().getSlug() + "[bot]"; | ||
var user = gitHub.getUser(botName); | ||
var creds = new UsernamePasswordCredentialsProvider( | ||
botName, | ||
GitHubAccessor.getToken(gitHub) | ||
); | ||
|
||
git.commit().setCredentialsProvider(creds) | ||
.setCommitter(botName, user.getId() + "+" + botName + "@users.noreply.github.com") | ||
.setMessage("Update formatting") | ||
.setSign(false) | ||
.setNoVerify(true) | ||
.call(); | ||
git.push().setRemote("origin").setRefSpecs(new RefSpec("HEAD:refs/heads/" + pr.getHead().getRef())).setCredentialsProvider(creds).call(); | ||
onSuccess.run(); | ||
}); | ||
}) | ||
.build() | ||
.queue(gh, actions); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/net/neoforged/automation/command/Requirement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.neoforged.automation.command; | ||
|
||
import net.neoforged.automation.command.api.GHCommandContext; | ||
import net.neoforged.automation.util.FunctionalInterfaces; | ||
import org.kohsuke.github.GHPermissionType; | ||
|
||
public enum Requirement implements FunctionalInterfaces.PredException<GHCommandContext> { | ||
IS_PR(ctx -> ctx.issue().isPullRequest()), | ||
IS_MAINTAINER(ctx -> ctx.repository().hasPermission(ctx.user(), GHPermissionType.WRITE)), | ||
IS_AUTHOR(ctx -> ctx.user().equals(ctx.issue().getUser())); | ||
|
||
private final FunctionalInterfaces.PredException<GHCommandContext> test; | ||
|
||
Requirement(FunctionalInterfaces.PredException<GHCommandContext> test) { | ||
this.test = test; | ||
} | ||
|
||
@Override | ||
public boolean test(GHCommandContext ghCommandContext) throws Exception { | ||
return test.test(ghCommandContext); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/net/neoforged/automation/command/api/GHCommandContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.neoforged.automation.command.api; | ||
|
||
import org.kohsuke.github.GHEventPayload; | ||
import org.kohsuke.github.GHIssue; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GHUser; | ||
import org.kohsuke.github.GitHub; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Consumer; | ||
|
||
public record GHCommandContext(GitHub gitHub, GHEventPayload.IssueComment payload, Runnable onError, Runnable onSuccess) { | ||
public static final int DEFERRED_RESPONSE = 2; | ||
|
||
public GHUser user() { | ||
return payload.getSender(); | ||
} | ||
|
||
public GHRepository repository() { | ||
return issue().getRepository(); | ||
} | ||
|
||
public GHIssue issue() { | ||
return payload.getIssue(); | ||
} | ||
|
||
public GHPullRequest pullRequest() throws IOException { | ||
return repository().getPullRequest(issue().getNumber()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/net/neoforged/automation/runner/ActionFailedCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.neoforged.automation.runner; | ||
|
||
import org.kohsuke.github.GHWorkflowRun; | ||
import org.kohsuke.github.GitHub; | ||
|
||
@FunctionalInterface | ||
public interface ActionFailedCallback { | ||
void onFailed(GitHub gitHub, GHWorkflowRun run) throws Exception; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/neoforged/automation/runner/ActionFinishedCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.neoforged.automation.runner; | ||
|
||
import org.kohsuke.github.GHWorkflowRun; | ||
import org.kohsuke.github.GitHub; | ||
|
||
import java.nio.file.Path; | ||
|
||
@FunctionalInterface | ||
public interface ActionFinishedCallback { | ||
void onFinished(GitHub gitHub, GHWorkflowRun run, Path artifact) throws Exception; | ||
} |
Oops, something went wrong.