use of com.google.gerrit.entities.InternalGroup in project gerrit by GerritCodeReview.
the class GroupIndexerImpl method index.
@Override
public void index(AccountGroup.UUID uuid) {
// Evict the cache to get an up-to-date value for sure.
groupCache.evict(uuid);
Optional<InternalGroup> internalGroup = groupCache.get(uuid);
if (internalGroup.isPresent()) {
logger.atFine().log("Replace group %s in index", uuid.get());
} else {
logger.atFine().log("Delete group %s from index", uuid.get());
}
for (Index<AccountGroup.UUID, InternalGroup> i : getWriteIndexes()) {
if (internalGroup.isPresent()) {
try (TraceTimer traceTimer = TraceContext.newTimer("Replacing group", Metadata.builder().groupUuid(uuid.get()).indexVersion(i.getSchema().getVersion()).build())) {
i.replace(internalGroup.get());
} catch (RuntimeException e) {
throw new StorageException(String.format("Failed to replace group %s in index version %d", uuid.get(), i.getSchema().getVersion()), e);
}
} else {
try (TraceTimer traceTimer = TraceContext.newTimer("Deleting group", Metadata.builder().groupUuid(uuid.get()).indexVersion(i.getSchema().getVersion()).build())) {
i.delete(uuid);
} catch (RuntimeException e) {
throw new StorageException(String.format("Failed to delete group %s from index version %d", uuid.get(), i.getSchema().getVersion()), e);
}
}
}
fireGroupIndexedEvent(uuid.get());
}
use of com.google.gerrit.entities.InternalGroup in project gerrit by GerritCodeReview.
the class AllGroupsIndexer method reindexGroups.
private SiteIndexer.Result reindexGroups(GroupIndex index, List<AccountGroup.UUID> uuids, ProgressMonitor progress) {
progress.beginTask("Reindexing groups", uuids.size());
List<ListenableFuture<?>> futures = new ArrayList<>(uuids.size());
AtomicBoolean ok = new AtomicBoolean(true);
AtomicInteger done = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
Stopwatch sw = Stopwatch.createStarted();
groupCache.evict(uuids);
Map<AccountGroup.UUID, InternalGroup> reindexedGroups = groupCache.get(uuids);
for (AccountGroup.UUID uuid : uuids) {
String desc = "group " + uuid;
ListenableFuture<?> future = executor.submit(() -> {
try {
groupCache.evict(uuid);
InternalGroup internalGroup = reindexedGroups.get(uuid);
if (internalGroup != null) {
if (isFirstInsertForEntry.equals(IsFirstInsertForEntry.YES)) {
index.insert(internalGroup);
} else {
index.replace(internalGroup);
}
} else {
index.delete(uuid);
// The UUID here is read from group name notes. If it fails to load from group
// cache, there exists an inconsistency.
GroupsNoteDbConsistencyChecker.logFailToLoadFromGroupRefAsWarning(uuid);
}
verboseWriter.println("Reindexed " + desc);
done.incrementAndGet();
} catch (Exception e) {
failed.incrementAndGet();
throw e;
}
return null;
});
addErrorListener(future, desc, progress, ok);
futures.add(future);
}
try {
Futures.successfulAsList(futures).get();
} catch (ExecutionException | InterruptedException e) {
logger.atSevere().withCause(e).log("Error waiting on group futures");
return SiteIndexer.Result.create(sw, false, 0, 0);
}
progress.endTask();
return SiteIndexer.Result.create(sw, ok.get(), done.get(), failed.get());
}
use of com.google.gerrit.entities.InternalGroup in project gerrit by GerritCodeReview.
the class CreateGroup method createGroup.
private InternalGroup createGroup(CreateGroupArgs createGroupArgs) throws ResourceConflictException, IOException, ConfigInvalidException {
String nameLower = createGroupArgs.getGroupName().toLowerCase(Locale.US);
for (String name : systemGroupBackend.getNames()) {
if (name.toLowerCase(Locale.US).equals(nameLower)) {
throw new ResourceConflictException("group '" + name + "' already exists");
}
}
for (String name : systemGroupBackend.getReservedNames()) {
if (name.toLowerCase(Locale.US).equals(nameLower)) {
throw new ResourceConflictException("group name '" + name + "' is reserved");
}
}
AccountGroup.Id groupId = AccountGroup.id(sequences.nextGroupId());
AccountGroup.UUID uuid = MoreObjects.firstNonNull(createGroupArgs.uuid, GroupUuid.make(createGroupArgs.getGroupName(), self.get().newCommitterIdent(TimeUtil.now(), serverTimeZone)));
InternalGroupCreation groupCreation = InternalGroupCreation.builder().setGroupUUID(uuid).setNameKey(createGroupArgs.getGroup()).setId(groupId).build();
GroupDelta.Builder groupDeltaBuilder = GroupDelta.builder().setVisibleToAll(createGroupArgs.visibleToAll);
if (createGroupArgs.ownerGroupUuid != null) {
Optional<InternalGroup> ownerGroup = groupCache.get(createGroupArgs.ownerGroupUuid);
ownerGroup.map(InternalGroup::getGroupUUID).ifPresent(groupDeltaBuilder::setOwnerGroupUUID);
}
if (createGroupArgs.groupDescription != null) {
groupDeltaBuilder.setDescription(createGroupArgs.groupDescription);
}
groupDeltaBuilder.setMemberModification(members -> ImmutableSet.copyOf(createGroupArgs.initialMembers));
try {
return groupsUpdateProvider.get().createGroup(groupCreation, groupDeltaBuilder.build());
} catch (DuplicateKeyException e) {
throw new ResourceConflictException("group '" + createGroupArgs.getGroupName() + "' already exists", e);
}
}
use of com.google.gerrit.entities.InternalGroup in project gerrit by GerritCodeReview.
the class GetAuditLog method apply.
@Override
public Response<List<? extends GroupAuditEventInfo>> apply(GroupResource rsrc) throws AuthException, NotInternalGroupException, IOException, ConfigInvalidException, PermissionBackendException {
GroupDescription.Internal group = rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new);
if (!rsrc.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
AccountLoader accountLoader = accountLoaderFactory.create(true);
List<GroupAuditEventInfo> auditEvents = new ArrayList<>();
try (Repository allUsersRepo = repoManager.openRepository(allUsers)) {
for (AccountGroupMemberAudit auditEvent : groups.getMembersAudit(allUsersRepo, group.getGroupUUID())) {
AccountInfo member = accountLoader.get(auditEvent.memberId());
auditEvents.add(GroupAuditEventInfo.createAddUserEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
if (!auditEvent.isActive()) {
auditEvents.add(GroupAuditEventInfo.createRemoveUserEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
}
}
List<AccountGroupByIdAudit> subGroupsAudit = groups.getSubgroupsAudit(allUsersRepo, group.getGroupUUID());
Map<AccountGroup.UUID, InternalGroup> groups = groupCache.get(subGroupsAudit.stream().map(a -> a.includeUuid()).collect(toImmutableList()));
for (AccountGroupByIdAudit auditEvent : subGroupsAudit) {
AccountGroup.UUID includedGroupUUID = auditEvent.includeUuid();
InternalGroup includedGroup = groups.get(includedGroupUUID);
GroupInfo member;
if (includedGroup != null) {
member = groupJson.format(new InternalGroupDescription(includedGroup));
} else {
member = new GroupInfo();
member.id = Url.encode(includedGroupUUID.get());
GroupDescription.Basic groupDescription = groupBackend.get(includedGroupUUID);
if (groupDescription != null) {
member.name = groupDescription.getName();
}
}
auditEvents.add(GroupAuditEventInfo.createAddGroupEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
if (!auditEvent.isActive()) {
auditEvents.add(GroupAuditEventInfo.createRemoveGroupEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
}
}
}
accountLoader.fill();
// sort by date and then reverse so that the newest audit event comes first
auditEvents.sort(comparing((GroupAuditEventInfo a) -> a.date).reversed());
return Response.ok(auditEvents);
}
use of com.google.gerrit.entities.InternalGroup in project gerrit by GerritCodeReview.
the class AgreementsIT method configureContributorAgreement.
protected ContributorAgreement configureContributorAgreement(boolean autoVerify) throws Exception {
ContributorAgreement.Builder ca;
String name = autoVerify ? "cla-test-group" : "cla-test-no-auto-verify-group";
AccountGroup.UUID g = groupOperations.newGroup().name(name).create();
GroupApi groupApi = gApi.groups().id(g.get());
groupApi.description("CLA test group");
InternalGroup caGroup = group(AccountGroup.uuid(groupApi.detail().id));
GroupReference groupRef = GroupReference.create(caGroup.getGroupUUID(), caGroup.getName());
PermissionRule rule = PermissionRule.builder(groupRef).setAction(PermissionRule.Action.ALLOW).build();
if (autoVerify) {
ca = ContributorAgreement.builder("cla-test");
ca.setAutoVerify(groupRef);
ca.setAccepted(ImmutableList.of(rule));
} else {
ca = ContributorAgreement.builder("cla-test-no-auto-verify");
}
ca.setDescription("description");
ca.setAgreementUrl("agreement-url");
ca.setAccepted(ImmutableList.of(rule));
ca.setExcludeProjectsRegexes(ImmutableList.of("ExcludedProject"));
try (ProjectConfigUpdate u = updateProject(allProjects)) {
ContributorAgreement contributorAgreement = ca.build();
u.getConfig().replace(contributorAgreement);
u.save();
return contributorAgreement;
}
}
Aggregations