@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 void loadProjects(Collector collector, DbSession dbSession, SearchResponseData result) { Collection<ComponentDto> loadedComponents = result.getComponents(); for (ComponentDto component : loadedComponents) { collector.addProjectUuid(component.projectUuid()); } Set<String> loadedProjectUuids = loadedComponents.stream().filter(cpt -> cpt.uuid().equals(cpt.projectUuid())).map(ComponentDto::uuid).collect(MoreCollectors.toSet()); Set<String> projectUuidsToLoad = copyOf(difference(collector.getProjectUuids(), loadedProjectUuids)); if (!projectUuidsToLoad.isEmpty()) { List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, collector.getProjectUuids()); result.addComponents(projects); } }
private static void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) { builder.branchUuid(branch == null && pullRequest == null ? null : component.projectUuid()); builder.mainBranch(UNKNOWN_COMPONENT.equals(component) || (branch == null && pullRequest == null) || (branch != null && !branch.equals(component.getBranch())) || (pullRequest != null && !pullRequest.equals(component.getPullRequest()))); } }
private void writeProfiles(JsonWriter json, DbSession dbSession, ComponentDto component) { Set<QualityProfile> qualityProfiles = dbClient.liveMeasureDao().selectMeasure(dbSession, component.projectUuid(), QUALITY_PROFILES_KEY) .map(LiveMeasureDto::getDataAsString) .map(data -> QPMeasureData.fromJson(data).getProfiles()) .orElse(emptySortedSet()); Map<String, QProfileDto> dtoByQPKey = dbClient.qualityProfileDao().selectByUuids(dbSession, qualityProfiles.stream().map(QualityProfile::getQpKey).collect(Collectors.toList())) .stream() .collect(uniqueIndex(QProfileDto::getKee)); json.name("qualityProfiles").beginArray(); qualityProfiles.forEach(qp -> writeToJson(json, qp, !dtoByQPKey.containsKey(qp.getQpKey()))); json.endArray(); }
public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) { String projectUuid = component.getMainBranchProjectUuid(); if (projectUuid == null) { projectUuid = component.projectUuid(); } return mapper(dbSession).selectByProjectUuid(projectUuid); }
private ComponentDto getRootProjectOrBranch(ComponentDto component, DbSession session) { if (!component.isRootProject()) { return dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid()); } else { return component; } }
public T addProjectPermission(String permission, ComponentDto... components) { Arrays.stream(components).forEach(component -> { checkArgument( component.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "public component %s can't be granted public permission %s", component.uuid(), permission); }); registerComponents(components); this.projectPermissions.add(permission); Arrays.stream(components) .forEach(component -> this.projectUuidByPermission.put(permission, component.projectUuid())); return clazz.cast(this); }
private static SearchWsResponse buildResponse(List<ComponentDto> components, OrganizationDto organization, Map<String, String> projectKeysByUuids, Paging paging) { SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder(); responseBuilder.getPagingBuilder() .setPageIndex(paging.pageIndex()) .setPageSize(paging.pageSize()) .setTotal(paging.total()) .build(); components.stream() .map(dto -> dtoToComponent(organization, dto, projectKeysByUuids.get(dto.projectUuid()))) .forEach(responseBuilder::addComponents); return responseBuilder.build(); }
private static ComponentDto createButDoNotInsertBranch(ComponentDto project) { BranchType randomBranchType = BranchType.values()[new Random().nextInt(BranchType.values().length)]; BranchDto branchDto = newBranchDto(project.projectUuid(), randomBranchType); return ComponentTesting.newProjectBranch(project, branchDto); }
@Test public void get_by_key_on_a_root_project() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto result = underTest.selectOrFailByKey(dbSession, project.getDbKey()); assertThat(result.getDbKey()).isEqualTo(project.getDbKey()); assertThat(result.uuid()).isEqualTo(project.uuid()); assertThat(result.getUuidPath()).isEqualTo(project.getUuidPath()); assertThat(result.getRootUuid()).isEqualTo(project.uuid()); assertThat(result.projectUuid()).isEqualTo(project.uuid()); }
private LiveMeasureDto assertThatIntMeasureHasValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedValue); return measure; }
@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; }
private void assertThatIntMeasureHasLeakValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isNull(); assertThat(measure.getVariation()).isEqualTo(expectedValue); }
private void assertThatRatingMeasureHasLeakValue(ComponentDto component, Rating expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), ratingMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getVariation()).isEqualTo((double) expectedValue.getIndex()); }
public static SnapshotDto newAnalysis(ComponentDto rootComponent) { checkNotNull(rootComponent.uuid(), "Project UUID must be set"); checkArgument(rootComponent.uuid().equals(rootComponent.projectUuid()), "Component is not a tree root"); return new SnapshotDto() .setUuid(randomAlphanumeric(40)) .setComponentUuid(rootComponent.uuid()) .setStatus(SnapshotDto.STATUS_PROCESSED) .setCreatedAt(System.currentTimeMillis()) .setBuildDate(System.currentTimeMillis()) .setLast(true); }
private ShowWsResponse doHandle(Request request) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.USER, component); Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.projectUuid()); List<ComponentDto> ancestors = dbClient.componentDao().selectAncestors(dbSession, component); OrganizationDto organizationDto = componentFinder.getOrganization(dbSession, component); return buildResponse(component, organizationDto, ancestors, lastAnalysis); } }
private static FileSourceDto newFileSourceDto(ComponentDto file) { return new FileSourceDto() .setFileUuid(file.uuid()) .setProjectUuid(file.projectUuid()) .setDataHash("0263047cd758c68c27683625f072f010") .setLineHashes(of("8d7b3d6b83c0a517eac07e1aac94b773")) .setCreatedAt(System.currentTimeMillis()) .setUpdatedAt(System.currentTimeMillis()) .setRevision("123456789") .setSrcHash("123456"); } }
public ComponentAndBranch insertProjectBranch(ComponentDto project, BranchType type, String branchKey) { BranchDto branchDto = newBranchDto(project.projectUuid(), type).setKey(branchKey); ComponentDto newComponent = dbTester.components().insertProjectBranch(project, branchDto); return new ComponentAndBranch(newComponent, branchDto); }
public static ComponentDoc toDocument(ComponentDto component) { return new ComponentDoc() .setId(component.uuid()) .setName(component.name()) .setKey(component.getDbKey()) .setProjectUuid(component.projectUuid()) .setOrganization(component.getOrganizationUuid()) .setLanguage(component.language()) .setQualifier(component.qualifier()); } }
public static LiveMeasureDto newLiveMeasure(ComponentDto component, MetricDto metric) { return new LiveMeasureDto() .setMetricId(metric.getId()) .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setData(String.valueOf(cursor++)) .setValue((double) cursor++) .setVariation((double) cursor++); } }