Search in sources :

Example 16 with RunOptions

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

the class CommitRewriterTest method fixAttentionFooter_okReason_noRewrite.

@Test
public void fixAttentionFooter_okReason_noRewrite() throws Exception {
    Change c = newChange();
    ImmutableList<String> okAccountNames = ImmutableList.of("Someone", "Someone else", "someone", "someone else", "Anonymous", "anonymous", "<GERRIT_ACCOUNT_1>", "<GERRIT_ACCOUNT_2>");
    ImmutableList.Builder<AttentionSetUpdate> attentionSetUpdatesBeforeRewrite = new ImmutableList.Builder<>();
    for (String okAccountName : okAccountNames) {
        ChangeUpdate firstAttentionSetUpdate = newUpdate(c, changeOwner);
        firstAttentionSetUpdate.putReviewer(otherUserId, REVIEWER);
        firstAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", okAccountName)));
        firstAttentionSetUpdate.commit();
        ChangeUpdate secondAttentionSetUpdate = newUpdate(c, changeOwner);
        secondAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", okAccountName)));
        secondAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", okAccountName)));
        secondAttentionSetUpdate.commit();
        ChangeUpdate thirdAttentionSetUpdate = newUpdate(c, changeOwner);
        thirdAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", okAccountName)));
        thirdAttentionSetUpdate.commit();
        attentionSetUpdatesBeforeRewrite.add(AttentionSetUpdate.createFromRead(thirdAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", okAccountName)), AttentionSetUpdate.createFromRead(secondAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", okAccountName)), AttentionSetUpdate.createFromRead(secondAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", okAccountName)), AttentionSetUpdate.createFromRead(firstAttentionSetUpdate.getWhen(), otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", okAccountName)));
    }
    ChangeNotes notesBeforeRewrite = newNotes(c);
    assertThat(notesBeforeRewrite.getAttentionSetUpdates()).containsExactlyElementsIn(attentionSetUpdatesBeforeRewrite.build());
    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();
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : Ref(org.eclipse.jgit.lib.Ref) BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Change(com.google.gerrit.entities.Change) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Test(org.junit.Test)

Example 17 with RunOptions

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

the class CommitRewriterTest method fixReviewerMessage.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixReviewerMessage() throws Exception {
    Change c = newChange();
    ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
    ChangeUpdate addReviewerUpdate = newUpdate(c, changeOwner);
    addReviewerUpdate.putReviewer(otherUserId, REVIEWER);
    addReviewerUpdate.commit();
    commitsToFix.add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, String.format("Removed reviewer %s.", otherUser.getAccount().fullName()), "Removed: " + getValidIdentAsString(otherUser.getAccount())), getAuthorIdent(changeOwner.getAccount())));
    ChangeUpdate addCcUpdate = newUpdate(c, changeOwner);
    addCcUpdate.putReviewer(otherUserId, CC);
    addCcUpdate.commit();
    commitsToFix.add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, String.format("Removed cc %s with the following votes:\n\n * Code-Review+2", otherUser.getAccount().fullName()), "Removed: " + getValidIdentAsString(otherUser.getAccount())), getAuthorIdent(changeOwner.getAccount())));
    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()));
    Instant updateTimestamp = serverIdent.getWhen().toInstant();
    ImmutableList<ReviewerStatusUpdate> expectedReviewerUpdates = ImmutableList.of(ReviewerStatusUpdate.create(addReviewerUpdate.when, changeOwner.getAccountId(), otherUserId, REVIEWER), ReviewerStatusUpdate.create(updateTimestamp, changeOwner.getAccountId(), otherUserId, REMOVED), ReviewerStatusUpdate.create(addCcUpdate.when, changeOwner.getAccountId(), otherUserId, CC), ReviewerStatusUpdate.create(updateTimestamp, changeOwner.getAccountId(), otherUserId, REMOVED));
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(notesBeforeRewrite.getReviewerUpdates()).isEqualTo(expectedReviewerUpdates);
    assertThat(changeMessages(notesBeforeRewrite)).containsExactly("Removed reviewer Other Account.", "Removed cc Other Account with the following votes:\n\n * Code-Review+2");
    assertThat(notesAfterRewrite.getReviewerUpdates()).isEqualTo(expectedReviewerUpdates);
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Removed reviewer", "Removed cc");
    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" + "-Removed reviewer Other Account.\n" + "+Removed reviewer\n", "@@ -6,3 +6 @@\n" + "-Removed cc Other Account with the following votes:\n" + "-\n" + "- * Code-Review+2\n" + "+Removed cc\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) 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)

