GroupConfig: add a consistency check after loading a group

Change-Id: I5743ac5262ec2e5ff7c6be17df9c4d4237d9b3fe
This commit is contained in:
Changcheng Xiao
2017-11-27 15:45:14 +01:00
parent 59fecfc6e3
commit 02058e1e78
7 changed files with 244 additions and 22 deletions

View File

@@ -17,10 +17,13 @@ package com.google.gerrit.server.group.db;
import static com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.error;
import static com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.warning;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.extensions.api.config.ConsistencyCheckInfo;
import com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.RefNames;
@@ -32,6 +35,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId;
@@ -210,6 +214,56 @@ public class GroupsNoteDbConsistencyChecker {
return problems;
}
public static void ensureConsistentWithGroupNameNotes(
Repository allUsersRepo, InternalGroup group) throws IOException {
List<ConsistencyCheckInfo.ConsistencyProblemInfo> problems =
GroupsNoteDbConsistencyChecker.checkWithGroupNameNotes(
allUsersRepo, group.getName(), group.getGroupUUID());
problems.forEach(GroupsNoteDbConsistencyChecker::logConsistencyProblem);
}
/**
* Check group 'uuid' and 'name' read from 'group.config' with group name notes.
*
* @param allUsersRepo 'All-Users' repository.
* @param groupName the name of the group to be checked.
* @param groupUUID the {@code AccountGroup.UUID} of the group to be checked.
* @return a list of {@code ConsistencyProblemInfo} containing the problem details.
*/
@VisibleForTesting
static List<ConsistencyProblemInfo> checkWithGroupNameNotes(
Repository allUsersRepo, String groupName, AccountGroup.UUID groupUUID) throws IOException {
try {
Optional<GroupReference> groupRef =
GroupNameNotes.loadOneGroupReference(allUsersRepo, groupName);
if (!groupRef.isPresent()) {
return ImmutableList.of(
warning("Group with name '%s' doesn't exist in the list of all names", groupName));
}
AccountGroup.UUID uuid = groupRef.get().getUUID();
String name = groupRef.get().getName();
List<ConsistencyProblemInfo> problems = new ArrayList<>();
if (!Objects.equals(groupUUID, uuid)) {
problems.add(
warning(
"group with name '%s' has UUID '%s' in 'group.config' but '%s' in group name notes",
groupName, groupUUID, uuid));
}
if (!Objects.equals(groupName, name)) {
problems.add(
warning("group note of name '%s' claims to represent name of '%s'", groupName, name));
}
return problems;
} catch (ConfigInvalidException e) {
return ImmutableList.of(
warning("fail to check consistency with group name notes: %s", e.getMessage()));
}
}
public static void logConsistencyProblemAsWarning(String fmt, Object... args) {
logConsistencyProblem(warning(fmt, args));
}