use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.
the class CommitRewriterTest method validHistoryNoOp.
@Test
public void validHistoryNoOp() throws Exception {
String tag = "jenkins";
Change c = newChange();
ChangeUpdate update = newUpdate(c, changeOwner);
update.setChangeMessage("verification from jenkins");
update.setTag(tag);
update.commit();
ChangeUpdate updateWithSubject = newUpdate(c, changeOwner);
updateWithSubject.setSubjectForCommit("Update with subject");
updateWithSubject.commit();
ChangeNotes notesBeforeRewrite = newNotes(c);
Ref metaRefBefore = repo.exactRef(RefNames.changeMetaRef(c.getId()));
RunOptions options = new RunOptions();
options.dryRun = false;
BackfillResult backfillResult = rewriter.backfillProject(project, repo, options);
ChangeNotes notesAfterRewrite = newNotes(c);
Ref metaRefAfter = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(notesBeforeRewrite.getMetaId()).isEqualTo(notesAfterRewrite.getMetaId());
assertThat(metaRefBefore.getObjectId()).isEqualTo(metaRefAfter.getObjectId());
assertThat(backfillResult.fixedRefDiff).isEmpty();
}
use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixSubmitChangeMessage.
@Test
public void fixSubmitChangeMessage() throws Exception {
Change c = newChange();
ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
ChangeUpdate invalidMergedMessageUpdate = newUpdate(c, changeOwner);
invalidMergedMessageUpdate.setChangeMessage("Change has been successfully merged by " + changeOwner.getName());
invalidMergedMessageUpdate.setTopic("");
commitsToFix.add(invalidMergedMessageUpdate.commit());
ChangeUpdate invalidCherryPickedMessageUpdate = newUpdate(c, changeOwner);
invalidCherryPickedMessageUpdate.setChangeMessage("Change has been successfully cherry-picked as e40dc1a50dc7f457a37579e2755374f3e1a5413b by " + changeOwner.getName());
commitsToFix.add(invalidCherryPickedMessageUpdate.commit());
ChangeUpdate invalidRebasedMessageUpdate = newUpdate(c, changeOwner);
invalidRebasedMessageUpdate.setChangeMessage("Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b by " + changeOwner.getName());
commitsToFix.add(invalidRebasedMessageUpdate.commit());
ChangeUpdate validSubmitMessageUpdate = newUpdate(c, changeOwner);
validSubmitMessageUpdate.setChangeMessage("Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b");
validSubmitMessageUpdate.commit();
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
ImmutableList<Integer> invalidCommits = commitsToFix.build().stream().map(commit -> commitsBeforeRewrite.indexOf(commit)).collect(toImmutableList());
ChangeNotes notesBeforeRewrite = newNotes(c);
RunOptions options = new RunOptions();
options.dryRun = false;
BackfillResult result = rewriter.backfillProject(project, repo, options);
assertThat(result.fixedRefDiff.keySet()).containsExactly(RefNames.changeMetaRef(c.getId()));
ChangeNotes notesAfterRewrite = newNotes(c);
assertThat(changeMessages(notesBeforeRewrite)).containsExactly("Change has been successfully merged by Change Owner", "Change has been successfully cherry-picked as e40dc1a50dc7f457a37579e2755374f3e1a5413b by Change Owner", "Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b by Change Owner", "Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b");
assertThat(changeMessages(notesAfterRewrite)).containsExactly("Change has been successfully merged", "Change has been successfully cherry-picked as e40dc1a50dc7f457a37579e2755374f3e1a5413b", "Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b", "Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b");
Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, invalidCommits);
assertFixedCommits(commitsToFix.build(), result, c.getId());
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -6 +6 @@\n" + "-Change has been successfully merged by Change Owner\n" + "+Change has been successfully merged\n", "@@ -6 +6 @@\n" + "-Change has been successfully cherry-picked as e40dc1a50dc7f457a37579e2755374f3e1a5413b by Change Owner\n" + "+Change has been successfully cherry-picked as e40dc1a50dc7f457a37579e2755374f3e1a5413b\n", "@@ -6 +6 @@\n" + "-Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b by Change Owner\n" + "+Change has been successfully rebased and submitted as e40dc1a50dc7f457a37579e2755374f3e1a5413b\n");
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixCodeOwnersOnReviewChangeMessage.
@Test
public void fixCodeOwnersOnReviewChangeMessage() throws Exception {
Change c = newChange();
ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
ChangeUpdate invalidOnReviewUpdate = newUpdate(c, changeOwner);
invalidOnReviewUpdate.setChangeMessage("Patch Set 1: Any-Label+2 Other-Label+2 Code-Review+2\n\n" + "By voting Code-Review+2 the following files are now code-owner approved by Change Owner:\n" + " * file1.java\n" + " * file2.ts\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n" + "By voting Other-Label+2 the code-owners submit requirement is still overridden by Change Owner\n");
commitsToFix.add(invalidOnReviewUpdate.commit());
ChangeUpdate invalidOnReviewUpdateAnyOrder = newUpdate(c, changeOwner);
invalidOnReviewUpdateAnyOrder.setChangeMessage("Patch Set 1: Any-Label+2 Other-Label+2 Code-Review+2\n\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n" + "By voting Other-Label+2 the code-owners submit requirement is still overridden by Change Owner\n" + "By voting Code-Review+2 the following files are now code-owner approved by Change Owner:\n" + " * file1.java\n" + " * file2.ts\n");
commitsToFix.add(invalidOnReviewUpdateAnyOrder.commit());
ChangeUpdate invalidOnApprovalUpdate = newUpdate(c, otherUser);
invalidOnApprovalUpdate.setChangeMessage("Patch Set 1: -Code-Review\n\n" + "By removing the Code-Review+2 vote the following files are no longer explicitly code-owner approved by Other Account:\n" + " * file1.java\n" + " * file2.ts\n" + "\nThe listed files are still implicitly approved by Other Account.\n");
commitsToFix.add(invalidOnApprovalUpdate.commit());
ChangeUpdate invalidOnOverrideUpdate = newUpdate(c, changeOwner);
invalidOnOverrideUpdate.setChangeMessage("Patch Set 1: -Owners-Override\n\n" + "(1 comment)\n\n" + "By removing the Owners-Override+1 vote the code-owners submit requirement is no longer overridden by Change Owner\n");
commitsToFix.add(invalidOnOverrideUpdate.commit());
ChangeUpdate partiallyValidOnReviewUpdate = newUpdate(c, changeOwner);
partiallyValidOnReviewUpdate.setChangeMessage("Patch Set 1: Any-Label+2 Code-Review+2\n\n" + "By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file1.java\n" + " * file2.ts\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n");
commitsToFix.add(partiallyValidOnReviewUpdate.commit());
ChangeUpdate validOnApprovalUpdate = newUpdate(c, changeOwner);
validOnApprovalUpdate.setChangeMessage("Patch Set 1: Code-Review-2\n\n" + "By voting Code-Review-2 the following files are no longer explicitly code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file4.java\n");
validOnApprovalUpdate.commit();
ChangeUpdate validOnOverrideUpdate = newUpdate(c, changeOwner);
validOnOverrideUpdate.setChangeMessage("Patch Set 1: Owners-Override+1\n\n" + "By voting Owners-Override+1 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n");
validOnOverrideUpdate.commit();
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
ImmutableList<Integer> invalidCommits = commitsToFix.build().stream().map(commit -> commitsBeforeRewrite.indexOf(commit)).collect(toImmutableList());
ChangeNotes notesBeforeRewrite = newNotes(c);
RunOptions options = new RunOptions();
options.dryRun = false;
BackfillResult result = rewriter.backfillProject(project, repo, options);
assertThat(result.fixedRefDiff.keySet()).containsExactly(RefNames.changeMetaRef(c.getId()));
ChangeNotes notesAfterRewrite = newNotes(c);
assertThat(changeMessages(notesBeforeRewrite)).hasSize(7);
assertThat(changeMessages(notesAfterRewrite)).containsExactly("Patch Set 1: Any-Label+2 Other-Label+2 Code-Review+2\n\n" + "By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file1.java\n" + " * file2.ts\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n" + "By voting Other-Label+2 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n", "Patch Set 1: Any-Label+2 Other-Label+2 Code-Review+2\n\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n" + "By voting Other-Label+2 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n" + "By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file1.java\n" + " * file2.ts\n", "Patch Set 1: -Code-Review\n" + "\n" + "By removing the Code-Review+2 vote the following files are no longer explicitly code-owner approved by <GERRIT_ACCOUNT_2>:\n" + " * file1.java\n" + " * file2.ts\n" + "\nThe listed files are still implicitly approved by <GERRIT_ACCOUNT_2>.\n", "Patch Set 1: -Owners-Override\n" + "\n" + "(1 comment)\n" + "\n" + "By removing the Owners-Override+1 vote the code-owners submit requirement is no longer overridden by <GERRIT_ACCOUNT_1>\n", "Patch Set 1: Any-Label+2 Code-Review+2\n\n" + "By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file1.java\n" + " * file2.ts\n" + "By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n", "Patch Set 1: Code-Review-2\n\n" + "By voting Code-Review-2 the following files are no longer explicitly code-owner approved by <GERRIT_ACCOUNT_1>:\n" + " * file4.java\n", "Patch Set 1: Owners-Override+1\n" + "\n" + "By voting Owners-Override+1 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n");
Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, invalidCommits);
assertFixedCommits(commitsToFix.build(), result, c.getId());
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -8 +8 @@\n" + "-By voting Code-Review+2 the following files are now code-owner approved by Change Owner:\n" + "+By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n" + "@@ -11,2 +11,2 @@\n" + "-By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n" + "-By voting Other-Label+2 the code-owners submit requirement is still overridden by Change Owner\n" + "+By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n" + "+By voting Other-Label+2 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n", "@@ -8,3 +8,3 @@\n" + "-By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n" + "-By voting Other-Label+2 the code-owners submit requirement is still overridden by Change Owner\n" + "-By voting Code-Review+2 the following files are now code-owner approved by Change Owner:\n" + "+By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n" + "+By voting Other-Label+2 the code-owners submit requirement is still overridden by <GERRIT_ACCOUNT_1>\n" + "+By voting Code-Review+2 the following files are now code-owner approved by <GERRIT_ACCOUNT_1>:\n", "@@ -8 +8 @@\n" + "-By removing the Code-Review+2 vote the following files are no longer explicitly code-owner approved by Other Account:\n" + "+By removing the Code-Review+2 vote the following files are no longer explicitly code-owner approved by <GERRIT_ACCOUNT_2>:\n" + "@@ -12 +12 @@\n" + "-The listed files are still implicitly approved by Other Account.\n" + "+The listed files are still implicitly approved by <GERRIT_ACCOUNT_2>.\n", "@@ -10 +10 @@\n" + "-By removing the Owners-Override+1 vote the code-owners submit requirement is no longer overridden by Change Owner\n" + "+By removing the Owners-Override+1 vote the code-owners submit requirement is no longer overridden by <GERRIT_ACCOUNT_1>\n", "@@ -11 +11 @@\n" + "-By voting Any-Label+2 the code-owners submit requirement is overridden by Change Owner\n" + "+By voting Any-Label+2 the code-owners submit requirement is overridden by <GERRIT_ACCOUNT_1>\n");
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixRemoveVoteChangeMessageWithNoFooterLabel_matchDuplicateAccounts.
@Test
public void fixRemoveVoteChangeMessageWithNoFooterLabel_matchDuplicateAccounts() throws Exception {
Account duplicateCodeOwner = Account.builder(Account.id(4), TimeUtil.now()).setFullName(changeOwner.getName()).setPreferredEmail("other@test.com").build();
accountCache.put(duplicateCodeOwner);
Change c = newChange();
ChangeUpdate approvalUpdate = newUpdate(c, changeOwner);
approvalUpdate.putApproval(VERIFIED, (short) +2);
approvalUpdate.putApprovalFor(duplicateCodeOwner.id(), VERIFIED, (short) -1);
approvalUpdate.commit();
writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
"Removed Verified+2 by Change Owner <other@test.com>"), getAuthorIdent(changeOwner.getAccount()));
writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
"Removed Verified+2 by Change Owner <change@owner.com>"), getAuthorIdent(changeOwner.getAccount()));
writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
"Removed Verified-1 by Change Owner <other@test.com>"), getAuthorIdent(changeOwner.getAccount()));
RunOptions options = new RunOptions();
options.dryRun = false;
BackfillResult result = rewriter.backfillProject(project, repo, options);
assertThat(result.fixedRefDiff.keySet()).containsExactly(RefNames.changeMetaRef(c.getId()));
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -6 +6 @@\n" + "-Removed Verified+2 by Change Owner <other@test.com>\n" + "+Removed Verified+2 by <GERRIT_ACCOUNT_4>\n", "@@ -6 +6 @@\n" + "-Removed Verified+2 by Change Owner <change@owner.com>\n" + "+Removed Verified+2 by <GERRIT_ACCOUNT_1>\n", "@@ -6 +6 @@\n" + "-Removed Verified-1 by Change Owner <other@test.com>\n" + "+Removed Verified-1 by <GERRIT_ACCOUNT_4>\n");
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.
the class CommitRewriterTest method testMaxRefsToUpdate.
private void testMaxRefsToUpdate(int numberOfInvalidChanges, int numberOfValidChanges, int maxRefsToUpdate, int maxRefsInBatch) throws Exception {
ImmutableMap.Builder<String, ObjectId> expectedFixedRefsToOldMetaBuilder = new ImmutableMap.Builder<>();
ImmutableMap.Builder<String, ObjectId> expectedSkippedRefsToOldMetaBuilder = new ImmutableMap.Builder<>();
for (int i = 0; i < numberOfValidChanges; i++) {
Change c = newChange();
ChangeUpdate updateWithSubject = newUpdate(c, changeOwner);
updateWithSubject.setSubjectForCommit("Update with subject");
updateWithSubject.commit();
String refName = RefNames.changeMetaRef(c.getId());
Ref metaRefBeforeRewrite = repo.exactRef(refName);
expectedSkippedRefsToOldMetaBuilder.put(refName, metaRefBeforeRewrite.getObjectId());
}
Set<String> invalidRefs = new HashSet<>();
for (int i = 0; i < numberOfInvalidChanges; i++) {
Change c = newChange();
ChangeUpdate update = newUpdate(c, changeOwner);
update.setChangeMessage("Change has been successfully merged by " + changeOwner.getName());
update.commit();
ChangeUpdate updateWithSubject = newUpdate(c, changeOwner);
updateWithSubject.setSubjectForCommit("Update with subject");
updateWithSubject.commit();
String refName = RefNames.changeMetaRef(c.getId());
invalidRefs.add(refName);
}
int i = 0;
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_CHANGES)) {
Ref metaRefBeforeRewrite = repo.exactRef(ref.getName());
if (!invalidRefs.contains(ref.getName())) {
continue;
}
if (i < maxRefsToUpdate) {
expectedFixedRefsToOldMetaBuilder.put(ref.getName(), metaRefBeforeRewrite.getObjectId());
} else {
expectedSkippedRefsToOldMetaBuilder.put(ref.getName(), metaRefBeforeRewrite.getObjectId());
}
i++;
}
ImmutableMap<String, ObjectId> expectedFixedRefsToOldMeta = expectedFixedRefsToOldMetaBuilder.build();
ImmutableMap<String, ObjectId> expectedSkippedRefsToOldMeta = expectedSkippedRefsToOldMetaBuilder.build();
RunOptions options = new RunOptions();
options.dryRun = false;
options.outputDiff = false;
options.verifyCommits = false;
options.maxRefsInBatch = maxRefsInBatch;
options.maxRefsToUpdate = maxRefsToUpdate;
BackfillResult backfillResult = rewriter.backfillProject(project, repo, options);
assertThat(backfillResult.fixedRefDiff.keySet()).isEqualTo(expectedFixedRefsToOldMeta.keySet());
for (Map.Entry<String, ObjectId> refEntry : expectedFixedRefsToOldMeta.entrySet()) {
Ref metaRefAfterRewrite = repo.exactRef(refEntry.getKey());
assertThat(refEntry.getValue()).isNotEqualTo(metaRefAfterRewrite.getObjectId());
}
for (Map.Entry<String, ObjectId> refEntry : expectedSkippedRefsToOldMeta.entrySet()) {
Ref metaRefAfterRewrite = repo.exactRef(refEntry.getKey());
assertThat(refEntry.getValue()).isEqualTo(metaRefAfterRewrite.getObjectId());
}
RunOptions secondRunOptions = new RunOptions();
secondRunOptions.dryRun = false;
secondRunOptions.outputDiff = false;
secondRunOptions.verifyCommits = false;
secondRunOptions.maxRefsInBatch = maxRefsInBatch;
secondRunOptions.maxRefsToUpdate = numberOfInvalidChanges + numberOfValidChanges;
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
int expectedSecondRunResult = numberOfInvalidChanges > maxRefsToUpdate ? numberOfInvalidChanges - maxRefsToUpdate : 0;
assertThat(secondRunResult.fixedRefDiff.keySet().size()).isEqualTo(expectedSecondRunResult);
}
Aggregations