use of com.google.gerrit.server.notedb.CommitRewriter.RunOptions in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixSubmitChangeMessageAndFooters.
// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixSubmitChangeMessageAndFooters() throws Exception {
Change c = newChange();
PersonIdent invalidAuthorIdent = new PersonIdent(changeOwner.getName(), changeNoteUtil.getAccountIdAsEmailAddress(changeOwner.getAccountId()), Date.from(TimeUtil.now()), serverIdent.getTimeZone());
String changeOwnerIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Change has been successfully merged by " + changeOwner.getName(), "Status: merged", "Tag: autogenerated:gerrit:merged", "Reviewer: " + changeOwnerIdentToFix, "Label: SUBM=+1", "Submission-id: 6310-1521542139810-cfb7e159", "Submitted-with: OK", "Submitted-with: OK: Code-Review: " + changeOwnerIdentToFix), invalidAuthorIdent);
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("@@ -1 +1 @@\n" + "-author Change Owner <1@gerrit> 1254344405 -0700\n" + "+author Gerrit User 1 <1@gerrit> 1254344405 -0700\n" + "@@ -6 +6 @@\n" + "-Change has been successfully merged by Change Owner\n" + "+Change has been successfully merged\n" + "@@ -11 +11 @@\n" + "-Reviewer: Change Owner <1@gerrit>\n" + "+Reviewer: Gerrit User 1 <1@gerrit>\n" + "@@ -15 +15 @@\n" + "-Submitted-with: OK: Code-Review: Change Owner <1@gerrit>\n" + "+Submitted-with: OK: Code-Review: Gerrit User 1 <1@gerrit>\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.RunOptions in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixAssigneeFooterIdent.
@Test
public void fixAssigneeFooterIdent() throws Exception {
Change c = newChange();
String assigneeIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
RevCommit invalidUpdateCommit = writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Assignee added", "Assignee: " + assigneeIdentToFix), getAuthorIdent(changeOwner.getAccount()));
ChangeUpdate changeAssigneeUpdate = newUpdate(c, changeOwner);
changeAssigneeUpdate.setAssignee(otherUserId);
changeAssigneeUpdate.commit();
ChangeUpdate removeAssigneeUpdate = newUpdate(c, changeOwner);
removeAssigneeUpdate.removeAssignee();
removeAssigneeUpdate.commit();
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
int invalidCommitIndex = commitsBeforeRewrite.indexOf(invalidUpdateCommit);
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(notesBeforeRewrite.getPastAssignees()).containsExactly(changeOwner.getAccountId(), otherUser.getAccountId());
assertThat(notesBeforeRewrite.getChange().getAssignee()).isNull();
assertThat(notesAfterRewrite.getPastAssignees()).containsExactly(changeOwner.getAccountId(), otherUser.getAccountId());
assertThat(notesAfterRewrite.getChange().getAssignee()).isNull();
Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, ImmutableList.of(invalidCommitIndex));
assertFixedCommits(ImmutableList.of(invalidUpdateCommit.getId()), result, c.getId());
RevCommit fixedUpdateCommit = commitsAfterRewrite.get(invalidCommitIndex);
assertThat(invalidUpdateCommit.getAuthorIdent()).isEqualTo(fixedUpdateCommit.getAuthorIdent());
assertThat(invalidUpdateCommit.getCommitterIdent()).isEqualTo(fixedUpdateCommit.getCommitterIdent());
assertThat(invalidUpdateCommit.getFullMessage()).isNotEqualTo(fixedUpdateCommit.getFullMessage());
assertThat(fixedUpdateCommit.getFullMessage()).doesNotContain(changeOwner.getName());
assertThat(invalidUpdateCommit.getFullMessage()).contains(assigneeIdentToFix);
String expectedFixedIdent = getValidIdentAsString(changeOwner.getAccount());
assertThat(fixedUpdateCommit.getFullMessage()).contains(expectedFixedIdent);
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -9 +9 @@\n" + "-Assignee: Change Owner <1@gerrit>\n" + "+Assignee: Gerrit User 1 <1@gerrit>\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.RunOptions in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixLabelFooterIdent.
// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixLabelFooterIdent() throws Exception {
Change c = newChange();
String approverIdentToFix = getAccountIdentToFix(otherUser.getAccount());
String changeOwnerIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
ChangeUpdate approvalUpdateByOtherUser = newUpdate(c, otherUser);
approvalUpdateByOtherUser.putApproval(VERIFIED, (short) -1);
approvalUpdateByOtherUser.commit();
ImmutableList<ObjectId> commitsToFix = new ImmutableList.Builder<ObjectId>().add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
null, "Label: -Verified " + approverIdentToFix, "Label: Custom-Label-1=-1 " + approverIdentToFix, "Label: Verified=+1", "Label: Custom-Label-1=+1", "Label: Custom-Label-2=+2 " + approverIdentToFix, "Label: Custom-Label-3=0 " + approverIdentToFix), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
null, "Label: -Verified " + changeOwnerIdentToFix, "Label: Custom-Label-1=+1"), getAuthorIdent(otherUser.getAccount()))).build();
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
ImmutableList<Integer> invalidCommits = commitsToFix.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()));
Instant updateTimestamp = serverIdent.getWhen().toInstant();
ImmutableList<PatchSetApproval> expectedApprovals = ImmutableList.of(PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), changeOwner.getAccountId(), LabelId.create(VERIFIED))).value(0).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), changeOwner.getAccountId(), LabelId.create("Custom-Label-1"))).value(+1).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), otherUserId, LabelId.create(VERIFIED))).value(0).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), otherUserId, LabelId.create("Custom-Label-1"))).value(+1).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), otherUserId, LabelId.create("Custom-Label-2"))).value(+2).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), otherUserId, LabelId.create("Custom-Label-3"))).value(0).granted(updateTimestamp).build());
ChangeNotes notesAfterRewrite = newNotes(c);
assertThat(notesBeforeRewrite.getApprovals().get(c.currentPatchSetId())).containsExactlyElementsIn(expectedApprovals);
assertThat(notesAfterRewrite.getApprovals().get(c.currentPatchSetId())).containsExactlyElementsIn(expectedApprovals);
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, result, c.getId());
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -7,2 +7,2 @@\n" + "-Label: -Verified Other Account <2@gerrit>\n" + "-Label: Custom-Label-1=-1 Other Account <2@gerrit>\n" + "+Label: -Verified Gerrit User 2 <2@gerrit>\n" + "+Label: Custom-Label-1=-1 Gerrit User 2 <2@gerrit>\n" + "@@ -11,2 +11,2 @@\n" + "-Label: Custom-Label-2=+2 Other Account <2@gerrit>\n" + "-Label: Custom-Label-3=0 Other Account <2@gerrit>\n" + "+Label: Custom-Label-2=+2 Gerrit User 2 <2@gerrit>\n" + "+Label: Custom-Label-3=0 Gerrit User 2 <2@gerrit>\n", "@@ -7 +7 @@\n" + "-Label: -Verified Change Owner <1@gerrit>\n" + "+Label: -Verified Gerrit User 1 <1@gerrit>\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.RunOptions in project gerrit by GerritCodeReview.
the class CommitRewriterTest method numRefs_greater_maxRefsToUpdate_allFixed.
@Test
public void numRefs_greater_maxRefsToUpdate_allFixed() throws Exception {
int numberOfChanges = 12;
ImmutableMap.Builder<String, Ref> refsToOldMetaBuilder = new ImmutableMap.Builder<>();
for (int i = 0; i < numberOfChanges; 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());
Ref metaRefBeforeRewrite = repo.exactRef(refName);
refsToOldMetaBuilder.put(refName, metaRefBeforeRewrite);
}
ImmutableMap<String, Ref> refsToOldMeta = refsToOldMetaBuilder.build();
RunOptions options = new RunOptions();
options.dryRun = false;
options.outputDiff = false;
options.verifyCommits = false;
options.maxRefsInBatch = 10;
options.maxRefsToUpdate = 12;
BackfillResult backfillResult = rewriter.backfillProject(project, repo, options);
assertThat(backfillResult.fixedRefDiff.keySet()).isEqualTo(refsToOldMeta.keySet());
for (Map.Entry<String, Ref> refEntry : refsToOldMeta.entrySet()) {
Ref metaRefAfterRewrite = repo.exactRef(refEntry.getKey());
assertThat(refEntry.getValue()).isNotEqualTo(metaRefAfterRewrite);
}
}
use of com.google.gerrit.server.notedb.CommitRewriter.RunOptions in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixSubmittedWithFooterIdent.
@Test
public void fixSubmittedWithFooterIdent() throws Exception {
Change c = newChange();
ChangeUpdate preSubmitUpdate = newUpdate(c, changeOwner);
preSubmitUpdate.setChangeMessage("Per-submit update");
preSubmitUpdate.commit();
String otherUserIdentToFix = getAccountIdentToFix(otherUser.getAccount());
String changeOwnerIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
RevCommit invalidUpdateCommit = writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
null, "Label: SUBM=+1", "Submission-id: 5271-1496917120975-10a10df9", "Submitted-with: NOT_READY", "Submitted-with: NEED: Code-Review: " + otherUserIdentToFix, "Submitted-with: OK: Code-Style", "Submitted-with: OK: Verified: " + changeOwnerIdentToFix, "Submitted-with: FORCED with error"), getAuthorIdent(changeOwner.getAccount()));
ChangeUpdate postSubmitUpdate = newUpdate(c, changeOwner);
postSubmitUpdate.setChangeMessage("Per-submit update");
postSubmitUpdate.commit();
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
int invalidCommitIndex = commitsBeforeRewrite.indexOf(invalidUpdateCommit);
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);
ImmutableList<SubmitRecord> expectedRecords = ImmutableList.of(submitRecord("NOT_READY", null, submitLabel(CODE_REVIEW, "NEED", otherUserId), submitLabel("Code-Style", "OK", null), submitLabel(VERIFIED, "OK", changeOwner.getAccountId())), submitRecord("FORCED", " with error"));
assertThat(notesBeforeRewrite.getSubmitRecords()).isEqualTo(expectedRecords);
assertThat(notesAfterRewrite.getSubmitRecords()).isEqualTo(expectedRecords);
Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, ImmutableList.of(invalidCommitIndex));
assertFixedCommits(ImmutableList.of(invalidUpdateCommit.getId()), result, c.getId());
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).containsExactly("@@ -10 +10 @@\n" + "-Submitted-with: NEED: Code-Review: Other Account <2@gerrit>\n" + "+Submitted-with: NEED: Code-Review: Gerrit User 2 <2@gerrit>\n" + "@@ -12 +12 @@\n" + "-Submitted-with: OK: Verified: Change Owner <1@gerrit>\n" + "+Submitted-with: OK: Verified: Gerrit User 1 <1@gerrit>\n");
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Aggregations