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)); }
@Override public boolean areWorkersPaused() { try (DbSession dbSession = dbClient.openSession(false)) { Optional<String> val = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.COMPUTE_ENGINE_PAUSE); return "true".equals(val.orElse(null)); } }
private Optional<Ce.Task> searchTaskByUuid(DbSession dbSession, Request request) { String textQuery = request.getQ(); if (textQuery == null) { return Optional.empty(); } Optional<CeQueueDto> queue = dbClient.ceQueueDao().selectByUuid(dbSession, textQuery); if (queue.isPresent()) { return Optional.of(formatter.formatQueue(dbSession, queue.get())); } Optional<CeActivityDto> activity = dbClient.ceActivityDao().selectByUuid(dbSession, textQuery); return activity.map(ceActivityDto -> formatter.formatActivity(dbSession, ceActivityDto, null, emptyList())); }
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 void validateLogin(SettingData data) { try (DbSession dbSession = dbClient.openSession(false)) { List<UserDto> users = dbClient.userDao().selectByLogins(dbSession, data.values).stream().filter(UserDto::isActive).collect(Collectors.toList()); checkRequest(data.values.size() == users.size(), "Error when validating login setting with key '%s' and values [%s]. A value is not a valid login.", data.key, data.values.stream().collect(Collectors.joining(", "))); } } }
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); } }
@Override public void resetTasksWithUnknownWorkerUUIDs(Set<String> knownWorkerUUIDs) { try (DbSession dbSession = dbClient.openSession(false)) { dbClient.ceQueueDao().resetTasksWithUnknownWorkerUUIDs(dbSession, knownWorkerUUIDs); dbSession.commit(); } }
@Before public void setUp() { when(dispatcher.getKey()).thenReturn("NewViolations"); when(emailChannel.getKey()).thenReturn("Email"); when(twitterChannel.getKey()).thenReturn("Twitter"); when(dbClient.openSession(anyBoolean())).thenReturn(dbSession); when(dbClient.propertiesDao()).thenReturn(propertiesDao); when(dbClient.notificationQueueDao()).thenReturn(notificationQueueDao); when(dbClient.authorizationDao()).thenReturn(authorizationDao); underTest = new DefaultNotificationManager(new NotificationChannel[] {emailChannel, twitterChannel}, dbClient); }
private void cleanUpTaskInputOrphans() { try (DbSession dbSession = dbClient.openSession(false)) { // Reports that have been processed are not kept in database yet. // They are supposed to be systematically dropped. // Let's clean-up orphans if any. List<String> uuids = dbClient.ceTaskInputDao().selectUuidsNotInQueue(dbSession); dbClient.ceTaskInputDao().deleteByUuids(dbSession, uuids); dbSession.commit(); } }
private UserDto getUser(DbSession dbSession, String userLogin) { return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin), "User with login '%s' is not found'", userLogin); } }
@Override public QualityGate findDefaultQualityGate(Organization organization) { try (DbSession dbSession = dbClient.openSession(false)) { QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid()); if (qualityGateDto == null) { throw new IllegalStateException("The default Quality gate is missing on organization " + organization.getKey()); } return toQualityGate(dbSession, qualityGateDto); } }
@Override public Map<String, String> loadAll() { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.propertiesDao().selectGlobalProperties(dbSession) .stream() .collect(MoreCollectors.uniqueIndex(PropertyDto::getKey, p -> defaultString(p.getValue()))); } }
@Test public void remove_saves_error_message_and_stacktrace_when_exception_is_provided() { Throwable error = new NullPointerException("Fake NPE to test persistence to DB"); CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1")); Optional<CeTask> peek = underTest.peek(WORKER_UUID_1); underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error); Optional<CeActivityDto> activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()); assertThat(activityDto).isPresent(); assertThat(activityDto.get().getErrorMessage()).isEqualTo(error.getMessage()); assertThat(activityDto.get().getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error)); assertThat(activityDto.get().getErrorType()).isNull(); }
private void deleteQualityGates(DbSession dbSession, OrganizationDto organization) { Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization); dbClient.qualityGateDao().deleteByUuids(dbSession, qualityGates.stream() .filter(q -> !q.isBuiltIn()) .map(QualityGateDto::getUuid) .collect(MoreCollectors.toList())); dbClient.qualityGateDao().deleteOrgQualityGatesByOrganization(dbSession, organization); }
@Test public void test_queue_state_with_default_settings() { when(dbClient.ceQueueDao().countByStatus(any(), eq(CeQueueDto.Status.PENDING))).thenReturn(10); when(dbClient.ceQueueDao().countByStatus(any(), eq(CeQueueDto.Status.IN_PROGRESS))).thenReturn(1); CeQueueGlobalSection underTest = new CeQueueGlobalSection(dbClient); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Total Pending", 10); assertThatAttributeIs(section, "Total In Progress", 1); assertThatAttributeIs(section, "Max Workers per Node", 1); }
private void doHandle(Request request, Response response, String componentKey, @Nullable String branchKey, @Nullable String pullRequestKey) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, componentKey, branchKey, pullRequestKey); userSession.checkComponentPermission(UserRole.USER, component); checkRequest(isProject(component), "Component '%s' must be a project.", componentKey); AnalysisStatusWsResponse.Builder responseBuilder = AnalysisStatusWsResponse.newBuilder(); CeActivityDto lastActivity = dbClient.ceActivityDao().selectLastByComponentUuid(dbSession, component.uuid()).orElse(null); responseBuilder.setComponent(formatComponent(dbSession, component, lastActivity, branchKey, pullRequestKey)); writeProtobuf(responseBuilder.build(), request, response); } }
private Optional<ServerId> readCurrentServerId(DbSession dbSession) { PropertyDto dto = dbClient.propertiesDao().selectGlobalProperty(dbSession, SERVER_ID); if (dto == null) { return Optional.empty(); } String value = dto.getValue(); if (isEmpty(value)) { return Optional.empty(); } return Optional.of(ServerId.parse(value)); }
@Override public boolean isEnabled(DbSession dbSession) { Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.ORGANIZATION_ENABLED); return value.map("true"::equals).orElse(false); }
@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()); }
private void verifyDb(ServerId expectedServerId, String expectedChecksum) { assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, CoreProperties.SERVER_ID)) .extracting(PropertyDto::getValue) .containsExactly(expectedServerId.toString()); assertThat(dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.SERVER_ID_CHECKSUM)) .hasValue(expectedChecksum); }