Search in sources :

Example 26 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class OutgoingEmail method toAddress.

private Address toAddress(Account.Id id) {
    Optional<Account> accountState = args.accountCache.get(id).map(AccountState::account);
    if (!accountState.isPresent()) {
        return null;
    }
    Account account = accountState.get();
    String e = account.preferredEmail();
    if (!account.isActive() || e == null) {
        return null;
    }
    return Address.create(account.fullName(), e);
}
Also used : Account(com.google.gerrit.entities.Account) AccountState(com.google.gerrit.server.account.AccountState)

Example 27 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class SetMembersCommand method reportMembersAction.

private void reportMembersAction(String action, GroupResource group, List<Account.Id> accountIdList) throws UnsupportedEncodingException, IOException {
    String names = accountIdList.stream().map(accountId -> {
        Optional<AccountState> accountState = accountCache.get(accountId);
        if (!accountState.isPresent()) {
            return "n/a";
        }
        return MoreObjects.firstNonNull(accountState.get().account().preferredEmail(), "n/a");
    }).collect(joining(", "));
    out.write(String.format("Members %s group %s: %s\n", action, group.getName(), names).getBytes(ENC));
}
Also used : CommandMetaData(com.google.gerrit.sshd.CommandMetaData) InternalGroup(com.google.gerrit.entities.InternalGroup) AccountCache(com.google.gerrit.server.account.AccountCache) GroupResource(com.google.gerrit.server.group.GroupResource) DeleteSubgroups(com.google.gerrit.server.restapi.group.DeleteSubgroups) IdString(com.google.gerrit.extensions.restapi.IdString) Inject(com.google.inject.Inject) DeleteMembers(com.google.gerrit.server.restapi.group.DeleteMembers) ArrayList(java.util.ArrayList) AddSubgroups(com.google.gerrit.server.restapi.group.AddSubgroups) GroupsCollection(com.google.gerrit.server.restapi.group.GroupsCollection) Argument(org.kohsuke.args4j.Argument) AddMembers(com.google.gerrit.server.restapi.group.AddMembers) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) AccountGroup(com.google.gerrit.entities.AccountGroup) MoreObjects(com.google.common.base.MoreObjects) Account(com.google.gerrit.entities.Account) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) Streams(com.google.common.collect.Streams) Collectors.joining(java.util.stream.Collectors.joining) TopLevelResource(com.google.gerrit.extensions.restapi.TopLevelResource) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SshCommand(com.google.gerrit.sshd.SshCommand) Optional(java.util.Optional) AccountState(com.google.gerrit.server.account.AccountState) GroupCache(com.google.gerrit.server.account.GroupCache) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Optional(java.util.Optional) IdString(com.google.gerrit.extensions.restapi.IdString)

Example 28 with Account

use of com.google.gerrit.entities.Account 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 29 with Account

use of com.google.gerrit.entities.Account 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 30 with Account

use of com.google.gerrit.entities.Account 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)

Aggregations

Account (com.google.gerrit.entities.Account)124 Test (org.junit.Test)59 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)37 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)35 AccountState (com.google.gerrit.server.account.AccountState)35 IOException (java.io.IOException)31 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.entities.Change)28 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 Inject (com.google.inject.Inject)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)25 List (java.util.List)24 ArrayList (java.util.ArrayList)23 HashSet (java.util.HashSet)23 Set (java.util.Set)22 RefNames (com.google.gerrit.entities.RefNames)21 AuthRequest (com.google.gerrit.server.account.AuthRequest)21 Map (java.util.Map)21 ObjectId (org.eclipse.jgit.lib.ObjectId)21 ImmutableList (com.google.common.collect.ImmutableList)20