private static boolean isMainProject(ComponentDto projectOrModule) { return projectOrModule.isRootProject() && projectOrModule.getMainBranchProjectUuid() == null; }
private static String toProjectUuid(ComponentDto componentDto) { String mainBranchProjectUuid = componentDto.getMainBranchProjectUuid(); return mainBranchProjectUuid == null ? componentDto.projectUuid() : mainBranchProjectUuid; }
private static ComponentDto checkComponent(Optional<ComponentDto> componentDto, String message, Object... messageArguments) { if (componentDto.isPresent() && componentDto.get().isEnabled() && componentDto.get().getMainBranchProjectUuid() == null) { return componentDto.get(); } throw new NotFoundException(format(message, messageArguments)); }
/** * Helper methods which sets both {@link #componentUuid} and {@link #mainComponentUuid} from the specified * {@link ComponentDto}. */ public CeQueueDto setComponent(@Nullable ComponentDto component) { if (component == null) { this.componentUuid = null; this.mainComponentUuid = null; } else { this.componentUuid = requireNonNull(component.uuid()); this.mainComponentUuid = firstNonNull(component.getMainBranchProjectUuid(), component.uuid()); } return this; }
private static boolean isMainProject(ComponentDto p) { return Scopes.PROJECT.equals(p.scope()) && Qualifiers.PROJECT.equals(p.qualifier()) && p.getMainBranchProjectUuid() == null; }
public static Component fromDto(ComponentDto dto) { String uuid = dto.uuid(); return new Component(uuid, firstNonNull(dto.getMainBranchProjectUuid(), uuid)); }
@Override protected List<ComponentDto> doKeepAuthorizedComponents(String permission, Collection<ComponentDto> components) { try (DbSession dbSession = dbClient.openSession(false)) { Set<String> projectUuids = components.stream() .map(c -> defaultIfEmpty(c.getMainBranchProjectUuid(), c.projectUuid())) .collect(MoreCollectors.toSet(components.size())); Set<String> authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedProjectUuids(dbSession, projectUuids, getUserId(), permission); return components.stream() .filter(c -> authorizedProjectUuids.contains(c.projectUuid()) || authorizedProjectUuids.contains(c.getMainBranchProjectUuid())) .collect(MoreCollectors.toList(components.size())); } }
private List<Setting> loadSettings(DbSession dbSession, Optional<ComponentDto> component, Set<String> keys) { // List of settings must be kept in the following orders : default -> global -> component -> branch List<Setting> settings = new ArrayList<>(); settings.addAll(loadDefaultValues(keys)); settings.addAll(loadGlobalSettings(dbSession, keys)); if (component.isPresent() && component.get().getBranch() != null && component.get().getMainBranchProjectUuid() != null) { ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, component.get().getMainBranchProjectUuid()); settings.addAll(loadComponentSettings(dbSession, keys, project).values()); } component.ifPresent(componentDto -> settings.addAll(loadComponentSettings(dbSession, keys, componentDto).values())); return settings.stream() .filter(s -> settingsWsSupport.isVisible(s.getKey(), s.getDefinition(), component)) .collect(Collectors.toList()); }
@Override public final boolean hasComponentPermission(String permission, ComponentDto component) { if (isRoot()) { return true; } String projectUuid = defaultString(component.getMainBranchProjectUuid(), component.projectUuid()); return hasProjectUuidPermission(permission, projectUuid); }
private Stream<WebhookDto> readWebHooksFrom(String projectUuid) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<ComponentDto> optionalComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, projectUuid).orElse(null)); ComponentDto componentDto = checkStateWithOptional(optionalComponentDto, "the requested project '%s' was not found", projectUuid); if (componentDto.getMainBranchProjectUuid() != null && !componentDto.uuid().equals(componentDto.getMainBranchProjectUuid())) { Optional<ComponentDto> mainBranchComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, componentDto.getMainBranchProjectUuid()).orElse(null)); componentDto = checkStateWithOptional(mainBranchComponentDto, "the requested project '%s' was not found", projectUuid); } WebhookDao dao = dbClient.webhookDao(); return Stream.concat( dao.selectByProject(dbSession, componentDto).stream(), dao.selectByOrganizationUuid(dbSession, componentDto.getOrganizationUuid()).stream()); } }
public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) { String projectUuid = component.getMainBranchProjectUuid(); if (projectUuid == null) { projectUuid = component.projectUuid(); } return mapper(dbSession).selectByProjectUuid(projectUuid); }
public void delete(DbSession dbSession, ComponentDto project) { checkArgument(!hasNotProjectScope(project) && !isNotDeletable(project) && project.getMainBranchProjectUuid() == null, "Only projects can be deleted"); dbClient.purgeDao().deleteProject(dbSession, project.uuid()); dbClient.userDao().cleanHomepage(dbSession, project); projectIndexers.commitAndIndex(dbSession, singletonList(project), PROJECT_DELETION); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { List<ComponentDto> projects = getAuthorizedComponents(searchComponents(dbSession, request)) .stream() .filter(p -> p.getMainBranchProjectUuid() == null) .collect(MoreCollectors.toList()); JsonWriter json = response.newJsonWriter(); json.beginArray(); for (ComponentDto project : projects) { addProject(json, project); } json.endArray(); json.close(); } }
@SafeVarargs public final ComponentDto insertProjectBranch(ComponentDto project, Consumer<BranchDto>... dtoPopulators) { // MainBranchProjectUuid will be null if it's a main branch BranchDto branchDto = newBranchDto(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid()), LONG); Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(branchDto)); ComponentDto branch = newProjectBranch(project, branchDto); insertComponent(branch); dbClient.branchDao().insert(dbSession, branchDto); db.commit(); return branch; }
public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) { boolean isMain = branchComponent.getMainBranchProjectUuid() == null; String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid(); String key = isMain ? "master" : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(branchComponent.uuid()) .setProjectUuid(projectUuid) .setBranchType(branchType); }
public final ComponentDto insertProjectBranch(ComponentDto project, BranchDto branchDto) { // MainBranchProjectUuid will be null if it's a main branch checkArgument(branchDto.getProjectUuid().equals(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid()))); ComponentDto branch = newProjectBranch(project, branchDto); insertComponent(branch); dbClient.branchDao().insert(dbSession, branchDto); db.commit(); return branch; }
/** * Grant permission on given project */ public UserPermissionDto insertProjectPermissionOnUser(UserDto user, String permission, ComponentDto project) { checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); UserPermissionDto dto = new UserPermissionDto(project.getOrganizationUuid(), permission, user.getId(), project.getId()); db.getDbClient().userPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; }
public GroupPermissionDto insertProjectPermissionOnAnyone(String permission, ComponentDto project) { checkArgument(!project.isPrivate(), "No permission to group AnyOne can be granted on a private project"); checkArgument(!PUBLIC_PERMISSIONS.contains(permission), "permission %s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); GroupPermissionDto dto = new GroupPermissionDto() .setOrganizationUuid(project.getOrganizationUuid()) .setGroupId(null) .setRole(permission) .setResourceId(project.getId()); db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; }
public GroupPermissionDto insertProjectPermissionOnGroup(GroupDto group, String permission, ComponentDto project) { checkArgument(group.getOrganizationUuid().equals(project.getOrganizationUuid()), "Different organizations"); checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); GroupPermissionDto dto = new GroupPermissionDto() .setOrganizationUuid(group.getOrganizationUuid()) .setGroupId(group.getId()) .setRole(permission) .setResourceId(project.getId()); db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; }
public static IssueDoc newDoc(String key, ComponentDto componentDto) { String mainBranchProjectUuid = componentDto.getMainBranchProjectUuid(); return newDoc() .setKey(key) .setBranchUuid(componentDto.projectUuid()) .setComponentUuid(componentDto.uuid()) .setModuleUuid(!componentDto.scope().equals(Scopes.PROJECT) ? componentDto.moduleUuid() : componentDto.uuid()) .setModuleUuidPath(componentDto.moduleUuidPath()) .setProjectUuid(mainBranchProjectUuid == null ? componentDto.projectUuid() : mainBranchProjectUuid) .setOrganizationUuid(componentDto.getOrganizationUuid()) // File path make no sens on modules and projects .setFilePath(!componentDto.scope().equals(Scopes.PROJECT) ? componentDto.path() : null) .setIsMainBranch(mainBranchProjectUuid == null); }