Search in sources :

Example 26 with GroupReference

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;
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupReference(com.google.gerrit.entities.GroupReference) InternalGroup(com.google.gerrit.entities.InternalGroup)

Example 27 with GroupReference

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);
}
Also used : Config(org.eclipse.jgit.lib.Config) AllProjectsCreatorTestUtil.readAllProjectsConfig(com.google.gerrit.server.schema.testing.AllProjectsCreatorTestUtil.readAllProjectsConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) GroupReference(com.google.gerrit.entities.GroupReference) Test(org.junit.Test)

Example 28 with GroupReference

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);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) Note(org.eclipse.jgit.notes.Note) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) GroupReference(com.google.gerrit.entities.GroupReference) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 29 with GroupReference

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);
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) Project(com.google.gerrit.entities.Project) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 30 with GroupReference

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());
    }
}
Also used : NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) Address(com.google.gerrit.entities.Address) ArrayList(java.util.ArrayList) NotifyConfig(com.google.gerrit.entities.NotifyConfig) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

GroupReference (com.google.gerrit.entities.GroupReference)59 Test (org.junit.Test)24 AccountGroup (com.google.gerrit.entities.AccountGroup)18 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 GroupDescription (com.google.gerrit.entities.GroupDescription)8 InternalGroup (com.google.gerrit.entities.InternalGroup)7 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)7 IOException (java.io.IOException)7 Repository (org.eclipse.jgit.lib.Repository)7 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)6 Config (org.eclipse.jgit.lib.Config)6 CachedProjectConfig (com.google.gerrit.entities.CachedProjectConfig)5 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)5 NotifyConfig (com.google.gerrit.entities.NotifyConfig)4 ArrayList (java.util.ArrayList)4 Account (com.google.gerrit.entities.Account)3 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)3 Permission (com.google.gerrit.entities.Permission)3 PermissionRule (com.google.gerrit.entities.PermissionRule)3 ProjectAccessInfo (com.google.gerrit.extensions.api.access.ProjectAccessInfo)3