Search in sources :

Example 1 with BackfillResult

use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixDeleteChangeMessageCommitMessage.

@Test
public void fixDeleteChangeMessageCommitMessage() throws Exception {
    Change c = newChange();
    ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
    ChangeUpdate invalidDeleteChangeMessageUpdate = newUpdate(c, changeOwner);
    invalidDeleteChangeMessageUpdate.setChangeMessage("Change message removed by: " + changeOwner.getName());
    commitsToFix.add(invalidDeleteChangeMessageUpdate.commit());
    ChangeUpdate invalidDeleteChangeMessageUpdateWithReason = newUpdate(c, changeOwner);
    invalidDeleteChangeMessageUpdateWithReason.setChangeMessage(String.format("Change message removed by: %s\nReason: %s", changeOwner.getName(), "contains confidential information"));
    commitsToFix.add(invalidDeleteChangeMessageUpdateWithReason.commit());
    ChangeUpdate validDeleteChangeMessageUpdate = newUpdate(c, changeOwner);
    validDeleteChangeMessageUpdate.setChangeMessage("Change message removed by: <GERRIT_ACCOUNT_1>");
    validDeleteChangeMessageUpdate.commit();
    ChangeUpdate validDeleteChangeMessageUpdateWithReason = newUpdate(c, changeOwner);
    validDeleteChangeMessageUpdateWithReason.setChangeMessage("Change message removed by: <GERRIT_ACCOUNT_1>\nReason: abusive language");
    validDeleteChangeMessageUpdateWithReason.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 message removed by: Change Owner", "Change message removed by: Change Owner\n" + "Reason: contains confidential information", "Change message removed by: <GERRIT_ACCOUNT_1>", "Change message removed by: <GERRIT_ACCOUNT_1>\n" + "Reason: abusive language");
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Change message removed", "Change message removed\n" + "Reason: contains confidential information", "Change message removed by: <GERRIT_ACCOUNT_1>", "Change message removed by: <GERRIT_ACCOUNT_1>\n" + "Reason: abusive language");
    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 message removed by: Change Owner\n" + "+Change message removed\n", "@@ -6 +6 @@\n" + "-Change message removed by: Change Owner\n" + "+Change message removed\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CommitDiff(com.google.gerrit.server.notedb.CommitRewriter.CommitDiff) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Gson(com.google.gson.Gson) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Map(java.util.Map) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Instant(java.time.Instant) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) VERIFIED(com.google.gerrit.entities.LabelId.VERIFIED) Ref(org.eclipse.jgit.lib.Ref) IntStream(java.util.stream.IntStream) LabelId(com.google.gerrit.entities.LabelId) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CC(com.google.gerrit.server.notedb.ReviewerStateInternal.CC) HashSet(java.util.HashSet) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ImmutableList(com.google.common.collect.ImmutableList) CODE_REVIEW(com.google.gerrit.entities.LabelId.CODE_REVIEW) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Before(org.junit.Before) Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) CurrentUser(com.google.gerrit.server.CurrentUser) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) REMOVED(com.google.gerrit.server.notedb.ReviewerStateInternal.REMOVED) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Repository(org.eclipse.jgit.lib.Repository) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.gerrit.entities.Change) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 2 with BackfillResult

use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixAssigneeChangeMessage.

