Line data Source code
1 : // Copyright (C) 2017 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.update; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.gerrit.entities.Account; 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.account.AccountState; 25 : import com.google.gerrit.server.change.NotifyResolver; 26 : import java.io.IOException; 27 : import java.time.Instant; 28 : import java.time.ZoneId; 29 : import org.eclipse.jgit.lib.PersonIdent; 30 : import org.eclipse.jgit.revwalk.RevWalk; 31 : 32 : /** 33 : * Context for performing a {@link BatchUpdate}. 34 : * 35 : * <p>A single update may span multiple changes, but they all belong to a single repo. 36 : */ 37 : public interface Context { 38 : /** 39 : * Get the project name this update operates on. 40 : * 41 : * @return project. 42 : */ 43 : Project.NameKey getProject(); 44 : 45 : /** 46 : * Get a read-only view of the open repository for this project. 47 : * 48 : * <p>Will be opened lazily if necessary. 49 : * 50 : * @return repository instance. 51 : * @throws IOException if an error occurred opening the repo. 52 : */ 53 : RepoView getRepoView() throws IOException; 54 : 55 : /** 56 : * Get a walk for this project. 57 : * 58 : * <p>The repository will be opened lazily if necessary; callers should not close the walk. 59 : * 60 : * @return walk. 61 : * @throws IOException if an error occurred opening the repo. 62 : */ 63 : RevWalk getRevWalk() throws IOException; 64 : 65 : /** 66 : * Get the timestamp at which this update takes place. 67 : * 68 : * @return timestamp. 69 : */ 70 : Instant getWhen(); 71 : 72 : /** 73 : * Get the time zone ID in which this update takes place. 74 : * 75 : * <p>In the current implementation, this is always the time zone ID of the server. 76 : * 77 : * @return zone ID. 78 : */ 79 : ZoneId getZoneId(); 80 : 81 : /** 82 : * Get the user performing the update. 83 : * 84 : * <p>In the current implementation, this is always an {@link IdentifiedUser} or {@link 85 : * com.google.gerrit.server.InternalUser}. 86 : * 87 : * @return user. 88 : */ 89 : CurrentUser getUser(); 90 : 91 : /** 92 : * Get the notification settings configured by the caller. 93 : * 94 : * <p>If there are multiple changes in a batch, they may have different settings. For example, WIP 95 : * changes may have reduced {@code NotifyHandling} levels, and may be in a batch with non-WIP 96 : * changes. 97 : * 98 : * @param changeId change ID 99 : * @return notification settings. 100 : */ 101 : NotifyResolver.Result getNotify(Change.Id changeId); 102 : 103 : /** 104 : * Get the identified user performing the update. 105 : * 106 : * <p>Convenience method for {@code getUser().asIdentifiedUser()}. 107 : * 108 : * @see CurrentUser#asIdentifiedUser() 109 : * @return user. 110 : */ 111 : default IdentifiedUser getIdentifiedUser() { 112 103 : return requireNonNull(getUser()).asIdentifiedUser(); 113 : } 114 : 115 : /** 116 : * Get the account of the user performing the update. 117 : * 118 : * <p>Convenience method for {@code getIdentifiedUser().account()}. 119 : * 120 : * @see CurrentUser#asIdentifiedUser() 121 : * @return account. 122 : */ 123 : default AccountState getAccount() { 124 103 : return getIdentifiedUser().state(); 125 : } 126 : 127 : /** 128 : * Get the account ID of the user performing the update. 129 : * 130 : * <p>Convenience method for {@code getUser().getAccountId()} 131 : * 132 : * @see CurrentUser#getAccountId() 133 : * @return account ID. 134 : */ 135 : default Account.Id getAccountId() { 136 103 : return getIdentifiedUser().getAccountId(); 137 : } 138 : 139 : /** 140 : * Creates a new {@link PersonIdent} with {@link #getWhen()} as timestamp. 141 : * 142 : * @param personIdent {@link PersonIdent} to be copied 143 : * @return copied {@link PersonIdent} with {@link #getWhen()} as timestamp 144 : */ 145 : default PersonIdent newPersonIdent(PersonIdent personIdent) { 146 0 : return new PersonIdent(personIdent, getWhen().toEpochMilli(), personIdent.getTimeZoneOffset()); 147 : } 148 : 149 : /** 150 : * Creates a committer {@link PersonIdent} for {@link #getIdentifiedUser()}. 151 : * 152 : * @return the created committer {@link PersonIdent} 153 : */ 154 : default PersonIdent newCommitterIdent() { 155 13 : return newCommitterIdent(getIdentifiedUser()); 156 : } 157 : 158 : /** 159 : * Creates a committer {@link PersonIdent} for the given user. 160 : * 161 : * @param user user for which a committer {@link PersonIdent} should be created 162 : * @return the created committer {@link PersonIdent} 163 : */ 164 : default PersonIdent newCommitterIdent(IdentifiedUser user) { 165 17 : return user.newCommitterIdent(getWhen(), getZoneId()); 166 : } 167 : }