Skip to content

Commit

Permalink
Work on command run tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Aug 11, 2024
1 parent 087578f commit 7b2e9c2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/neoforged/automation/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public record Configuration(
PRActions prActions,
Map<String, RepoConfiguration> repositories
) {
public record RepoConfiguration(Boolean enabled, Map<String, LabelLock> labelLocks, List<String> formattingTasks) {
public record RepoConfiguration(Boolean enabled, Map<String, LabelLock> 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));
Expand Down
57 changes: 32 additions & 25 deletions src/main/java/net/neoforged/automation/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GHCommandContext> register(CommandDispatcher<GHCommandContext> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

public class FormattingCommand {

public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions actions, Configuration.RepoConfiguration repoConfiguration, Consumer<GHWorkflowRun> onFailure, Runnable onSuccess) throws IOException {
public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions actions, Configuration.RepoConfiguration repoConfiguration, String command, Consumer<GHWorkflowRun> 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) -> {
Expand Down

0 comments on commit 7b2e9c2

Please sign in to comment.