LCOV - code coverage report
Current view: top level - server/change - RemoveFromAttentionSetOp.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 27 27 100.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2020 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.change;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.gerrit.entities.Account;
      20             : import com.google.gerrit.entities.AttentionSetUpdate;
      21             : import com.google.gerrit.entities.AttentionSetUpdate.Operation;
      22             : import com.google.gerrit.entities.Change;
      23             : import com.google.gerrit.extensions.restapi.RestApiException;
      24             : import com.google.gerrit.server.mail.send.RemoveFromAttentionSetSender;
      25             : import com.google.gerrit.server.notedb.ChangeUpdate;
      26             : import com.google.gerrit.server.query.change.ChangeData;
      27             : import com.google.gerrit.server.update.BatchUpdateOp;
      28             : import com.google.gerrit.server.update.ChangeContext;
      29             : import com.google.gerrit.server.update.PostUpdateContext;
      30             : import com.google.gerrit.server.util.AttentionSetEmail;
      31             : import com.google.inject.Inject;
      32             : import com.google.inject.assistedinject.Assisted;
      33             : import java.util.Optional;
      34             : 
      35             : /** Remove a specified user from the attention set. */
      36             : public class RemoveFromAttentionSetOp implements BatchUpdateOp {
      37             :   public interface Factory {
      38             :     RemoveFromAttentionSetOp create(Account.Id attentionUserId, String reason, boolean notify);
      39             :   }
      40             : 
      41             :   private final ChangeData.Factory changeDataFactory;
      42             :   private final RemoveFromAttentionSetSender.Factory removeFromAttentionSetSender;
      43             :   private final AttentionSetEmail.Factory attentionSetEmailFactory;
      44             : 
      45             :   private final Account.Id attentionUserId;
      46             :   private final String reason;
      47             : 
      48             :   private Change change;
      49             :   private boolean notify;
      50             : 
      51             :   /**
      52             :    * Remove a specified user from the attention set.
      53             :    *
      54             :    * @param attentionUserId the id of the user we want to add to the attention set.
      55             :    * @param reason the reason for adding that user.
      56             :    * @param notify whether or not to send emails if the operation is successful.
      57             :    */
      58             :   @Inject
      59             :   RemoveFromAttentionSetOp(
      60             :       ChangeData.Factory changeDataFactory,
      61             :       RemoveFromAttentionSetSender.Factory removeFromAttentionSetSenderFactory,
      62             :       AttentionSetEmail.Factory attentionSetEmailFactory,
      63             :       @Assisted Account.Id attentionUserId,
      64             :       @Assisted String reason,
      65          65 :       @Assisted boolean notify) {
      66          65 :     this.changeDataFactory = changeDataFactory;
      67          65 :     this.removeFromAttentionSetSender = removeFromAttentionSetSenderFactory;
      68          65 :     this.attentionSetEmailFactory = attentionSetEmailFactory;
      69          65 :     this.attentionUserId = requireNonNull(attentionUserId, "user");
      70          65 :     this.reason = requireNonNull(reason, "reason");
      71          65 :     this.notify = notify;
      72          65 :   }
      73             : 
      74             :   @Override
      75             :   public boolean updateChange(ChangeContext ctx) throws RestApiException {
      76          65 :     ChangeData changeData = changeDataFactory.create(ctx.getNotes());
      77          65 :     Optional<AttentionSetUpdate> existingEntry =
      78          65 :         changeData.attentionSet().stream()
      79          65 :             .filter(u -> u.account().equals(attentionUserId))
      80          65 :             .findAny();
      81          65 :     if (!existingEntry.isPresent() || existingEntry.get().operation() == Operation.REMOVE) {
      82             :       // We still need to perform this update to ensure that we don't add the user in a follow-up
      83             :       // operation, but no need to send an email about it.
      84          65 :       notify = false;
      85             :     }
      86             : 
      87          65 :     change = ctx.getChange();
      88             : 
      89          65 :     ChangeUpdate changeUpdate = ctx.getUpdate(ctx.getChange().currentPatchSetId());
      90          65 :     changeUpdate.addToPlannedAttentionSetUpdates(
      91          65 :         AttentionSetUpdate.createForWrite(attentionUserId, Operation.REMOVE, reason));
      92          65 :     return true;
      93             :   }
      94             : 
      95             :   @Override
      96             :   public void postUpdate(PostUpdateContext ctx) {
      97          65 :     if (!notify) {
      98          65 :       return;
      99             :     }
     100          13 :     attentionSetEmailFactory
     101          13 :         .create(
     102          13 :             removeFromAttentionSetSender.create(ctx.getProject(), change.getId()),
     103             :             ctx,
     104             :             change,
     105             :             reason,
     106             :             attentionUserId)
     107          13 :         .sendAsync();
     108          13 :   }
     109             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750