LCOV - code coverage report
Current view: top level - server/submit - EmailMerge.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 34 88.2 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.submit;
      16             : 
      17             : import com.google.common.base.Strings;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import com.google.gerrit.common.Nullable;
      20             : import com.google.gerrit.entities.Change;
      21             : import com.google.gerrit.entities.Project;
      22             : import com.google.gerrit.server.CurrentUser;
      23             : import com.google.gerrit.server.IdentifiedUser;
      24             : import com.google.gerrit.server.change.NotifyResolver;
      25             : import com.google.gerrit.server.config.SendEmailExecutor;
      26             : import com.google.gerrit.server.mail.send.MergedSender;
      27             : import com.google.gerrit.server.mail.send.MessageIdGenerator;
      28             : import com.google.gerrit.server.update.RepoView;
      29             : import com.google.gerrit.server.util.RequestContext;
      30             : import com.google.gerrit.server.util.ThreadLocalRequestContext;
      31             : import com.google.inject.Inject;
      32             : import com.google.inject.OutOfScopeException;
      33             : import com.google.inject.assistedinject.Assisted;
      34             : import java.util.Optional;
      35             : import java.util.concurrent.ExecutorService;
      36             : import java.util.concurrent.Future;
      37             : 
      38             : class EmailMerge implements Runnable, RequestContext {
      39          53 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      40             : 
      41             :   interface Factory {
      42             :     EmailMerge create(
      43             :         Project.NameKey project,
      44             :         Change change,
      45             :         IdentifiedUser submitter,
      46             :         NotifyResolver.Result notify,
      47             :         RepoView repoView,
      48             :         String stickyApprovalDiff);
      49             :   }
      50             : 
      51             :   private final ExecutorService sendEmailsExecutor;
      52             :   private final MergedSender.Factory mergedSenderFactory;
      53             :   private final ThreadLocalRequestContext requestContext;
      54             :   private final MessageIdGenerator messageIdGenerator;
      55             : 
      56             :   private final Project.NameKey project;
      57             :   private final Change change;
      58             :   private final IdentifiedUser submitter;
      59             :   private final NotifyResolver.Result notify;
      60             :   private final RepoView repoView;
      61             :   private final String stickyApprovalDiff;
      62             : 
      63             :   @Inject
      64             :   EmailMerge(
      65             :       @SendEmailExecutor ExecutorService executor,
      66             :       MergedSender.Factory mergedSenderFactory,
      67             :       ThreadLocalRequestContext requestContext,
      68             :       MessageIdGenerator messageIdGenerator,
      69             :       @Assisted Project.NameKey project,
      70             :       @Assisted Change change,
      71             :       @Assisted @Nullable IdentifiedUser submitter,
      72             :       @Assisted NotifyResolver.Result notify,
      73             :       @Assisted RepoView repoView,
      74          53 :       @Assisted String stickyApprovalDiff) {
      75          53 :     this.sendEmailsExecutor = executor;
      76          53 :     this.mergedSenderFactory = mergedSenderFactory;
      77          53 :     this.requestContext = requestContext;
      78          53 :     this.messageIdGenerator = messageIdGenerator;
      79          53 :     this.project = project;
      80          53 :     this.change = change;
      81          53 :     this.submitter = submitter;
      82          53 :     this.notify = notify;
      83          53 :     this.repoView = repoView;
      84          53 :     this.stickyApprovalDiff = stickyApprovalDiff;
      85          53 :   }
      86             : 
      87             :   void sendAsync() {
      88             :     @SuppressWarnings("unused")
      89          53 :     Future<?> possiblyIgnoredError = sendEmailsExecutor.submit(this);
      90          53 :   }
      91             : 
      92             :   @Override
      93             :   public void run() {
      94          53 :     RequestContext old = requestContext.setContext(this);
      95             :     try {
      96          53 :       MergedSender emailSender =
      97          53 :           mergedSenderFactory.create(
      98             :               project,
      99          53 :               change.getId(),
     100          53 :               Optional.ofNullable(Strings.emptyToNull(stickyApprovalDiff)));
     101          53 :       if (submitter != null) {
     102          53 :         emailSender.setFrom(submitter.getAccountId());
     103             :       }
     104          53 :       emailSender.setNotify(notify);
     105          53 :       emailSender.setMessageId(
     106          53 :           messageIdGenerator.fromChangeUpdate(repoView, change.currentPatchSetId()));
     107          53 :       emailSender.send();
     108           0 :     } catch (Exception e) {
     109           0 :       logger.atSevere().withCause(e).log("Cannot email merged notification for %s", change.getId());
     110             :     } finally {
     111          53 :       requestContext.setContext(old);
     112             :     }
     113          53 :   }
     114             : 
     115             :   @Override
     116             :   public String toString() {
     117           0 :     return "send-email merged";
     118             :   }
     119             : 
     120             :   @Override
     121             :   public CurrentUser getUser() {
     122          53 :     if (submitter != null) {
     123          53 :       return submitter;
     124             :     }
     125           0 :     throw new OutOfScopeException("No user on email thread");
     126             :   }
     127             : }

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