@CheckForNull private ComponentDto retrieveParentModuleIfNotCurrentComponent(ComponentDto componentDto, DbSession session) { final String moduleUuid = componentDto.moduleUuid(); if (moduleUuid == null || componentDto.uuid().equals(moduleUuid)) { return null; } return dbClient.componentDao().selectOrFailByUuid(session, moduleUuid); }
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()); }
private ComponentDto getRootProjectOrBranch(ComponentDto component, DbSession session) { if (!component.isRootProject()) { return dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid()); } else { return component; } }
ComponentDto project(DbSession session, DefaultIssue issue) { return getDbClient().componentDao().selectOrFailByUuid(session, issue.projectUuid()); }
private void validateProject(DbSession dbSession, Optional<ComponentDto> project, String rawProjectKey) { List<String> errors = new ArrayList<>(); if (!project.isPresent()) { return; } ComponentDto component = project.get(); if (!Qualifiers.PROJECT.equals(component.qualifier()) || !Scopes.PROJECT.equals(component.scope())) { errors.add(format("Component '%s' is not a project", rawProjectKey)); } if (!component.projectUuid().equals(component.uuid())) { // Project key is already used as a module of another project ComponentDto anotherBaseProject = dbClient.componentDao().selectOrFailByUuid(dbSession, component.projectUuid()); errors.add(format("The project '%s' is already defined in SonarQube but as a module of project '%s'. " + "If you really want to stop directly analysing project '%s', please first delete it from SonarQube and then relaunch the analysis of project '%s'.", rawProjectKey, anotherBaseProject.getKey(), anotherBaseProject.getKey(), rawProjectKey)); } if (!errors.isEmpty()) { throw BadRequestException.create(errors); } }
ComponentDto component(DbSession session, DefaultIssue issue) { return getDbClient().componentDao().selectOrFailByUuid(session, issue.componentUuid()); }
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); }
private void checkPermission(DbSession dbSession, CustomMeasureDto customMeasure) { userSession.checkLoggedIn(); ComponentDto component = dbClient.componentDao().selectOrFailByUuid(dbSession, customMeasure.getComponentUuid()); userSession.checkComponentPermission(UserRole.ADMIN, component); } }
private Map<String, String> keysByUUid(DbSession session, ComponentDto component) { Map<String, String> keysByUUid = newHashMap(); if (Scopes.PROJECT.equals(component.scope())) { List<ComponentDto> modulesTree = dbClient.componentDao().selectDescendantModules(session, component.uuid()); for (ComponentDto componentDto : modulesTree) { keysByUUid.put(componentDto.uuid(), componentDto.getKey()); } } else { String moduleUuid = component.moduleUuid(); checkArgument(moduleUuid != null, "The component '%s' has no module uuid", component.uuid()); ComponentDto module = dbClient.componentDao().selectOrFailByUuid(session, moduleUuid); keysByUUid.put(module.uuid(), module.getKey()); } return keysByUUid; }
/** * Same as {@link #saveIssue(DbSession, DefaultIssue, IssueChangeContext, String)} but populates the specified * {@link SearchResponseData} with the DTOs (rule and components) retrieved from DB to save the issue. */ public SearchResponseData saveIssueAndPreloadSearchResponseData(DbSession dbSession, DefaultIssue issue, IssueChangeContext context, @Nullable String comment, boolean refreshMeasures) { Optional<RuleDefinitionDto> rule = getRuleByKey(dbSession, issue.getRuleKey()); ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, issue.projectUuid()); ComponentDto component = getComponent(dbSession, issue, issue.componentUuid()); IssueDto issueDto = doSaveIssue(dbSession, issue, context, comment, rule, project, component); SearchResponseData result = new SearchResponseData(issueDto); rule.ifPresent(r -> result.addRules(singletonList(r))); result.addComponents(singleton(project)); result.addComponents(singleton(component)); if (refreshMeasures) { List<DefaultIssue> changedIssues = result.getIssues().stream().map(IssueDto::toDefaultIssue).collect(MoreCollectors.toList(result.getIssues().size())); issueChangePostProcessor.process(dbSession, changedIssues, singleton(component)); } return result; }
@Override public void onEnd(CeTask ceTask, CeActivityDto.Status status, @Nullable CeTaskResult taskResult, @Nullable Throwable error) { if (status == CeActivityDto.Status.SUCCESS) { return; } String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null); if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) { return; } if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.TYPE))) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid); checkScopeAndQualifier(projectDto); CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid()) .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid()))); ReportAnalysisFailureNotification taskFailureNotification = buildNotification(ceActivityDto, projectDto, error); notificationService.deliver(taskFailureNotificationSerializer.toNotification(taskFailureNotification)); } } }
@Test public void selectOrFailByUuid_fails_when_component_not_found() { db.components().insertPublicProject(); expectedException.expect(RowNotFoundException.class); underTest.selectOrFailByUuid(dbSession, "unknown"); }
@Test public void create_application() { NewComponent application = NewComponent.newComponentBuilder() .setKey("app-key") .setName("app-name") .setQualifier(APP) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); ComponentDto returned = underTest.create(db.getSession(), application, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.getDbKey()).isEqualTo("app-key"); assertThat(loaded.name()).isEqualTo("app-name"); assertThat(loaded.qualifier()).isEqualTo("APP"); assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue(); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isPresent(); assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); assertThat(branch.get().getMergeBranchUuid()).isNull(); assertThat(branch.get().getBranchType()).isEqualTo(BranchType.LONG); assertThat(branch.get().getUuid()).isEqualTo(returned.uuid()); assertThat(branch.get().getProjectUuid()).isEqualTo(returned.uuid()); }
ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.getDbKey()).isEqualTo(DEFAULT_PROJECT_KEY); assertThat(loaded.name()).isEqualTo(DEFAULT_PROJECT_NAME);
@Test public void create_view() { NewComponent view = NewComponent.newComponentBuilder() .setKey("view-key") .setName("view-name") .setQualifier(VIEW) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); ComponentDto returned = underTest.create(db.getSession(), view, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.getDbKey()).isEqualTo("view-key"); assertThat(loaded.name()).isEqualTo("view-name"); assertThat(loaded.qualifier()).isEqualTo("VW"); assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue(); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isNotPresent(); }
@Override public void handle(Request request, Response response) throws Exception { int id = request.mandatoryParamAsInt(PARAM_ID); String value = request.param(PARAM_VALUE); String description = request.param(PARAM_DESCRIPTION); checkParameters(value, description); try (DbSession dbSession = dbClient.openSession(true)) { CustomMeasureDto customMeasure = dbClient.customMeasureDao().selectById(dbSession, id); checkArgument(customMeasure != null, "Custom measure with id '%s' does not exist", id); int customMetricId = customMeasure.getMetricId(); MetricDto metric = dbClient.metricDao().selectById(dbSession, customMetricId); checkState(metric != null, "Metric with id '%s' does not exist", customMetricId); ComponentDto component = dbClient.componentDao().selectOrFailByUuid(dbSession, customMeasure.getComponentUuid()); checkPermissions(userSession, component); String userUuid = requireNonNull(userSession.getUuid(), "User uuid should not be null"); UserDto user = dbClient.userDao().selectByUuid(dbSession, userUuid); checkState(user != null, "User with uuid '%s' does not exist", userUuid); setValue(customMeasure, value, metric); setDescription(customMeasure, description); customMeasure.setUserUuid(user.getUuid()); customMeasure.setUpdatedAt(system.now()); dbClient.customMeasureDao().update(dbSession, customMeasure); dbSession.commit(); JsonWriter json = response.newJsonWriter(); customMeasureJsonWriter.write(json, customMeasure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS); json.close(); } }
@Test public void persist_private_flag_false_when_creating_project() { OrganizationDto organization = db.organizations().insert(); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .setOrganizationUuid(organization.getUuid()) .setPrivate(false) .build(); ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); }
@Test public void persist_private_flag_true_when_creating_project() { OrganizationDto organization = db.organizations().insert(); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .setOrganizationUuid(organization.getUuid()) .setPrivate(true) .build(); ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); }
ComponentDto project = dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid());
private ComponentDto getProject(ComponentDto module, DbSession session) { if (!module.isRootProject()) { return dbClient.componentDao().selectOrFailByUuid(session, module.projectUuid()); } else { return module; } }