diff --git a/src/main/java/net/neoforged/automation/Configuration.java b/src/main/java/net/neoforged/automation/Configuration.java index 0e1d9d0..b136596 100644 --- a/src/main/java/net/neoforged/automation/Configuration.java +++ b/src/main/java/net/neoforged/automation/Configuration.java @@ -18,11 +18,11 @@ public record Configuration( PRActions prActions, Map repositories ) { - public record RepoConfiguration(Boolean enabled, Map labelLocks, List formattingTasks) { + public record RepoConfiguration(Boolean enabled, Map labelLocks, @Nullable String baseRunCommand, String runUploadPattern) { public RepoConfiguration { enabled = enabled == null || enabled; } - public static final RepoConfiguration DEFAULT = new RepoConfiguration(true, Map.of(), List.of()); + public static final RepoConfiguration DEFAULT = new RepoConfiguration(true, Map.of(), null, null); } private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER).enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)); diff --git a/src/main/java/net/neoforged/automation/command/Commands.java b/src/main/java/net/neoforged/automation/command/Commands.java index 29faabb..4ec660e 100644 --- a/src/main/java/net/neoforged/automation/command/Commands.java +++ b/src/main/java/net/neoforged/automation/command/Commands.java @@ -2,41 +2,48 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; 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; +import java.util.Arrays; +import java.util.stream.Collectors; + public class Commands { public static CommandDispatcher register(CommandDispatcher dispatcher) { - dispatcher.register(literal("applyFormatting") + dispatcher.register(literal("run") .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); + .then(argument("tasks", StringArgumentType.greedyString())) + .executes(FunctionalInterfaces.throwingCommand(context -> { + var pr = context.getSource().pullRequest(); + var config = Configuration.get(); + var repoConfig = Configuration.get(context.getSource().repository()); + if (repoConfig.baseRunCommand() != null) { + var tasks = context.getArgument("tasks", String.class).trim(); + var comment = pr.comment("Running Gradle tasks `" + tasks + "`..."); + FormattingCommand.run( + context.getSource().gitHub(), pr, + config.prActions(), repoConfig, + Arrays.stream(tasks.split(" ")).map(t -> "./gradlew " + t).collect(Collectors.joining(" && ")), + 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); } - }, () -> { - context.getSource().onSuccess().run(); - FunctionalInterfaces.ignoreExceptions(comment::delete); - } - ); - } - return GHCommandContext.DEFERRED_RESPONSE; - }))); + ); + } + return GHCommandContext.DEFERRED_RESPONSE; + }))); return dispatcher; } diff --git a/src/main/java/net/neoforged/automation/command/FormattingCommand.java b/src/main/java/net/neoforged/automation/command/FormattingCommand.java index 871cf63..4ca9409 100644 --- a/src/main/java/net/neoforged/automation/command/FormattingCommand.java +++ b/src/main/java/net/neoforged/automation/command/FormattingCommand.java @@ -17,10 +17,10 @@ public class FormattingCommand { - public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions actions, Configuration.RepoConfiguration repoConfiguration, Consumer onFailure, Runnable onSuccess) throws IOException { + public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions actions, Configuration.RepoConfiguration repoConfiguration, String command, Consumer onFailure, Runnable onSuccess) throws IOException { PRActionRunner.builder(pr) - .upload("src*/main/java/") - .command(repoConfiguration.formattingTasks()) + .upload(repoConfiguration.runUploadPattern()) + .command(repoConfiguration.baseRunCommand(), command) .onFailed((gitHub, run) -> onFailure.accept(run)) .onFinished((gitHub, run, artifact) -> { PRRunUtils.setupPR(pr, (dir, git) -> {