/** * On MySQL, as PROJECTS.KEE is not unique, if the same project is provisioned multiple times, then it will be duplicated in the database. * So, after creating a project, we commit, and we search in the db if their are some duplications and we remove them. * * SONAR-6332 */ private void removeDuplicatedProjects(DbSession session, String projectKey) { List<ComponentDto> duplicated = dbClient.componentDao().selectComponentsHavingSameKeyOrderedById(session, projectKey); for (int i = 1; i < duplicated.size(); i++) { dbClient.componentDao().delete(session, duplicated.get(i).getId()); } }
private ComponentDto toProject(String projectKey) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, projectKey); checkState(opt.isPresent(), "Project with key '%s' can't be found", projectKey); return opt.get(); } }
private Map<String, ComponentDto> loadProjects(DbSession dbSession, Collection<ComponentDto> components) { Set<String> projectUuids = components.stream() .filter(c -> QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT.contains(c.qualifier())) .map(ComponentDto::projectUuid) .collect(MoreCollectors.toSet()); return dbClient.componentDao().selectByUuids(dbSession, projectUuids).stream() .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); }
/** * 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)); }
@CheckForNull private ComponentDto loadComponent(DbSession dbSession, Request request) { String componentId = request.getComponentId(); if (componentId == null) { return null; } return checkFoundWithOptional(dbClient.componentDao().selectByUuid(dbSession, componentId), "Component '%s' does not exist", componentId); }
private ComponentDto getProjectOrApplication(DbSession dbSession, SnapshotDto analysis) { ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, analysis.getComponentUuid()).orElse(null); checkState(project != null, "Project of analysis '%s' is not found", analysis.getUuid()); checkArgument(ALLOWED_QUALIFIERS.contains(project.qualifier()) && Scopes.PROJECT.equals(project.scope()), "An event must be created on a project or an application"); return project; }
private void deleteProjects(DbSession dbSession, OrganizationDto organization) { List<ComponentDto> roots = dbClient.componentDao().selectProjectsByOrganization(dbSession, organization.getUuid()); try { componentCleanerService.delete(dbSession, roots); } finally { Set<Project> projects = roots.stream() .filter(DeleteAction::isMainProject) .map(Project::from) .collect(MoreCollectors.toSet()); projectLifeCycleListeners.onProjectsDeleted(projects); } }
@Override public void indexOnStartup(Set<IndexType> uninitializedIndexTypes) { try (DbSession dbSession = dbClient.openSession(false)) { Map<String, String> viewAndProjectViewUuidMap = newHashMap(); for (UuidWithProjectUuidDto uuidWithProjectUuidDto : dbClient.componentDao().selectAllViewsAndSubViews(dbSession)) { viewAndProjectViewUuidMap.put(uuidWithProjectUuidDto.getUuid(), uuidWithProjectUuidDto.getProjectUuid()); } index(dbSession, viewAndProjectViewUuidMap, false, Size.LARGE); } }
private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsRequest request, Paging paging) { com.google.common.base.Optional<ProjectWsRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); if (project.isPresent()) { return singletonList(wsSupport.getRootComponentOrModule(dbSession, project.get())); } return dbClient.componentDao().selectByQuery(dbSession, toDbQuery(request), paging.offset(), paging.pageSize()); }
private ProjectAndSnapshot getSnapshotThenProject(DbSession dbSession, String analysisUuid) { SnapshotDto snapshotDto = getSnapshot(dbSession, analysisUuid); ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, snapshotDto.getComponentUuid()); return new ProjectAndSnapshot(projectDto, snapshotDto); }
@Test public void bulk_updateKey_on_branch_containing_slash() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch/with/slash")); String newKey = "newKey"; underTest.bulkUpdateKey(dbSession, project.uuid(), project.getKey(), newKey, t -> true); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, newKey)).hasSize(1); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, ComponentDto.generateBranchKey(newKey, branch.getBranch()))).hasSize(1); }
private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issueDto) { ComponentDto project = dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get(); userSession.logIn("john") .addProjectPermission(ISSUE_ADMIN, project) .addProjectPermission(USER, project); }
private ComponentDto insertProject(String projectUuid, String projectKey) { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization(), projectUuid) .setDbKey(projectKey); dbClient.componentDao().insert(dbSession, project); dbSession.commit(); return project; }
private ComponentDto createProject(String projectKey) { ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()).setDbKey(projectKey); dbClient.componentDao().insert(dbSession, project); dbSession.commit(); return project; } }
private ComponentDto createProject(String projectKey) { ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()).setDbKey(projectKey); dbClient.componentDao().insert(dbSession, project); dbSession.commit(); return project; }
private ComponentDto insertComponent(String key, String uuid) { ComponentDto componentDto = new ComponentDto() .setOrganizationUuid("org1") .setDbKey(key) .setUuid(uuid) .setUuidPath(uuid + ".") .setRootUuid(uuid) .setProjectUuid(uuid); dbClient.componentDao().insert(db.getSession(), componentDto); return componentDto; }
private ComponentDto insertComponent(String key, String uuid) { ComponentDto componentDto = new ComponentDto() .setOrganizationUuid("org1") .setDbKey(key) .setUuid(uuid) .setUuidPath(uuid + ".") .setRootUuid(uuid) .setProjectUuid(uuid); dbClient.componentDao().insert(db.getSession(), componentDto); return componentDto; }
@Test public void bulk_update_key_updates_disabled_components() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "A").setDbKey("my_project")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:module")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:inactive_module").setEnabled(false)); Set<RekeyedResource> rekeyedResources = underTest.bulkUpdateKey(dbSession, "A", "my_", "your_", doNotReturnAnyRekeyedResource()); List<ComponentDto> result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); assertThat(result) .hasSize(3) .extracting(ComponentDto::getDbKey) .containsOnlyOnce("your_project", "your_project:module", "your_project:inactive_module"); }
@Test public void delete_permissions_on_project_deletion() { ComponentDto project = createAndIndexPrivateProject(); UserDto user = db.users().insertUser(); db.users().insertProjectPermissionOnUser(user, USER, project); indexPermissions(project, ProjectIndexer.Cause.PROJECT_CREATION); verifyAuthorized(project, user); db.getDbClient().componentDao().delete(db.getSession(), project.getId()); indexPermissions(project, ProjectIndexer.Cause.PROJECT_DELETION); verifyNotAuthorized(project, user); assertThatAuthIndexHasSize(0); }
@Test public void not_fail_if_analysis_date_is_after_last_analysis() { ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY); dbClient.componentDao().insert(dbTester.getSession(), project); dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01 dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build()); underTest.execute(new TestComputationStepContext()); }