LCOV - code coverage report
Current view: top level - server/mail/send - AddKeySender.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 39 41 95.1 %
Date: 2022-11-19 15:00:39 Functions: 10 10 100.0 %

          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.mail.send;
      16             : 
      17             : import com.google.common.base.Joiner;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.gerrit.exceptions.EmailException;
      20             : import com.google.gerrit.extensions.api.changes.RecipientType;
      21             : import com.google.gerrit.server.IdentifiedUser;
      22             : import com.google.gerrit.server.account.AccountSshKey;
      23             : import com.google.inject.assistedinject.Assisted;
      24             : import com.google.inject.assistedinject.AssistedInject;
      25             : import java.util.List;
      26             : 
      27             : /** Sender that informs a user by email about the addition of an SSH or GPG key to their account. */
      28             : public class AddKeySender extends OutgoingEmail {
      29             :   public interface Factory {
      30             :     AddKeySender create(IdentifiedUser user, AccountSshKey sshKey);
      31             : 
      32             :     AddKeySender create(IdentifiedUser user, List<String> gpgKey);
      33             :   }
      34             : 
      35             :   private final IdentifiedUser user;
      36             :   private final AccountSshKey sshKey;
      37             :   private final List<String> gpgKeys;
      38             :   private final MessageIdGenerator messageIdGenerator;
      39             : 
      40             :   @AssistedInject
      41             :   public AddKeySender(
      42             :       EmailArguments args,
      43             :       MessageIdGenerator messageIdGenerator,
      44             :       @Assisted IdentifiedUser user,
      45             :       @Assisted AccountSshKey sshKey) {
      46           5 :     super(args, "addkey");
      47           5 :     this.messageIdGenerator = messageIdGenerator;
      48           5 :     this.user = user;
      49           5 :     this.sshKey = sshKey;
      50           5 :     this.gpgKeys = null;
      51           5 :   }
      52             : 
      53             :   @AssistedInject
      54             :   public AddKeySender(
      55             :       EmailArguments args,
      56             :       MessageIdGenerator messageIdGenerator,
      57             :       @Assisted IdentifiedUser user,
      58             :       @Assisted List<String> gpgKeys) {
      59           2 :     super(args, "addkey");
      60           2 :     this.messageIdGenerator = messageIdGenerator;
      61           2 :     this.user = user;
      62           2 :     this.sshKey = null;
      63           2 :     this.gpgKeys = gpgKeys;
      64           2 :   }
      65             : 
      66             :   @Override
      67             :   protected void init() throws EmailException {
      68           6 :     super.init();
      69           6 :     setHeader("Subject", String.format("[Gerrit Code Review] New %s Keys Added", getKeyType()));
      70           6 :     setMessageId(messageIdGenerator.fromAccountUpdate(user.getAccountId()));
      71           6 :     add(RecipientType.TO, user.getAccountId());
      72           6 :   }
      73             : 
      74             :   @Override
      75             :   protected boolean shouldSendMessage() {
      76           6 :     if (sshKey == null && (gpgKeys == null || gpgKeys.isEmpty())) {
      77             :       // Don't email if no keys were added.
      78           0 :       return false;
      79             :     }
      80             : 
      81           6 :     return true;
      82             :   }
      83             : 
      84             :   @Override
      85             :   protected void format() throws EmailException {
      86           6 :     appendText(textTemplate("AddKey"));
      87           6 :     if (useHtml()) {
      88           6 :       appendHtml(soyHtmlTemplate("AddKeyHtml"));
      89             :     }
      90           6 :   }
      91             : 
      92             :   @Override
      93             :   protected void setupSoyContext() {
      94           6 :     super.setupSoyContext();
      95           6 :     soyContextEmailData.put("email", getEmail());
      96           6 :     soyContextEmailData.put("gpgKeys", getGpgKeys());
      97           6 :     soyContextEmailData.put("keyType", getKeyType());
      98           6 :     soyContextEmailData.put("sshKey", getSshKey());
      99           6 :     soyContextEmailData.put("userNameEmail", getUserNameEmailFor(user.getAccountId()));
     100           6 :   }
     101             : 
     102             :   private String getEmail() {
     103           6 :     return user.getAccount().preferredEmail();
     104             :   }
     105             : 
     106             :   private String getKeyType() {
     107           6 :     if (sshKey != null) {
     108           5 :       return "SSH";
     109           2 :     } else if (gpgKeys != null) {
     110           2 :       return "GPG";
     111             :     }
     112           0 :     return "Unknown";
     113             :   }
     114             : 
     115             :   @Nullable
     116             :   private String getSshKey() {
     117           6 :     return (sshKey != null) ? sshKey.sshPublicKey() + "\n" : null;
     118             :   }
     119             : 
     120             :   @Nullable
     121             :   private String getGpgKeys() {
     122           6 :     if (gpgKeys != null) {
     123           2 :       return Joiner.on("\n").join(gpgKeys);
     124             :     }
     125           5 :     return null;
     126             :   }
     127             : }

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