use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.
the class GroupResolver method parseId.
/**
* Parses a group ID and returns the group without making any permission check whether the current
* user can see the group.
*
* @param id ID of the group, can be a group UUID, a group name or a legacy group ID
* @return the group, null if no group is found for the given group ID
*/
public GroupDescription.Basic parseId(String id) {
logger.atFine().log("Parsing group %s", id);
AccountGroup.UUID uuid = AccountGroup.uuid(id);
if (groupBackend.handles(uuid)) {
logger.atFine().log("Group UUID %s is handled by a group backend", uuid.get());
GroupDescription.Basic d = groupBackend.get(uuid);
if (d != null) {
logger.atFine().log("Found group %s", d.getName());
return d;
}
}
// Might be a numeric AccountGroup.Id. -> Internal group.
if (id.matches("^[1-9][0-9]*$")) {
logger.atFine().log("Group ID %s is a numeric ID", id);
try {
AccountGroup.Id groupId = AccountGroup.Id.parse(id);
Optional<InternalGroup> group = groupCache.get(groupId);
if (group.isPresent()) {
logger.atFine().log("Found internal group %s (UUID = %s)", group.get().getName(), group.get().getGroupUUID().get());
return new InternalGroupDescription(group.get());
}
} catch (IllegalArgumentException e) {
// Ignored
logger.atFine().withCause(e).log("Parsing numeric group ID %s failed", id);
}
}
// Might be a group name, be nice and accept unique names.
logger.atFine().log("Try finding a group with name %s", id);
GroupReference ref = GroupBackends.findExactSuggestion(groupBackend, id);
if (ref != null) {
GroupDescription.Basic d = groupBackend.get(ref.getUUID());
if (d != null) {
logger.atFine().log("Found group %s", d.getName());
return d;
}
}
logger.atFine().log("Group %s not found", id);
return null;
}
use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.
the class AllProjectsCreatorTest method createDefaultAllProjectsConfig.
@Test
public void createDefaultAllProjectsConfig() throws Exception {
// Loads the expected configs.
Config expectedConfig = new Config();
expectedConfig.fromText(getDefaultAllProjectsWithAllDefaultSections());
GroupReference adminsGroup = createGroupReference("Administrators");
GroupReference batchUsersGroup = createGroupReference(ServiceUserClassifier.SERVICE_USERS);
AllProjectsInput allProjectsInput = AllProjectsInput.builder().administratorsGroup(adminsGroup).serviceUsersGroup(batchUsersGroup).build();
allProjectsCreator.create(allProjectsInput);
Config config = readAllProjectsConfig(repoManager, allProjectsName);
assertTwoConfigsEquivalent(config, expectedConfig);
}
use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.
the class GroupNameNotes method loadAllGroups.
/**
* Loads the {@code GroupReference}s (name/UUID pairs) for all groups.
*
* <p>Even though group UUIDs should be unique, this class doesn't enforce it. For this reason,
* it's technically possible that two of the {@code GroupReference}s have a duplicate UUID but a
* different name. In practice, this shouldn't occur unless we introduce a bug in the future.
*
* @param repository the repository which holds the commits of the notes
* @return the {@code GroupReference}s of all existing groups/notes
* @throws IOException if the repository can't be accessed for some reason
* @throws ConfigInvalidException if one of the notes is in an invalid state
*/
public static ImmutableList<GroupReference> loadAllGroups(Repository repository) throws IOException, ConfigInvalidException {
Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
if (ref == null) {
return ImmutableList.of();
}
try (TraceTimer ignored = TraceContext.newTimer("Loading all groups", Metadata.builder().noteDbRefName(RefNames.REFS_GROUPNAMES).build());
RevWalk revWalk = new RevWalk(repository);
ObjectReader reader = revWalk.getObjectReader()) {
RevCommit notesCommit = revWalk.parseCommit(ref.getObjectId());
NoteMap noteMap = NoteMap.read(reader, notesCommit);
Multiset<GroupReference> groupReferences = HashMultiset.create();
for (Note note : noteMap) {
GroupReference groupReference = getGroupReference(reader, note.getData());
int numOfOccurrences = groupReferences.add(groupReference, 1);
if (numOfOccurrences > 1) {
GroupsNoteDbConsistencyChecker.logConsistencyProblemAsWarning("The UUID of group %s (%s) is duplicate in group name notes", groupReference.getName(), groupReference.getUUID());
}
}
return ImmutableList.copyOf(groupReferences);
}
}
use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.
the class RenameGroupOp method run.
@Override
public void run() {
Iterable<Project.NameKey> names = tryingAgain ? retryOn : projectCache.all();
for (Project.NameKey projectName : names) {
CachedProjectConfig config = projectCache.get(projectName).orElseThrow(illegalState(projectName)).getConfig();
Optional<GroupReference> ref = config.getGroup(uuid);
if (!ref.isPresent() || newName.equals(ref.get().getName())) {
continue;
}
try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
rename(md);
} catch (RepositoryNotFoundException noProject) {
continue;
} catch (ConfigInvalidException | IOException err) {
logger.atSevere().withCause(err).log("Cannot rename group %s in %s", oldName, projectName);
}
}
// another attempt. If it doesn't update after that, give up.
if (!retryOn.isEmpty() && !tryingAgain) {
tryingAgain = true;
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = start(5, TimeUnit.MINUTES);
}
}
use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.
the class ProjectConfig method saveNotifySections.
private void saveNotifySections(Config rc, Set<AccountGroup.UUID> keepGroups) {
unsetSection(rc, NOTIFY);
for (NotifyConfig nc : sort(notifySections.values())) {
nc.getGroups().stream().map(GroupReference::getUUID).filter(Objects::nonNull).forEach(keepGroups::add);
List<String> email = nc.getGroups().stream().map(gr -> PermissionRule.create(gr).asString(false)).sorted().collect(toList());
// Separate stream operation so that emails list contains 2 sorted sub-lists.
nc.getAddresses().stream().map(Address::toString).sorted().forEach(email::add);
set(rc, NOTIFY, nc.getName(), KEY_HEADER, nc.getHeader(), NotifyConfig.Header.BCC);
if (email.isEmpty()) {
rc.unset(NOTIFY, nc.getName(), KEY_EMAIL);
} else {
rc.setStringList(NOTIFY, nc.getName(), KEY_EMAIL, email);
}
if (nc.getNotify().equals(Sets.immutableEnumSet(NotifyType.ALL))) {
rc.unset(NOTIFY, nc.getName(), KEY_TYPE);
} else {
List<String> types = new ArrayList<>(4);
for (NotifyType t : NotifyType.values()) {
if (nc.isNotify(t)) {
types.add(t.name().toLowerCase(Locale.US));
}
}
rc.setStringList(NOTIFY, nc.getName(), KEY_TYPE, types);
}
set(rc, NOTIFY, nc.getName(), KEY_FILTER, nc.getFilter());
}
}
Aggregations