Search in sources :

Example 1 with Operation

use of com.google.gerrit.entities.AttentionSetUpdate.Operation in project gerrit by GerritCodeReview.

the class RemoveFromAttentionSetOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws RestApiException {
    ChangeData changeData = changeDataFactory.create(ctx.getNotes());
    Optional<AttentionSetUpdate> existingEntry = changeData.attentionSet().stream().filter(u -> u.account().equals(attentionUserId)).findAny();
    if (!existingEntry.isPresent() || existingEntry.get().operation() == Operation.REMOVE) {
        // We still need to perform this update to ensure that we don't add the user in a follow-up
        // operation, but no need to send an email about it.
        notify = false;
    }
    change = ctx.getChange();
    ChangeUpdate update = ctx.getUpdate(ctx.getChange().currentPatchSetId());
    update.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(attentionUserId, Operation.REMOVE, reason));
    return true;
}
Also used : Operation(com.google.gerrit.entities.AttentionSetUpdate.Operation) Inject(com.google.inject.Inject) Account(com.google.gerrit.entities.Account) IOException(java.io.IOException) Assisted(com.google.inject.assistedinject.Assisted) MessageIdGenerator(com.google.gerrit.server.mail.send.MessageIdGenerator) RemoveFromAttentionSetSender(com.google.gerrit.server.mail.send.RemoveFromAttentionSetSender) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) PostUpdateContext(com.google.gerrit.server.update.PostUpdateContext) AttentionSetEmail(com.google.gerrit.server.util.AttentionSetEmail) Objects.requireNonNull(java.util.Objects.requireNonNull) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Optional(java.util.Optional) Change(com.google.gerrit.entities.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) ChangeContext(com.google.gerrit.server.update.ChangeContext) FluentLogger(com.google.common.flogger.FluentLogger) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 2 with Operation