@Test
public void fixAssigneeChangeMessage() throws Exception {
    Change c = newChange();
    ImmutableList<ObjectId> commitsToFix = new ImmutableList.Builder<ObjectId>().add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Assignee added: " + changeOwner.getNameEmail(), "Assignee: " + getValidIdentAsString(changeOwner.getAccount())), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, String.format("Assignee changed from: %s to: %s", changeOwner.getNameEmail(), otherUser.getNameEmail()), "Assignee: " + getValidIdentAsString(otherUser.getAccount())), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Assignee deleted: " + otherUser.getNameEmail(), "Assignee:"), getAuthorIdent(changeOwner.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()));
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(notesBeforeRewrite.getPastAssignees()).containsExactly(changeOwner.getAccountId(), otherUser.getAccountId());
    assertThat(notesBeforeRewrite.getChange().getAssignee()).isNull();
    assertThat(changeMessages(notesBeforeRewrite)).containsExactly("Assignee added: Change Owner <change@owner.com>", "Assignee changed from: Change Owner <change@owner.com> to: Other Account <other@account.com>", "Assignee deleted: Other Account <other@account.com>");
    assertThat(notesAfterRewrite.getPastAssignees()).containsExactly(changeOwner.getAccountId(), otherUser.getAccountId());
    assertThat(notesAfterRewrite.getChange().getAssignee()).isNull();
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Assignee added: " + AccountTemplateUtil.getAccountTemplate(changeOwner.getAccountId()), String.format("Assignee changed from: %s to: %s", AccountTemplateUtil.getAccountTemplate(changeOwner.getAccountId()), AccountTemplateUtil.getAccountTemplate(otherUser.getAccountId())), "Assignee deleted: " + AccountTemplateUtil.getAccountTemplate(otherUser.getAccountId()));
    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("@@ -6 +6 @@\n" + "-Assignee added: Change Owner <change@owner.com>\n" + "+Assignee added: <GERRIT_ACCOUNT_1>\n", "@@ -6 +6 @@\n" + "-Assignee changed from: Change Owner <change@owner.com> to: Other Account <other@account.com>\n" + "+Assignee changed from: <GERRIT_ACCOUNT_1> to: <GERRIT_ACCOUNT_2>\n", "@@ -6 +6 @@\n" + "-Assignee deleted: Other Account <other@account.com>\n" + "+Assignee deleted: <GERRIT_ACCOUNT_2>\n" + // Both empty value and space are parsed as deleted assignee anyway.
    "@@ -9 +9 @@\n" + "-Assignee:\n" + "+Assignee: \n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CommitDiff(com.google.gerrit.server.notedb.CommitRewriter.CommitDiff) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Gson(com.google.gson.Gson) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Map(java.util.Map) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Instant(java.time.Instant) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) VERIFIED(com.google.gerrit.entities.LabelId.VERIFIED) Ref(org.eclipse.jgit.lib.Ref) IntStream(java.util.stream.IntStream) LabelId(com.google.gerrit.entities.LabelId) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CC(com.google.gerrit.server.notedb.ReviewerStateInternal.CC) HashSet(java.util.HashSet) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ImmutableList(com.google.common.collect.ImmutableList) CODE_REVIEW(com.google.gerrit.entities.LabelId.CODE_REVIEW) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Before(org.junit.Before) Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) CurrentUser(com.google.gerrit.server.CurrentUser) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) REMOVED(com.google.gerrit.server.notedb.ReviewerStateInternal.REMOVED) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Repository(org.eclipse.jgit.lib.Repository) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 3 with BackfillResult

use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixRemoveVoteChangeMessage.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixRemoveVoteChangeMessage() throws Exception {
    Change c = newChange();
    String approverIdentToFix = getAccountIdentToFix(otherUser.getAccount());
    ChangeUpdate approvalUpdateByOtherUser = newUpdate(c, otherUser);
    approvalUpdateByOtherUser.putApproval(CODE_REVIEW, (short) +2);
    approvalUpdateByOtherUser.putApproval("Custom-Label", (short) -1);
    approvalUpdateByOtherUser.putApprovalFor(changeOwner.getAccountId(), VERIFIED, (short) -1);
    approvalUpdateByOtherUser.commit();
    ImmutableList<ObjectId> commitsToFix = new ImmutableList.Builder<ObjectId>().add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed Code-Review+2 by " + otherUser.getNameEmail(), "Label: -Code-Review " + approverIdentToFix), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed Custom-Label-1 by " + otherUser.getNameEmail(), "Label: -Custom-Label " + getValidIdentAsString(otherUser.getAccount())), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed Verified+2 by " + changeOwner.getNameEmail(), "Label: -Verified"), getAuthorIdent(changeOwner.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(), otherUserId, LabelId.create("Custom-Label"))).value(0).granted(updateTimestamp).build(), PatchSetApproval.builder().key(PatchSetApproval.key(c.currentPatchSetId(), otherUserId, LabelId.create(CODE_REVIEW))).value(0).granted(updateTimestamp).build());
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(changeMessages(notesBeforeRewrite)).containsExactly("Removed Code-Review+2 by Other Account <other@account.com>", "Removed Custom-Label-1 by Other Account <other@account.com>", "Removed Verified+2 by Change Owner <change@owner.com>");
    assertThat(notesBeforeRewrite.getApprovals().get(c.currentPatchSetId())).containsExactlyElementsIn(expectedApprovals);
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Removed Code-Review+2 by <GERRIT_ACCOUNT_2>", "Removed Custom-Label-1 by <GERRIT_ACCOUNT_2>", "Removed Verified+2 by <GERRIT_ACCOUNT_1>");
    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("@@ -6 +6 @@\n" + "-Removed Code-Review+2 by Other Account <other@account.com>\n" + "+Removed Code-Review+2 by <GERRIT_ACCOUNT_2>\n" + "@@ -9 +9 @@\n" + "-Label: -Code-Review Other Account <2@gerrit>\n" + "+Label: -Code-Review Gerrit User 2 <2@gerrit>\n", "@@ -6 +6 @@\n" + "-Removed Custom-Label-1 by Other Account <other@account.com>\n" + "+Removed Custom-Label-1 by <GERRIT_ACCOUNT_2>\n", "@@ -6 +6 @@\n" + "-Removed Verified+2 by Change Owner <change@owner.com>\n" + "+Removed Verified+2 by <GERRIT_ACCOUNT_1>\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CommitDiff(com.google.gerrit.server.notedb.CommitRewriter.CommitDiff) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Gson(com.google.gson.Gson) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Map(java.util.Map) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Instant(java.time.Instant) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) VERIFIED(com.google.gerrit.entities.LabelId.VERIFIED) Ref(org.eclipse.jgit.lib.Ref) IntStream(java.util.stream.IntStream) LabelId(com.google.gerrit.entities.LabelId) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CC(com.google.gerrit.server.notedb.ReviewerStateInternal.CC) HashSet(java.util.HashSet) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ImmutableList(com.google.common.collect.ImmutableList) CODE_REVIEW(com.google.gerrit.entities.LabelId.CODE_REVIEW) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Before(org.junit.Before) Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) CurrentUser(com.google.gerrit.server.CurrentUser) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) REMOVED(com.google.gerrit.server.notedb.ReviewerStateInternal.REMOVED) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Repository(org.eclipse.jgit.lib.Repository) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 4 with BackfillResult

use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixCodeOwnersOnAddReviewerChangeMessage.

@Test
public void fixCodeOwnersOnAddReviewerChangeMessage() throws Exception {
    Account reviewer = Account.builder(Account.id(3), TimeUtil.now()).setFullName("Reviewer User").setPreferredEmail("reviewer@account.com").build();
    accountCache.put(reviewer);
    Account duplicateCodeOwner = Account.builder(Account.id(4), TimeUtil.now()).setFullName(changeOwner.getName()).build();
    accountCache.put(duplicateCodeOwner);
    Account duplicateReviewer = Account.builder(Account.id(5), TimeUtil.now()).setFullName(reviewer.getName()).build();
    accountCache.put(duplicateReviewer);
    Change c = newChange();
    ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
    ChangeUpdate addReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    addReviewerUpdate.putReviewer(reviewer.id(), REVIEWER);
    addReviewerUpdate.commit();
    ChangeUpdate invalidOnAddReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    invalidOnAddReviewerUpdate.setChangeMessage("Reviewer User who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "   * file2.ts\n");
    commitsToFix.add(invalidOnAddReviewerUpdate.commit());
    ChangeUpdate addOtherReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    addOtherReviewerUpdate.putReviewer(otherUserId, REVIEWER);
    addOtherReviewerUpdate.commit();
    ChangeUpdate invalidOnAddReviewerMultipleReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    invalidOnAddReviewerMultipleReviewerUpdate.setChangeMessage("Reviewer User who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "\nOther Account who was added as reviewer owns the following files:\n" + "   * file3.js\n" + "\nMissing Reviewer who was added as reviewer owns the following files:\n" + "   * file4.java\n");
    commitsToFix.add(invalidOnAddReviewerMultipleReviewerUpdate.commit());
    ChangeUpdate addDuplicateReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    addDuplicateReviewerUpdate.putReviewer(duplicateReviewer.id(), REVIEWER);
    addDuplicateReviewerUpdate.commit();
    // Reviewer name resolves to multiple accounts in the same change
    ChangeUpdate onAddReviewerUpdateWithDuplicate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    onAddReviewerUpdateWithDuplicate.setChangeMessage("Reviewer User who was added as reviewer owns the following files:\n" + "   * file6.java\n");
    commitsToFix.add(onAddReviewerUpdateWithDuplicate.commit());
    ChangeUpdate validOnAddReviewerUpdate = newCodeOwnerAddReviewerUpdate(c, changeOwner);
    validOnAddReviewerUpdate.setChangeMessage("Gerrit Account who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "\n<GERRIT_ACCOUNT_1> who was added as reviewer owns the following files:\n" + "   * file3.js\n");
    validOnAddReviewerUpdate.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(4);
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("<GERRIT_ACCOUNT_3>, who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "   * file2.ts\n", "<GERRIT_ACCOUNT_3>, who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "\n<GERRIT_ACCOUNT_2>, who was added as reviewer owns the following files:\n" + "   * file3.js\n" + "\nAdded reviewer owns the following files:\n" + "   * file4.java\n", "Added reviewer owns the following files:\n" + "   * file6.java\n", "Gerrit Account who was added as reviewer owns the following files:\n" + "   * file1.java\n" + "\n<GERRIT_ACCOUNT_1> who was added as reviewer owns the following files:\n" + "   * file3.js\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("@@ -6 +6 @@\n" + "-Reviewer User who was added as reviewer owns the following files:\n" + "+<GERRIT_ACCOUNT_3>, who was added as reviewer owns the following files:\n", "@@ -6 +6 @@\n" + "-Reviewer User who was added as reviewer owns the following files:\n" + "+<GERRIT_ACCOUNT_3>, who was added as reviewer owns the following files:\n" + "@@ -9 +9 @@\n" + "-Other Account who was added as reviewer owns the following files:\n" + "+<GERRIT_ACCOUNT_2>, who was added as reviewer owns the following files:\n" + "@@ -12 +12 @@\n" + "-Missing Reviewer who was added as reviewer owns the following files:\n" + "+Added reviewer owns the following files:\n", "@@ -6 +6 @@\n" + "-Reviewer User who was added as reviewer owns the following files:\n" + "+Added reviewer owns the following files:\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CommitDiff(com.google.gerrit.server.notedb.CommitRewriter.CommitDiff) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Gson(com.google.gson.Gson) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Map(java.util.Map) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Instant(java.time.Instant) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) VERIFIED(com.google.gerrit.entities.LabelId.VERIFIED) Ref(org.eclipse.jgit.lib.Ref) IntStream(java.util.stream.IntStream) LabelId(com.google.gerrit.entities.LabelId) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CC(com.google.gerrit.server.notedb.ReviewerStateInternal.CC) HashSet(java.util.HashSet) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ImmutableList(com.google.common.collect.ImmutableList) CODE_REVIEW(com.google.gerrit.entities.LabelId.CODE_REVIEW) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Before(org.junit.Before) Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) CurrentUser(com.google.gerrit.server.CurrentUser) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) REMOVED(com.google.gerrit.server.notedb.ReviewerStateInternal.REMOVED) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Repository(org.eclipse.jgit.lib.Repository) Account(com.google.gerrit.entities.Account) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.gerrit.entities.Change) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 5 with BackfillResult

use of com.google.gerrit.server.notedb.CommitRewriter.BackfillResult in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixReviewerFooterIdent.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixReviewerFooterIdent() throws Exception {
    Change c = newChange();
    String reviewerIdentToFix = getAccountIdentToFix(otherUser.getAccount());
    ImmutableList<ObjectId> commitsToFix = new ImmutableList.Builder<ObjectId>().add(writeUpdate(RefNames.changeMetaRef(c.getId()), // valid change message that should not be overwritten
    getChangeUpdateBody(c, "Removed reviewer <GERRIT_ACCOUNT_1>.", "Reviewer: " + reviewerIdentToFix), getAuthorIdent(changeOwner.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), // valid change message that should not be overwritten
    getChangeUpdateBody(c, "Removed cc <GERRIT_ACCOUNT_2> with the following votes:\n\n * Code-Review+2 by <GERRIT_ACCOUNT_2>", "CC: " + reviewerIdentToFix), getAuthorIdent(otherUser.getAccount()))).add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Removed cc", "Removed: " + reviewerIdentToFix), getAuthorIdent(changeOwner.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<ReviewerStatusUpdate> expectedReviewerUpdates = ImmutableList.of(ReviewerStatusUpdate.create(updateTimestamp, changeOwner.getAccountId(), otherUserId, REVIEWER), ReviewerStatusUpdate.create(updateTimestamp, otherUserId, otherUserId, CC), ReviewerStatusUpdate.create(updateTimestamp, changeOwner.getAccountId(), otherUserId, REMOVED));
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(notesBeforeRewrite.getReviewerUpdates()).isEqualTo(expectedReviewerUpdates);
    assertThat(notesAfterRewrite.getReviewerUpdates()).isEqualTo(expectedReviewerUpdates);
    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("@@ -9 +9 @@\n" + "-Reviewer: Other Account <2@gerrit>\n" + "+Reviewer: Gerrit User 2 <2@gerrit>\n", "@@ -11 +11 @@\n" + "-CC: Other Account <2@gerrit>\n" + "+CC: Gerrit User 2 <2@gerrit>\n", "@@ -9 +9 @@\n" + "-Removed: Other Account <2@gerrit>\n" + "+Removed: Gerrit User 2 <2@gerrit>\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CommitDiff(com.google.gerrit.server.notedb.CommitRewriter.CommitDiff) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Gson(com.google.gson.Gson) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Map(java.util.Map) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Instant(java.time.Instant) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) VERIFIED(com.google.gerrit.entities.LabelId.VERIFIED) Ref(org.eclipse.jgit.lib.Ref) IntStream(java.util.stream.IntStream) LabelId(com.google.gerrit.entities.LabelId) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CC(com.google.gerrit.server.notedb.ReviewerStateInternal.CC) HashSet(java.util.HashSet) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ImmutableList(com.google.common.collect.ImmutableList) CODE_REVIEW(com.google.gerrit.entities.LabelId.CODE_REVIEW) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Before(org.junit.Before) Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) CurrentUser(com.google.gerrit.server.CurrentUser) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) REMOVED(com.google.gerrit.server.notedb.ReviewerStateInternal.REMOVED) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Repository(org.eclipse.jgit.lib.Repository) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) Change(com.google.gerrit.entities.Change) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

Change (com.google.gerrit.entities.Change)30 BackfillResult (com.google.gerrit.server.notedb.CommitRewriter.BackfillResult)30 RunOptions (com.google.gerrit.server.notedb.CommitRewriter.RunOptions)30 Test (org.junit.Test)29 Ref (org.eclipse.jgit.lib.Ref)21 RevCommit (org.eclipse.jgit.revwalk.RevCommit)15 PersonIdent (org.eclipse.jgit.lib.PersonIdent)14 ObjectId (org.eclipse.jgit.lib.ObjectId)13 ImmutableMap (com.google.common.collect.ImmutableMap)12 Account (com.google.gerrit.entities.Account)12 Instant (java.time.Instant)12 ImmutableList (com.google.common.collect.ImmutableList)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11 AttentionSetUpdate (com.google.gerrit.entities.AttentionSetUpdate)11 SubmitRecord (com.google.gerrit.entities.SubmitRecord)11 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)11 HashSet (java.util.HashSet)11 Map (java.util.Map)11 Truth.assertThat (com.google.common.truth.Truth.assertThat)10 Operation (com.google.gerrit.entities.AttentionSetUpdate.Operation)10