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)); }
public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) { Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' of pull request '%s' not found", key, pullRequest)); }
/** * Components of the main branch won't be found */ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) { Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch)); }
private void assertExistButDisabled(String... keys) { for (String key : keys) { ComponentDto dto = dbClient.componentDao().selectByKey(db.getSession(), key).get(); assertThat(dto.isEnabled()).isFalse(); } }
private static List<Issues.Component> formatComponents(SearchResponseData data) { Collection<ComponentDto> components = data.getComponents(); List<Issues.Component> result = new ArrayList<>(); for (ComponentDto dto : components) { String uuid = dto.uuid(); Component.Builder builder = Component.newBuilder() .setOrganization(data.getOrganizationKey(dto.getOrganizationUuid())) .setKey(dto.getKey()) .setUuid(uuid) .setQualifier(dto.qualifier()) .setName(nullToEmpty(dto.name())) .setLongName(nullToEmpty(dto.longName())) .setEnabled(dto.isEnabled()); ofNullable(dto.getBranch()).ifPresent(builder::setBranch); ofNullable(dto.getPullRequest()).ifPresent(builder::setPullRequest); ofNullable(emptyToNull(dto.path())).ifPresent(builder::setPath); result.add(builder.build()); } return result; }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key"); List<BranchDto> pullRequests = dbClient.branchDao().selectByComponent(dbSession, project).stream() .filter(b -> b.getBranchType() == PULL_REQUEST) .collect(toList()); List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList()); Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao() .selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) .stream().collect(uniqueIndex(BranchDto::getUuid)); Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), pullRequestUuids).stream() .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity())); Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream() .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream() .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder(); pullRequests .forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid()))); writeProtobuf(protobufResponse.build(), request, response); } }
private static Optional<ComponentUpdateDto> compareForUpdate(ComponentDto existing, ComponentDto target) { boolean hasDifferences = !StringUtils.equals(existing.getCopyResourceUuid(), target.getCopyResourceUuid()) || !StringUtils.equals(existing.description(), target.description()) || !StringUtils.equals(existing.getDbKey(), target.getDbKey()) || !existing.isEnabled() || !StringUtils.equals(existing.getUuidPath(), target.getUuidPath()) || !StringUtils.equals(existing.language(), target.language()) || !StringUtils.equals(existing.longName(), target.longName()) || !StringUtils.equals(existing.moduleUuid(), target.moduleUuid()) || !StringUtils.equals(existing.moduleUuidPath(), target.moduleUuidPath()) || !StringUtils.equals(existing.name(), target.name()) || !StringUtils.equals(existing.path(), target.path()) || !StringUtils.equals(existing.scope(), target.scope()) || !StringUtils.equals(existing.qualifier(), target.qualifier()); ComponentUpdateDto update = null; if (hasDifferences) { update = ComponentUpdateDto .copyFrom(target) .setBChanged(true); } return ofNullable(update); }
@Test public void selectByUuid_on_disabled_component() { ComponentDto enabledProject = db.components().insertPublicProject(p -> p.setEnabled(true)); ComponentDto disabledProject = db.components().insertPublicProject(p -> p.setEnabled(false)); ComponentDto result = underTest.selectByUuid(dbSession, disabledProject.uuid()).get(); assertThat(result).isNotNull(); assertThat(result.isEnabled()).isFalse(); }
@Test public void update_key_of_disabled_module() { ComponentDto project = insertProject(); ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project).setEnabled(false)); userSessionRule.addProjectPermission(UserRole.ADMIN, project); callByKey(module.getKey(), ANOTHER_KEY); assertThat(selectByKey(project.getKey()).isPresent()).isTrue(); assertThat(selectByKey(module.getKey()).isPresent()).isFalse(); ComponentDto loadedModule = selectByKey(ANOTHER_KEY).get(); assertThat(loadedModule.uuid()).isEqualTo(module.uuid()); assertThat(loadedModule.isEnabled()).isFalse(); }
@Test public void get_by_key_on_disabled_component() { ComponentDto project = db.components().insertPrivateProject(p -> p.setEnabled(false)); ComponentDto result = underTest.selectOrFailByKey(dbSession, project.getDbKey()); assertThat(result.isEnabled()).isFalse(); }
@Test public void get_by_id_on_disabled_component() { ComponentDto enabledProject = db.components().insertPrivateProject(); ComponentDto disabledProject = db.components().insertPrivateProject(p -> p.setEnabled(false)); Optional<ComponentDto> result = underTest.selectById(dbSession, disabledProject.getId()); assertThat(result).isPresent(); assertThat(result.get().isEnabled()).isFalse(); }
assertThat(projectReloaded.projectUuid()).isEqualTo(project.projectUuid()); assertThat(projectReloaded.getRootUuid()).isEqualTo(project.getRootUuid()); assertThat(projectReloaded.isEnabled()).isTrue(); assertThat(directoryReloaded.longName()).isEqualTo(removedDirectory.longName()); assertThat(directoryReloaded.path()).isEqualTo(removedDirectory.path()); assertThat(directoryReloaded.isEnabled()).isTrue(); assertThat(fileReloaded.name()).isEqualTo(removedFile.name()); assertThat(fileReloaded.path()).isEqualTo(removedFile.path()); assertThat(fileReloaded.isEnabled()).isTrue();
/** * Copy the A-fields to B-fields. The field bChanged is kept to false. */ public static ComponentUpdateDto copyFrom(ComponentDto from) { return new ComponentUpdateDto() .setUuid(from.uuid()) .setBChanged(false) .setBKey(from.getDbKey()) .setBCopyComponentUuid(from.getCopyResourceUuid()) .setBDescription(from.description()) .setBEnabled(from.isEnabled()) .setBUuidPath(from.getUuidPath()) .setBLanguage(from.language()) .setBLongName(from.longName()) .setBModuleUuid(from.moduleUuid()) .setBModuleUuidPath(from.moduleUuidPath()) .setBName(from.name()) .setBPath(from.path()) // We don't have a b_scope. The applyBChangesForRootComponentUuid query is using a case ... when to infer scope from the qualifier .setBQualifier(from.qualifier()); } }
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)); }
/** * Components of the main branch won't be found */ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) { java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch)); }
public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) { java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' of pull request '%s' not found", key, pullRequest)); }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key"); List<BranchDto> pullRequests = dbClient.branchDao().selectByComponent(dbSession, project).stream() .filter(b -> b.getBranchType() == PULL_REQUEST) .collect(toList()); List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList()); Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao() .selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) .stream().collect(uniqueIndex(BranchDto::getUuid)); Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), pullRequestUuids).stream() .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity())); Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream() .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream() .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder(); pullRequests .forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid()))); writeProtobuf(protobufResponse.build(), request, response); } }
private static Optional<ComponentUpdateDto> compareForUpdate(ComponentDto existing, ComponentDto target) { boolean hasDifferences = !StringUtils.equals(existing.getCopyResourceUuid(), target.getCopyResourceUuid()) || !StringUtils.equals(existing.description(), target.description()) || !existing.isEnabled() || !StringUtils.equals(existing.getUuidPath(), target.getUuidPath()) || !StringUtils.equals(existing.language(), target.language()) || !StringUtils.equals(existing.longName(), target.longName()) || !StringUtils.equals(existing.moduleUuid(), target.moduleUuid()) || !StringUtils.equals(existing.moduleUuidPath(), target.moduleUuidPath()) || !StringUtils.equals(existing.name(), target.name()) || !StringUtils.equals(existing.path(), target.path()) || !StringUtils.equals(existing.qualifier(), target.qualifier()); ComponentUpdateDto update = null; if (hasDifferences) { update = ComponentUpdateDto .copyFrom(target) .setBChanged(true); } return ofNullable(update); }
private static List<Issues.Component> formatComponents(SearchResponseData data) { Collection<ComponentDto> components = data.getComponents(); List<Issues.Component> result = new ArrayList<>(); for (ComponentDto dto : components) { String uuid = dto.uuid(); Component.Builder builder = Component.newBuilder() .setOrganization(data.getOrganizationKey(dto.getOrganizationUuid())) .setKey(dto.getKey()) .setUuid(uuid) .setQualifier(dto.qualifier()) .setName(nullToEmpty(dto.name())) .setLongName(nullToEmpty(dto.longName())) .setEnabled(dto.isEnabled()); setNullable(dto.getBranch(), builder::setBranch); setNullable(dto.getPullRequest(), builder::setPullRequest); setNullable(emptyToNull(dto.path()), builder::setPath); result.add(builder.build()); } return result; }
/** * Copy the A-fields to B-fields. The field bChanged is kept to false. */ public static ComponentUpdateDto copyFrom(ComponentDto from) { return new ComponentUpdateDto() .setUuid(from.uuid()) .setBChanged(false) .setBCopyComponentUuid(from.getCopyResourceUuid()) .setBDescription(from.description()) .setBEnabled(from.isEnabled()) .setBUuidPath(from.getUuidPath()) .setBLanguage(from.language()) .setBLongName(from.longName()) .setBModuleUuid(from.moduleUuid()) .setBModuleUuidPath(from.moduleUuidPath()) .setBName(from.name()) .setBPath(from.path()) .setBQualifier(from.qualifier()); } }