use of com.google.gerrit.entities.AttentionSetUpdate.Operation in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixAttentionFooter.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixAttentionFooter() throws Exception {
    Change c = newChange();
    ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
    // Only 'reason' fix is required
    ChangeUpdate invalidAttentionSetUpdate = newUpdate(c, changeOwner);
    invalidAttentionSetUpdate.putReviewer(otherUserId, REVIEWER);
    invalidAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", otherUser.getName())));
    commitsToFix.add(invalidAttentionSetUpdate.commit());
    ChangeUpdate invalidMultipleAttentionSetUpdate = newUpdate(c, changeOwner);
    invalidMultipleAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", otherUser.getName())));
    invalidMultipleAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", otherUser.getName())));
    commitsToFix.add(invalidMultipleAttentionSetUpdate.commit());
    String otherUserIdentToFix = getAccountIdentToFix(otherUser.getAccount());
    String changeOwnerIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
    commitsToFix.add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
    null, // Only 'person_ident' fix is required
    "Attention: " + gson.toJson(new AttentionStatusInNoteDb(otherUserIdentToFix, Operation.ADD, "Added by someone using the hovercard menu")), // Both 'reason' and 'person_ident' fix is required
    "Attention: " + gson.toJson(new AttentionStatusInNoteDb(changeOwnerIdentToFix, Operation.REMOVE, String.format("%s replied on the change", otherUser.getName())))), getAuthorIdent(changeOwner.getAccount())));
    ChangeUpdate validAttentionSetUpdate = newUpdate(c, changeOwner);
    validAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.REMOVE, "Removed by someone"));
    validAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.ADD, "Added by someone"));
    validAttentionSetUpdate.commit();
    ChangeUpdate invalidRemovedByClickUpdate = newUpdate(c, changeOwner);
    invalidRemovedByClickUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", otherUser.getName())));
    commitsToFix.add(invalidRemovedByClickUpdate.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()));
    notesBeforeRewrite.getAttentionSetUpdates();
    Instant updateTimestamp = serverIdent.getWhen().toInstant();
    ImmutableList<AttentionSetUpdate> attentionSetUpdatesBeforeRewrite = ImmutableList.of(AttentionSetUpdate.createFromRead(invalidRemovedByClickUpdate.getWhen(), changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", otherUser.getName())), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Added by someone"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone"), AttentionSetUpdate.createFromRead(updateTimestamp, changeOwner.getAccountId(), Operation.REMOVE, String.format("%s replied on the change", otherUser.getName())), AttentionSetUpdate.createFromRead(updateTimestamp, otherUserId, Operation.ADD, "Added by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", otherUser.getName())), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", otherUser.getName())), AttentionSetUpdate.createFromRead(invalidAttentionSetUpdate.getWhen(), otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", otherUser.getName())));
    ImmutableList<AttentionSetUpdate> attentionSetUpdatesAfterRewrite = ImmutableList.of(AttentionSetUpdate.createFromRead(invalidRemovedByClickUpdate.getWhen(), changeOwner.getAccountId(), Operation.REMOVE, "Removed by someone by clicking the attention icon"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Added by someone"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone"), AttentionSetUpdate.createFromRead(updateTimestamp, changeOwner.getAccountId(), Operation.REMOVE, "Someone replied on the change"), AttentionSetUpdate.createFromRead(updateTimestamp, otherUserId, Operation.ADD, "Added by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Someone replied on the change"), AttentionSetUpdate.createFromRead(invalidAttentionSetUpdate.getWhen(), otherUserId, Operation.ADD, "Added by someone using the hovercard menu"));
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(notesBeforeRewrite.getAttentionSetUpdates()).containsExactlyElementsIn(attentionSetUpdatesBeforeRewrite);
    assertThat(notesAfterRewrite.getAttentionSetUpdates()).containsExactlyElementsIn(attentionSetUpdatesAfterRewrite);
    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).hasSize(4);
    assertThat(commitHistoryDiff.get(0)).isEqualTo("@@ -8 +8 @@\n" + "-Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by Other Account using the hovercard menu\"}\n" + "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}\n");
    assertThat(Arrays.asList(commitHistoryDiff.get(1).split("\n"))).containsExactly("@@ -7,2 +7,2 @@", "-Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Other Account replied on the change\"}", "-Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by Other Account using the hovercard menu\"}", "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Someone replied on the change\"}", "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by someone using the hovercard menu\"}");
    assertThat(Arrays.asList(commitHistoryDiff.get(2).split("\n"))).containsExactly("@@ -7,2 +7,2 @@", "-Attention: {\"person_ident\":\"Other Account \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}", "-Attention: {\"person_ident\":\"Change Owner \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Other Account replied on the change\"}", "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}", "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Someone replied on the change\"}");
    assertThat(commitHistoryDiff.get(3)).isEqualTo("@@ -7 +7 @@\n" + "-Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by Other Account by clicking the attention icon\"}\n" + "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by someone by clicking the attention icon\"}\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) AttentionSetUpdate(com.google.gerrit.entities.AttentionSetUpdate) Instant(java.time.Instant) Change(com.google.gerrit.entities.Change) Ref(org.eclipse.jgit.lib.Ref) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) 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)2 AttentionSetUpdate (com.google.gerrit.entities.AttentionSetUpdate)2 Operation (com.google.gerrit.entities.AttentionSetUpdate.Operation)2 Change (com.google.gerrit.entities.Change)2 Inject (com.google.inject.Inject)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FluentLogger (com.google.common.flogger.FluentLogger)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ChangeMessage (com.google.gerrit.entities.ChangeMessage)1 LabelId (com.google.gerrit.entities.LabelId)1 CODE_REVIEW (com.google.gerrit.entities.LabelId.CODE_REVIEW)1 VERIFIED (com.google.gerrit.entities.LabelId.VERIFIED)1 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)1 RefNames (com.google.gerrit.entities.RefNames)1 SubmitRecord (com.google.gerrit.entities.SubmitRecord)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 RefUpdateUtil (com.google.gerrit.git.RefUpdateUtil)1