Example 18 with RunOptions

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

the class CommitRewriterTest method fixRemoveVoteChangeMessageWithNoFooterLabel_matchByName.

@Test
public void fixRemoveVoteChangeMessageWithNoFooterLabel_matchByName() throws Exception {
    Change c = newChange();
    ChangeUpdate approvalUpdate = newUpdate(c, changeOwner);
    approvalUpdate.putApproval(VERIFIED, (short) +2);
    approvalUpdate.putApprovalFor(otherUserId, VERIFIED, (short) -1);
    approvalUpdate.commit();
    writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed Verified+2 by Change Owner"), 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\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 : BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Change(com.google.gerrit.entities.Change) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) Test(org.junit.Test)

Example 19 with RunOptions

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

the class CommitRewriterTest method fixRealUserFooterIdent.

@Test
public void fixRealUserFooterIdent() throws Exception {
    Change c = newChange();
    String realUserIdentToFix = getAccountIdentToFix(otherUser.getAccount());
    ObjectId invalidUpdateCommit = writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Comment on behalf of user", "Real-user: " + realUserIdentToFix), getAuthorIdent(changeOwner.getAccount()));
    IdentifiedUser impersonatedChangeOwner = this.userFactory.runAs(null, changeOwner.getAccountId(), requireNonNull(otherUser).getRealUser());
    ChangeUpdate impersonatedChangeMessageUpdate = newUpdate(c, impersonatedChangeOwner);
    impersonatedChangeMessageUpdate.setChangeMessage("Other comment on behalf of");
    impersonatedChangeMessageUpdate.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(changeMessages(notesBeforeRewrite)).containsExactly("Comment on behalf of user", "Other comment on behalf of");
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getAuthor()).isEqualTo(changeOwner.getAccountId());
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getRealAuthor()).isEqualTo(otherUser.getAccountId());
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Comment on behalf of user", "Other comment on behalf of");
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getAuthor()).isEqualTo(changeOwner.getAccountId());
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getRealAuthor()).isEqualTo(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, ImmutableList.of(invalidCommitIndex));
    assertFixedCommits(ImmutableList.of(invalidUpdateCommit), result, c.getId());
    List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
    assertThat(commitHistoryDiff).containsExactly("@@ -9 +9 @@\n" + "-Real-user: Other Account <2@gerrit>\n" + "+Real-user: Gerrit User 2 <2@gerrit>\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) 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 20 with RunOptions

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

the class CommitRewriterTest method fixRemoveVotesChangeMessage.

@Test
public void fixRemoveVotesChangeMessage() throws Exception {
    Change c = newChange();
    ChangeUpdate approvalUpdate = newUpdate(c, changeOwner);
    approvalUpdate.putApproval(VERIFIED, (short) +2);
    approvalUpdate.putApprovalFor(otherUserId, VERIFIED, (short) -1);
    approvalUpdate.commit();
    writeUpdate(RefNames.changeMetaRef(c.getId()), // Even though footer is missing, accounts are matched among the account in change updates.
    getChangeUpdateBody(c, /*changeMessage=*/
    "Removed the following votes:\n" + String.format("* Verified-1 by %s\n", otherUser.getNameEmail())), getAuthorIdent(changeOwner.getAccount()));
    writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed the following votes:\n" + String.format("* Verified+2 by %s\n", changeOwner.getNameEmail()) + String.format("* Verified-1 by %s\n", changeOwner.getNameEmail()) + String.format("* Code-Review by %s\n", otherUser.getNameEmail())), getAuthorIdent(changeOwner.getAccount()));
    // No rewrite for default
    writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    "Removed the following votes:\n" + "* Verified+2 by Gerrit Account\n" + "* Verified-1 by <GERRIT_ACCOUNT_2>\n"), 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("@@ -7 +7 @@\n" + "-* Verified-1 by Other Account <other@account.com>\n" + "+* Verified-1 by <GERRIT_ACCOUNT_2>\n", "@@ -7,3 +7,3 @@\n" + "-* Verified+2 by Change Owner <change@owner.com>\n" + "-* Verified-1 by Change Owner <change@owner.com>\n" + "-* Code-Review by Other Account <other@account.com>\n" + "+* Verified+2 by <GERRIT_ACCOUNT_1>\n" + "+* Verified-1 by <GERRIT_ACCOUNT_1>\n" + "+* Code-Review by <GERRIT_ACCOUNT_2>\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) Change(com.google.gerrit.entities.Change) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) 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