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.mail.send; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.gerrit.common.Nullable; 21 : import com.google.gerrit.entities.Account; 22 : import com.google.gerrit.entities.PatchSet; 23 : import com.google.gerrit.entities.Project; 24 : import com.google.gerrit.entities.RefNames; 25 : import com.google.gerrit.exceptions.StorageException; 26 : import com.google.gerrit.mail.MailMessage; 27 : import com.google.gerrit.server.config.AllUsersName; 28 : import com.google.gerrit.server.git.GitRepositoryManager; 29 : import com.google.gerrit.server.update.RepoView; 30 : import com.google.inject.Inject; 31 : import java.io.IOException; 32 : import java.time.Instant; 33 : import java.util.Optional; 34 : import org.eclipse.jgit.lib.ObjectId; 35 : import org.eclipse.jgit.lib.Ref; 36 : import org.eclipse.jgit.lib.Repository; 37 : 38 : /** A generator class that creates a {@link MessageId} */ 39 : public class MessageIdGenerator { 40 : private final GitRepositoryManager repositoryManager; 41 : private final AllUsersName allUsersName; 42 : 43 : @Inject 44 150 : public MessageIdGenerator(GitRepositoryManager repositoryManager, AllUsersName allUsersName) { 45 150 : this.repositoryManager = repositoryManager; 46 150 : this.allUsersName = allUsersName; 47 150 : } 48 : 49 : /** 50 : * A unique id used which is a part of the header of all emails sent through by Gerrit. All of the 51 : * emails are sent via {@link OutgoingEmail#send()}. 52 : */ 53 : @AutoValue 54 106 : public abstract static class MessageId { 55 : public abstract String id(); 56 : } 57 : 58 : /** 59 : * Create a {@link MessageId} as a result of a change update. 60 : * 61 : * @return MessageId that depends on the patchset. 62 : */ 63 : public MessageId fromChangeUpdate(RepoView repoView, PatchSet.Id patchsetId) { 64 103 : return fromChangeUpdateAndReason(repoView, patchsetId, null); 65 : } 66 : 67 : public MessageId fromChangeUpdateAndReason( 68 : RepoView repoView, PatchSet.Id patchsetId, @Nullable String reason) { 69 103 : String suffix = (reason != null) ? ("-" + reason) : ""; 70 103 : String metaRef = patchsetId.changeId().toRefPrefix() + "meta"; 71 : Optional<ObjectId> metaSha1; 72 : try { 73 103 : metaSha1 = repoView.getRef(metaRef); 74 0 : } catch (IOException ex) { 75 0 : throw new StorageException("unable to extract info for Message-Id", ex); 76 103 : } 77 103 : return metaSha1 78 103 : .map(optional -> new AutoValue_MessageIdGenerator_MessageId(optional.getName() + suffix)) 79 103 : .orElseThrow(() -> new IllegalStateException(metaRef + " doesn't exist")); 80 : } 81 : 82 : public MessageId fromChangeUpdate(Project.NameKey project, PatchSet.Id patchsetId) { 83 31 : String metaRef = patchsetId.changeId().toRefPrefix() + "meta"; 84 31 : Ref ref = getRef(metaRef, project); 85 31 : checkState(ref != null, metaRef + " must exist"); 86 31 : return new AutoValue_MessageIdGenerator_MessageId(ref.getObjectId().getName()); 87 : } 88 : 89 : /** 90 : * Create a {@link MessageId} as a result of an account update 91 : * 92 : * @return {@link MessageId} that depends on the account id. 93 : */ 94 : public MessageId fromAccountUpdate(Account.Id accountId) { 95 7 : String userRef = RefNames.refsUsers(accountId); 96 7 : Ref ref = getRef(userRef, allUsersName); 97 7 : checkState(ref != null, userRef + " must exist"); 98 7 : return new AutoValue_MessageIdGenerator_MessageId(ref.getObjectId().getName()); 99 : } 100 : 101 : /** 102 : * Create a {@link MessageId} from a mail message. 103 : * 104 : * @param mailMessage The message that was sent but was rejected. 105 : * @return MessageId that depends on the MailMessage that was rejected. 106 : */ 107 : public MessageId fromMailMessage(MailMessage mailMessage) { 108 3 : return new AutoValue_MessageIdGenerator_MessageId(mailMessage.id() + "-REJECTION"); 109 : } 110 : 111 : /** 112 : * Create a {@link MessageId} from a reason, Account.Id, and timestamp. 113 : * 114 : * @param reason for performing this account update 115 : * @return MessageId that depends on the reason, accountId, and timestamp. 116 : */ 117 : public MessageId fromReasonAccountIdAndTimestamp( 118 : String reason, Account.Id accountId, Instant timestamp) { 119 6 : return new AutoValue_MessageIdGenerator_MessageId( 120 6 : reason + "-" + accountId.toString() + "-" + timestamp.toString()); 121 : } 122 : 123 : private Ref getRef(String userRef, Project.NameKey project) { 124 35 : try (Repository repository = repositoryManager.openRepository(project)) { 125 35 : return repository.getRefDatabase().findRef(userRef); 126 0 : } catch (IOException ex) { 127 0 : throw new StorageException("unable to extract info for Message-Id", ex); 128 : } 129 : } 130 : }