Skip to content

Commit 6ccf259

Browse files
committedMar 25, 2025
2467: Skara bot mistakenly reset the committer when amending commit message
Reviewed-by: erikj
1 parent e0767b8 commit 6ccf259

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed
 

‎bots/pr/src/main/java/org/openjdk/skara/bots/pr/CheckablePullRequest.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -193,15 +193,16 @@ Hash commit(Hash finalHead, Namespace namespace, String censusDomain, String spo
193193
return PullRequestUtils.createCommit(pr, localRepo, finalHead, author, committer, commitMessage);
194194
}
195195

196-
Hash amendManualReviewersAndStaleReviewers(Hash commit, Namespace namespace, Hash original) throws IOException {
196+
Hash amendManualReviewersAndStaleReviewers(Hash hash, Namespace namespace, Hash original) throws IOException {
197197
var activeReviews = filterActiveReviews(pr.reviews(), pr.targetRef());
198-
var originalCommitMessage = commitMessage(commit, activeReviews, namespace, false, original);
199-
var amendedCommitMessage = commitMessage(commit, activeReviews, namespace, true, original);
198+
var originalCommitMessage = commitMessage(hash, activeReviews, namespace, false, original);
199+
var amendedCommitMessage = commitMessage(hash, activeReviews, namespace, true, original);
200200

201201
if (originalCommitMessage.equals(amendedCommitMessage)) {
202-
return commit;
202+
return hash;
203203
} else {
204-
return localRepo.amend(amendedCommitMessage);
204+
var commit = localRepo.lookup(hash).orElseThrow();
205+
return localRepo.amend(amendedCommitMessage, commit.author().name(), commit.author().email(), commit.committer().name(), commit.committer().email());
205206
}
206207
}
207208

‎bots/pr/src/test/java/org/openjdk/skara/bots/pr/SponsorTests.java

+92-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -969,4 +969,95 @@ void retryAfterInterrupt(TestInfo testInfo) throws IOException {
969969
.contains("can only be used in open pull requests"));
970970
}
971971
}
972+
973+
@Test
974+
void sponsorWithAmendingCommitMessage(TestInfo testInfo) throws IOException {
975+
try (var credentials = new HostCredentials(testInfo);
976+
var tempFolder = new TemporaryDirectory();
977+
var pushedFolder = new TemporaryDirectory()) {
978+
var author = credentials.getHostedRepository();
979+
var integrator = credentials.getHostedRepository();
980+
var reviewer1 = credentials.getHostedRepository();
981+
var reviewer2 = credentials.getHostedRepository();
982+
983+
var censusBuilder = credentials.getCensusBuilder()
984+
.addReviewer(reviewer1.forge().currentUser().id())
985+
.addReviewer(reviewer2.forge().currentUser().id())
986+
.addAuthor(author.forge().currentUser().id());
987+
988+
var mergeBot = PullRequestBot.newBuilder().repo(integrator).censusRepo(censusBuilder.build()).useStaleReviews(false).build();
989+
990+
// Populate the projects repository
991+
var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
992+
var masterHash = localRepo.resolve("master").orElseThrow();
993+
assertFalse(CheckableRepository.hasBeenEdited(localRepo));
994+
localRepo.push(masterHash, author.authenticatedUrl(), "master", true);
995+
996+
// Make a change with a corresponding PR
997+
var authorFullName = author.forge().currentUser().fullName();
998+
var authorEmail = "ta@none.none";
999+
var editHash = CheckableRepository.appendAndCommit(localRepo, "This is a new line", "Append commit", authorFullName, authorEmail);
1000+
localRepo.push(editHash, author.authenticatedUrl(), "edit", true);
1001+
var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
1002+
1003+
// Approve it as reviewer2
1004+
var approval2Pr = reviewer2.pullRequest(pr.id());
1005+
approval2Pr.addReview(Review.Verdict.APPROVED, "Approved");
1006+
TestBotRunner.runPeriodicItems(mergeBot);
1007+
1008+
// Make a change with a corresponding PR
1009+
var updateHash = CheckableRepository.appendAndCommit(localRepo);
1010+
localRepo.push(updateHash, author.authenticatedUrl(), "refs/heads/edit", true);
1011+
TestBotRunner.runPeriodicItems(mergeBot);
1012+
1013+
// Approve it as reviewer1
1014+
var approval1Pr = reviewer1.pullRequest(pr.id());
1015+
approval1Pr.addReview(Review.Verdict.APPROVED, "Approved");
1016+
TestBotRunner.runPeriodicItems(mergeBot);
1017+
1018+
// Issue a merge command without being a Committer
1019+
pr.addComment("/integrate");
1020+
TestBotRunner.runPeriodicItems(mergeBot);
1021+
1022+
// The bot should reply that a sponsor is required
1023+
var sponsor = pr.comments().stream()
1024+
.filter(comment -> comment.body().contains("sponsor"))
1025+
.filter(comment -> comment.body().contains("your change"))
1026+
.count();
1027+
assertEquals(1, sponsor);
1028+
1029+
// The bot should not have pushed the commit
1030+
var notPushed = pr.comments().stream()
1031+
.filter(comment -> comment.body().contains("Pushed as commit"))
1032+
.count();
1033+
assertEquals(0, notPushed);
1034+
1035+
// Reviewer now agrees to sponsor
1036+
var reviewer1Pr = reviewer1.pullRequest(pr.id());
1037+
pr.addComment("/summary amendCommitMessage");
1038+
reviewer1Pr.addComment("/sponsor");
1039+
TestBotRunner.runPeriodicItems(mergeBot);
1040+
1041+
// The bot should have pushed the commit
1042+
var pushed = pr.comments().stream()
1043+
.filter(comment -> comment.body().contains("Pushed as commit"))
1044+
.count();
1045+
assertEquals(1, pushed);
1046+
1047+
// The change should now be present on the master branch
1048+
var pushedRepo = Repository.materialize(pushedFolder.path(), author.authenticatedUrl(), "master");
1049+
var headHash = pushedRepo.resolve("HEAD").orElseThrow();
1050+
var headCommit = pushedRepo.commits(headHash.hex() + "^.." + headHash.hex()).asList().get(0);
1051+
1052+
assertEquals("Generated Author 3", headCommit.author().name());
1053+
assertEquals("integrationauthor3@openjdk.org", headCommit.author().email());
1054+
1055+
// The committer should be the sponsor
1056+
assertEquals("Generated Reviewer 1", headCommit.committer().name());
1057+
assertEquals("integrationreviewer1@openjdk.org", headCommit.committer().email());
1058+
assertTrue(pr.store().labelNames().contains("integrated"));
1059+
assertFalse(pr.store().labelNames().contains("ready"));
1060+
assertFalse(pr.store().labelNames().contains("sponsor"));
1061+
}
1062+
}
9721063
}

0 commit comments

Comments
 (0)