LCOV - code coverage report
Current view: top level - server/change - AddToAttentionSetOp.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 26 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.Change;
      22             : import com.google.gerrit.extensions.restapi.RestApiException;
      23             : import com.google.gerrit.server.mail.send.AddToAttentionSetSender;
      24             : import com.google.gerrit.server.notedb.ChangeUpdate;
      25             : import com.google.gerrit.server.query.change.ChangeData;
      26             : import com.google.gerrit.server.update.BatchUpdateOp;
      27             : import com.google.gerrit.server.update.ChangeContext;
      28             : import com.google.gerrit.server.update.PostUpdateContext;
      29             : import com.google.gerrit.server.util.AttentionSetEmail;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.assistedinject.Assisted;
      32             : 
      33             : /** Add a specified user to the attention set. */
      34             : public class AddToAttentionSetOp implements BatchUpdateOp {
      35             :   public interface Factory {
      36             :     AddToAttentionSetOp create(Account.Id attentionUserId, String reason, boolean notify);
      37             :   }
      38             : 
      39             :   private final ChangeData.Factory changeDataFactory;
      40             :   private final AddToAttentionSetSender.Factory addToAttentionSetSender;
      41             :   private final AttentionSetEmail.Factory attentionSetEmailFactory;
      42             : 
      43             :   private final Account.Id attentionUserId;
      44             :   private final String reason;
      45             : 
      46             :   private Change change;
      47             :   private boolean notify;
      48             : 
      49             :   /**
      50             :    * Add a specified user to the attention set.
      51             :    *
      52             :    * @param attentionUserId the id of the user we want to add to the attention set.
      53             :    * @param reason the reason for adding that user.
      54             :    * @param notify whether or not to send emails if the operation is successful.
      55             :    */
      56             :   @Inject
      57             :   AddToAttentionSetOp(
      58             :       ChangeData.Factory changeDataFactory,
      59             :       AddToAttentionSetSender.Factory addToAttentionSetSender,
      60             :       AttentionSetEmail.Factory attentionSetEmailFactory,
      61             :       @Assisted Account.Id attentionUserId,
      62             :       @Assisted String reason,
      63          30 :       @Assisted boolean notify) {
      64          30 :     this.changeDataFactory = changeDataFactory;
      65          30 :     this.addToAttentionSetSender = addToAttentionSetSender;
      66          30 :     this.attentionSetEmailFactory = attentionSetEmailFactory;
      67             : 
      68          30 :     this.attentionUserId = requireNonNull(attentionUserId, "user");
      69          30 :     this.reason = requireNonNull(reason, "reason");
      70          30 :     this.notify = notify;
      71          30 :   }
      72             : 
      73             :   @Override
      74             :   public boolean updateChange(ChangeContext ctx) throws RestApiException {
      75          30 :     ChangeData changeData = changeDataFactory.create(ctx.getNotes());
      76          30 :     if (changeData.attentionSet().stream()
      77          30 :         .anyMatch(
      78             :             u ->
      79          14 :                 u.account().equals(attentionUserId)
      80          14 :                     && u.operation() == AttentionSetUpdate.Operation.ADD)) {
      81             :       // We still need to perform this update to ensure that we don't remove the user in a follow-up
      82             :       // operation, but no need to send an email about it.
      83           7 :       notify = false;
      84             :     }
      85             : 
      86          30 :     change = ctx.getChange();
      87             : 
      88          30 :     ChangeUpdate changeUpdate = ctx.getUpdate(ctx.getChange().currentPatchSetId());
      89          30 :     changeUpdate.addToPlannedAttentionSetUpdates(
      90          30 :         AttentionSetUpdate.createForWrite(
      91             :             attentionUserId, AttentionSetUpdate.Operation.ADD, reason));
      92          30 :     return true;
      93             :   }
      94             : 
      95             :   @Override
      96             :   public void postUpdate(PostUpdateContext ctx) {
      97          30 :     if (!notify) {
      98          30 :       return;
      99             :     }
     100           6 :     attentionSetEmailFactory
     101           6 :         .create(
     102           6 :             addToAttentionSetSender.create(ctx.getProject(), change.getId()),
     103             :             ctx,
     104             :             change,
     105             :             reason,
     106             :             attentionUserId)
     107           6 :         .sendAsync();
     108           6 :   }
     109             : }

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