Skip to content

Commit

Permalink
2138: Backport instructions should include suggested PR comment
Browse files Browse the repository at this point in the history
Reviewed-by: erikj
  • Loading branch information
zhaosongzs committed Jan 10, 2024
1 parent ef8238c commit b258994
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,40 @@ public void handle(PullRequestBot bot, HostedCommit commit, LimitedCensusInstanc
Hash backportHash;
var backportBranchName = "backport-" + realUser.username() + "-" + hash.abbreviate();
var backportBranchHash = fork.branchHash(backportBranchName);

var message = CommitMessageParsers.v1.parse(commit);
var formatter = DateTimeFormatter.ofPattern("d MMM uuuu");
var body = new ArrayList<String>();
body.add("> Hi all,");
body.add("> ");
body.add("> This pull request contains a backport of commit " +
"[" + hash.abbreviate() + "](" + commit.url() + ") from the " +
"[" + bot.repo().name() + "](" + bot.repo().webUrl() + ") repository.");
body.add(">");
var info = "> The commit being backported was authored by " + commit.author().name() + " on " +
commit.committed().format(formatter);
if (message.reviewers().isEmpty()) {
info += " and had no reviewers";
} else {
var reviewers = message.reviewers()
.stream()
.map(r -> censusInstance.census().contributor(r))
.map(c -> c.fullName().isPresent() ? c.fullName().get() : c.username())
.collect(Collectors.toList());
var numReviewers = reviewers.size();
var listing = numReviewers == 1 ?
reviewers.get(0) :
String.join(", ", reviewers.subList(0, numReviewers - 1));
if (numReviewers > 1) {
listing += " and " + reviewers.get(numReviewers - 1);
}
info += " and was reviewed by " + listing;
}
info += ".";
body.add(info);
body.add("> ");
body.add("> Thanks!");

if (backportBranchHash.isEmpty()) {
var localRepoDir = scratchArea.get(this)
.resolve(targetRepo.name())
Expand Down Expand Up @@ -311,7 +345,9 @@ public void handle(PullRequestBot bot, HostedCommit commit, LimitedCensusInstanc
lines.add("```");
lines.add("");
lines.add("Once you have resolved the conflicts as explained above continue with creating a pull request towards the [" + targetRepoName + "](" + targetRepo.webUrl() + ") with the title `Backport " + hash.hex() + "`.");

lines.add("");
lines.add("Below you can find a suggestion for the pull request body:");
lines.addAll(body);
reply.println(String.join("\n", lines));
localRepo.reset(head, true);
return;
Expand Down Expand Up @@ -342,39 +378,6 @@ public void handle(PullRequestBot bot, HostedCommit commit, LimitedCensusInstanc
fork.restrictPushAccess(new Branch(backportBranchName), realUser);
}

var message = CommitMessageParsers.v1.parse(commit);
var formatter = DateTimeFormatter.ofPattern("d MMM uuuu");
var body = new ArrayList<String>();
body.add("> Hi all,");
body.add("> ");
body.add("> This pull request contains a backport of commit " +
"[" + hash.abbreviate() + "](" + commit.url() + ") from the " +
"[" + bot.repo().name() + "](" + bot.repo().webUrl() + ") repository.");
body.add(">");
var info = "> The commit being backported was authored by " + commit.author().name() + " on " +
commit.committed().format(formatter);
if (message.reviewers().isEmpty()) {
info += " and had no reviewers";
} else {
var reviewers = message.reviewers()
.stream()
.map(r -> censusInstance.census().contributor(r))
.map(c -> c.fullName().isPresent() ? c.fullName().get() : c.username())
.collect(Collectors.toList());
var numReviewers = reviewers.size();
var listing = numReviewers == 1 ?
reviewers.get(0) :
String.join(", ", reviewers.subList(0, numReviewers - 1));
if (numReviewers > 1) {
listing += " and " + reviewers.get(numReviewers - 1);
}
info += " and was reviewed by " + listing;
}
info += ".";
body.add(info);
body.add("> ");
body.add("> Thanks!");

var createPrUrl = fork.createPullRequestUrl(targetRepo, targetBranch.name(), backportBranchName);
var targetBranchWebUrl = targetRepo.webUrl(targetBranch);
var backportBranchWebUrl = fork.webUrl(new Branch(backportBranchName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void backportDoesNotApply(TestInfo testInfo) throws IOException {
assertTrue(botReply.body().contains("Please fetch the appropriate branch/commit and manually resolve these conflicts"));
assertTrue(botReply.body().contains("master:master"));
assertTrue(botReply.body().contains("$ git checkout master"));
assertTrue(botReply.body().contains("Below you can find a suggestion for the pull request body:"));
assertEquals(List.of(), author.openPullRequests());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void simple(TestInfo testInfo) throws IOException {
TestBotRunner.runPeriodicItems(prBot);
assertLastCommentContains(pr, "@user2");
assertLastCommentContains(pr, "Could **not** automatically backport");
assertLastCommentContains(pr, "Below you can find a suggestion for the pull request body:");

// Resolve conflict
localRepo.push(masterHash, targetRepo.authenticatedUrl(), "master", true);
Expand Down

1 comment on commit b258994

@openjdk-notifier
Copy link

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.