Allow to update accounts and general preferences atomically
AccountsUpdate now also supports to update general preferences. This means account properties and general preferences can now be updated in the same transaction. Reading and writing general preferences is now done via AccountConfig. On load we always read the preferences.config file, but parsing it is done lazily when the general preferences are accessed for the first time. Most callers that load accounts are not interested in general preferences and this avoids unneeded parsing effort for them. PreferencesConfig encapsules all details about parsing and storing general preferences and also provides means to read and update the default general preferences. Change-Id: Ic0f2adfe4e96311af0c5bfa061fc7c0ca60d91fa Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -31,6 +31,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
|
||||
public class AccountValidator {
|
||||
@@ -45,12 +46,12 @@ public class AccountValidator {
|
||||
}
|
||||
|
||||
public List<String> validate(
|
||||
Account.Id accountId, RevWalk rw, @Nullable ObjectId oldId, ObjectId newId)
|
||||
Account.Id accountId, Repository repo, RevWalk rw, @Nullable ObjectId oldId, ObjectId newId)
|
||||
throws IOException {
|
||||
Optional<Account> oldAccount = Optional.empty();
|
||||
if (oldId != null && !ObjectId.zeroId().equals(oldId)) {
|
||||
try {
|
||||
oldAccount = loadAccount(accountId, rw, oldId, null);
|
||||
oldAccount = loadAccount(accountId, repo, rw, oldId, null);
|
||||
} catch (ConfigInvalidException e) {
|
||||
// ignore, maybe the new commit is repairing it now
|
||||
}
|
||||
@@ -59,7 +60,7 @@ public class AccountValidator {
|
||||
List<String> messages = new ArrayList<>();
|
||||
Optional<Account> newAccount;
|
||||
try {
|
||||
newAccount = loadAccount(accountId, rw, newId, messages);
|
||||
newAccount = loadAccount(accountId, repo, rw, newId, messages);
|
||||
} catch (ConfigInvalidException e) {
|
||||
return ImmutableList.of(
|
||||
String.format(
|
||||
@@ -91,10 +92,14 @@ public class AccountValidator {
|
||||
}
|
||||
|
||||
private Optional<Account> loadAccount(
|
||||
Account.Id accountId, RevWalk rw, ObjectId commit, @Nullable List<String> messages)
|
||||
Account.Id accountId,
|
||||
Repository repo,
|
||||
RevWalk rw,
|
||||
ObjectId commit,
|
||||
@Nullable List<String> messages)
|
||||
throws IOException, ConfigInvalidException {
|
||||
rw.reset();
|
||||
AccountConfig accountConfig = new AccountConfig(accountId);
|
||||
AccountConfig accountConfig = new AccountConfig(accountId, repo);
|
||||
accountConfig.setEagerParsing(true).load(rw, commit);
|
||||
if (messages != null) {
|
||||
messages.addAll(
|
||||
|
@@ -41,6 +41,7 @@ import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.events.CommitReceivedEvent;
|
||||
import com.google.gerrit.server.git.BanCommit;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.git.ProjectConfig;
|
||||
import com.google.gerrit.server.git.ValidationError;
|
||||
import com.google.gerrit.server.permissions.PermissionBackend;
|
||||
@@ -84,6 +85,7 @@ public class CommitValidators {
|
||||
private final PersonIdent gerritIdent;
|
||||
private final String canonicalWebUrl;
|
||||
private final DynamicSet<CommitValidationListener> pluginValidators;
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final AllUsersName allUsers;
|
||||
private final AllProjectsName allProjects;
|
||||
private final ExternalIdsConsistencyChecker externalIdsConsistencyChecker;
|
||||
@@ -97,6 +99,7 @@ public class CommitValidators {
|
||||
@CanonicalWebUrl @Nullable String canonicalWebUrl,
|
||||
@GerritServerConfig Config cfg,
|
||||
DynamicSet<CommitValidationListener> pluginValidators,
|
||||
GitRepositoryManager repoManager,
|
||||
AllUsersName allUsers,
|
||||
AllProjectsName allProjects,
|
||||
ExternalIdsConsistencyChecker externalIdsConsistencyChecker,
|
||||
@@ -105,6 +108,7 @@ public class CommitValidators {
|
||||
this.gerritIdent = gerritIdent;
|
||||
this.canonicalWebUrl = canonicalWebUrl;
|
||||
this.pluginValidators = pluginValidators;
|
||||
this.repoManager = repoManager;
|
||||
this.allUsers = allUsers;
|
||||
this.allProjects = allProjects;
|
||||
this.externalIdsConsistencyChecker = externalIdsConsistencyChecker;
|
||||
@@ -137,7 +141,7 @@ public class CommitValidators {
|
||||
new BannedCommitsValidator(rejectCommits),
|
||||
new PluginCommitValidationListener(pluginValidators),
|
||||
new ExternalIdUpdateListener(allUsers, externalIdsConsistencyChecker),
|
||||
new AccountCommitValidator(allUsers, accountValidator),
|
||||
new AccountCommitValidator(repoManager, allUsers, accountValidator),
|
||||
new GroupCommitValidator(allUsers)));
|
||||
}
|
||||
|
||||
@@ -163,7 +167,7 @@ public class CommitValidators {
|
||||
new ConfigValidator(branch, user, rw, allUsers, allProjects),
|
||||
new PluginCommitValidationListener(pluginValidators),
|
||||
new ExternalIdUpdateListener(allUsers, externalIdsConsistencyChecker),
|
||||
new AccountCommitValidator(allUsers, accountValidator),
|
||||
new AccountCommitValidator(repoManager, allUsers, accountValidator),
|
||||
new GroupCommitValidator(allUsers)));
|
||||
}
|
||||
|
||||
@@ -700,10 +704,15 @@ public class CommitValidators {
|
||||
}
|
||||
|
||||
public static class AccountCommitValidator implements CommitValidationListener {
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final AllUsersName allUsers;
|
||||
private final AccountValidator accountValidator;
|
||||
|
||||
public AccountCommitValidator(AllUsersName allUsers, AccountValidator accountValidator) {
|
||||
public AccountCommitValidator(
|
||||
GitRepositoryManager repoManager,
|
||||
AllUsersName allUsers,
|
||||
AccountValidator accountValidator) {
|
||||
this.repoManager = repoManager;
|
||||
this.allUsers = allUsers;
|
||||
this.accountValidator = accountValidator;
|
||||
}
|
||||
@@ -726,10 +735,11 @@ public class CommitValidators {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
try (Repository repo = repoManager.openRepository(allUsers)) {
|
||||
List<String> errorMessages =
|
||||
accountValidator.validate(
|
||||
accountId,
|
||||
repo,
|
||||
receiveEvent.revWalk,
|
||||
receiveEvent.command.getOldId(),
|
||||
receiveEvent.commit);
|
||||
|
@@ -285,7 +285,7 @@ public class MergeValidators {
|
||||
}
|
||||
|
||||
try (RevWalk rw = new RevWalk(repo)) {
|
||||
List<String> errorMessages = accountValidator.validate(accountId, rw, null, commit);
|
||||
List<String> errorMessages = accountValidator.validate(accountId, repo, rw, null, commit);
|
||||
if (!errorMessages.isEmpty()) {
|
||||
throw new MergeValidationException(
|
||||
"invalid account configuration: " + Joiner.on("; ").join(errorMessages));
|
||||
|
Reference in New Issue
Block a user