Skip to content
Permalink
Browse files
pr: add /integrate auto
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 18, 2021
1 parent a66ee40 commit 77cbb9292122f7cb619876eb94ecc5c8427c12b5
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 14 deletions.
@@ -310,6 +310,10 @@ public Collection<WorkItem> run(Path scratchPath) {
}
}

if (pr.labels().contains("auto") && pr.labels().contains("ready") && !pr.labels().contains("sponsor")) {
pr.addComment("/integrate\n" + PullRequestCommandWorkItem.VALID_BOT_COMMAND_MARKER);
}

// Must re-fetch PR after executing CheckRun
var updatedPR = pr.repository().pullRequest(pr.id());
return List.of(new PullRequestCommandWorkItem(bot, updatedPR, errorHandler));
@@ -40,6 +40,10 @@ public class IntegrateCommand implements CommandHandler {
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.pr");
private static final Pattern BACKPORT_PATTERN = Pattern.compile("<!-- backport ([0-9a-z]{40}) -->");

private void showHelp(PrintWriter reply) {
reply.println("usage: `/integrate [auto|manual|<hash>]`");
}

private Optional<String> checkProblem(Map<String, Check> performedChecks, String checkName, PullRequest pr) {
final var failure = "the status check `" + checkName + "` did not complete successfully";
final var inProgress = "the status check `" + checkName + "` is still in progress";
@@ -60,7 +64,7 @@ private Optional<String> checkProblem(Map<String, Check> performedChecks, String

@Override
public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInstance, Path scratchPath, CommandInvocation command, List<Comment> allComments, PrintWriter reply) {
if (!command.user().equals(pr.author())) {
if (!command.user().equals(pr.author()) && !command.user().equals(pr.repository().forge().currentUser())) {
reply.print("Only the author (@" + pr.author().username() + ") is allowed to issue the `integrate` command.");

// If the command author is allowed to sponsor this change, suggest that command
@@ -75,6 +79,36 @@ public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInst
return;
}

Hash targetHash = null;
if (!command.args().isEmpty()) {
var args = command.args().split(" ");
if (args.length != 1) {
showHelp(reply);
return;
}

var arg = args[0].trim();
if (arg.equals("auto")) {
pr.addLabel("auto");
reply.println("This pull request will be automatically integrated when it is ready");
return;
} else if (arg.equals("manual")) {
if (pr.labels().contains("auto")) {
pr.removeLabel("auto");
}
reply.println("This pull request will have to be integrated manually using the "+
"[/integrate](https://wiki.openjdk.java.net/display/SKARA/Pull+Request+Commands#PullRequestCommands-/integrate) pull request command.");
return;
} else {
// Validate the target hash if requested
targetHash = new Hash(arg);
if (!targetHash.isValid()) {
reply.println("The given argument, `" + arg + "`, is not a valid hash.");
return;
}
}
}

var problem = checkProblem(pr.checks(pr.headHash()), "jcheck", pr);
if (problem.isPresent()) {
reply.print("Your integration request cannot be fulfilled at this time, as ");
@@ -109,18 +143,14 @@ public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInst
bot.confOverrideName(),
bot.confOverrideRef());

// Validate the target hash if requested
var rebaseMessage = new StringWriter();
if (!command.args().isBlank()) {
var wantedHash = new Hash(command.args());
if (!PullRequestUtils.targetHash(pr, localRepo).equals(wantedHash)) {
reply.print("The head of the target branch is no longer at the requested hash " + wantedHash);
reply.println(" - it has moved to " + PullRequestUtils.targetHash(pr, localRepo) + ". Aborting integration.");
return;
}
};
if (targetHash != null && !PullRequestUtils.targetHash(pr, localRepo).equals(targetHash)) {
reply.print("The head of the target branch is no longer at the requested hash " + targetHash);
reply.println(" - it has moved to " + PullRequestUtils.targetHash(pr, localRepo) + ". Aborting integration.");
return;
}

// Now merge the latest changes from the target
var rebaseMessage = new StringWriter();
var rebaseWriter = new PrintWriter(rebaseMessage);
var rebasedHash = checkablePr.mergeTarget(rebaseWriter);
if (rebasedHash.isEmpty()) {
@@ -201,4 +231,9 @@ public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInst
public String description() {
return "performs integration of the changes in the PR";
}

@Override
public boolean allowedInBody() {
return true;
}
}
@@ -40,7 +40,6 @@ public class PullRequestCommandWorkItem extends PullRequestWorkItem {

private static final String commandReplyMarker = "<!-- Jmerge command reply message (%s) -->";
private static final Pattern commandReplyPattern = Pattern.compile("<!-- Jmerge command reply message \\((\\S+)\\) -->");
private static final String selfCommandMarker = "<!-- Valid self-command -->";
private final static Pattern pushedPattern = Pattern.compile("Pushed as commit ([a-f0-9]{40})\\.");

private static final Map<String, CommandHandler> commandHandlers = Map.ofEntries(
@@ -59,6 +58,8 @@ public class PullRequestCommandWorkItem extends PullRequestWorkItem {
Map.entry("clean", new CleanCommand())
);

public static final String VALID_BOT_COMMAND_MARKER = "<!-- Valid self-command -->";

static class HelpCommand implements CommandHandler {
@Override
public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInstance, Path scratchPath, CommandInvocation command, List<Comment> allComments, PrintWriter reply) {
@@ -116,7 +117,7 @@ private Optional<CommandInvocation> nextCommand(PullRequest pr, List<Comment> co
var body = PullRequestBody.parse(pr).bodyText();
var allCommands = Stream.concat(CommandExtractor.extractCommands(commandHandlers, body, "body", pr.author()).stream(),
comments.stream()
.filter(comment -> !comment.author().equals(self) || comment.body().endsWith(selfCommandMarker))
.filter(comment -> !comment.author().equals(self) || comment.body().endsWith(VALID_BOT_COMMAND_MARKER))
.flatMap(c -> CommandExtractor.extractCommands(commandHandlers, c.body(), c.id(), c.author()).stream()))
.collect(Collectors.toList());

1 comment on commit 77cbb92

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 77cbb92 Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.