Search in sources :

Example 81 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class IssueQueryFactoryTest method new_code_period_does_not_rely_on_date_for_reference_branch.

@Test
public void new_code_period_does_not_rely_on_date_for_reference_branch() {
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setPeriodMode(REFERENCE_BRANCH.name()).setPeriodParam("master"));
    SearchRequest request = new SearchRequest().setComponentUuids(Collections.singletonList(file.uuid())).setOnComponentOnly(true).setInNewCodePeriod(true);
    IssueQuery query = underTest.create(request);
    assertThat(query.componentUuids()).containsOnly(file.uuid());
    assertThat(query.newCodeOnReference()).isTrue();
    assertThat(query.createdAfter()).isNull();
}
Also used : SearchRequest(org.sonar.server.issue.SearchRequest) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 82 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class IssueQueryFactoryTest method leak_period_does_not_rely_on_date_for_reference_branch.

@Test
public void leak_period_does_not_rely_on_date_for_reference_branch() {
    long leakPeriodStart = addDays(new Date(), -14).getTime();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setPeriodMode(REFERENCE_BRANCH.name()).setPeriodParam("master"));
    SearchRequest request = new SearchRequest().setComponentUuids(Collections.singletonList(file.uuid())).setOnComponentOnly(true).setSinceLeakPeriod(true);
    IssueQuery query = underTest.create(request);
    assertThat(query.componentUuids()).containsOnly(file.uuid());
    assertThat(query.newCodeOnReference()).isTrue();
    assertThat(query.createdAfter()).isNull();
}
Also used : SearchRequest(org.sonar.server.issue.SearchRequest) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Date(java.util.Date) Test(org.junit.Test)

Example 83 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class ListAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto projectOrApp = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(projectOrApp);
        Collection<BranchDto> branches = dbClient.branchDao().selectByProject(dbSession, projectOrApp).stream().filter(b -> b.getBranchType() == BRANCH).collect(toList());
        List<String> branchUuids = branches.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder();
        branches.forEach(b -> addBranch(protobufResponse, b, qualityGateMeasuresByComponentUuids.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        WsUtils.writeProtobuf(protobufResponse.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) AbstractUserSession.insufficientPrivilegesException(org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException) ComponentFinder(org.sonar.server.component.ComponentFinder) ProjectBranches(org.sonarqube.ws.ProjectBranches) DbSession(org.sonar.db.DbSession) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) WebService(org.sonar.api.server.ws.WebService) Map(java.util.Map) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) Resources(com.google.common.io.Resources) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) WsUtils(org.sonar.server.ws.WsUtils) BranchesWs.addProjectParam(org.sonar.server.branch.ws.BranchesWs.addProjectParam) PARAM_PROJECT(org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_PROJECT) BRANCH(org.sonar.db.component.BranchType.BRANCH) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) List(java.util.List) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ACTION_LIST(org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_LIST) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 84 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class ComponentTreeAction method load.

private ComponentTreeData load(ComponentTreeRequest wsRequest) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto baseComponent = loadComponent(dbSession, wsRequest);
        checkPermissions(baseComponent);
        Optional<SnapshotDto> baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid());
        if (baseSnapshot.isEmpty()) {
            return ComponentTreeData.builder().setBaseComponent(baseComponent).build();
        }
        Set<String> requestedMetricKeys = new HashSet<>(wsRequest.getMetricKeys());
        Set<String> metricKeysToSearch = new HashSet<>(requestedMetricKeys);
        boolean isPR = isPR(wsRequest.getPullRequest());
        if (isPR) {
            PrMeasureFix.addReplacementMetricKeys(metricKeysToSearch);
        }
        ComponentTreeQuery componentTreeQuery = toComponentTreeQuery(wsRequest, baseComponent);
        List<ComponentDto> components = searchComponents(dbSession, componentTreeQuery);
        List<MetricDto> metrics = searchMetrics(dbSession, metricKeysToSearch);
        Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, baseComponent, componentTreeQuery, components, metrics);
        if (isPR) {
            PrMeasureFix.removeMetricsNotRequested(metrics, requestedMetricKeys);
            PrMeasureFix.createReplacementMeasures(metrics, measuresByComponentUuidAndMetric, requestedMetricKeys);
        }
        components = filterComponents(components, measuresByComponentUuidAndMetric, metrics, wsRequest);
        components = filterAuthorizedComponents(components);
        components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric);
        int componentCount = components.size();
        components = paginateComponents(components, wsRequest);
        return ComponentTreeData.builder().setBaseComponent(baseComponent).setComponentsFromDb(components).setComponentCount(componentCount).setMeasuresByComponentUuidAndMetric(measuresByComponentUuidAndMetric).setMetrics(metrics).setPeriod(snapshotToWsPeriods(baseSnapshot.get()).orElse(null)).setReferenceComponentsByUuid(searchReferenceComponentsById(dbSession, components)).build();
    }
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) ComponentTreeQuery(org.sonar.db.component.ComponentTreeQuery) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 85 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class SearchAction method doHandle.

private SearchWsResponse doHandle(SearchRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkPermission(GlobalPermission.ADMINISTER);
        ComponentQuery query = buildDbQuery(request);
        Paging paging = buildPaging(dbSession, request, query);
        List<ComponentDto> components = dbClient.componentDao().selectByQuery(dbSession, query, paging.offset(), paging.pageSize());
        Set<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(MoreCollectors.toHashSet(components.size()));
        Map<String, Long> lastAnalysisDateByComponentUuid = dbClient.snapshotDao().selectLastAnalysisDateByProjects(dbSession, componentUuids).stream().collect(Collectors.toMap(ProjectLastAnalysisDateDto::getProjectUuid, ProjectLastAnalysisDateDto::getDate));
        Map<String, SnapshotDto> snapshotsByComponentUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, componentUuids).stream().collect(MoreCollectors.uniqueIndex(SnapshotDto::getComponentUuid, identity()));
        return buildResponse(components, snapshotsByComponentUuid, lastAnalysisDateByComponentUuid, paging);
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentQuery(org.sonar.db.component.ComponentQuery) Paging(org.sonar.api.utils.Paging) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto)

Aggregations

SnapshotDto (org.sonar.db.component.SnapshotDto)326 Test (org.junit.Test)257 ComponentDto (org.sonar.db.component.ComponentDto)219 MetricDto (org.sonar.db.metric.MetricDto)54 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)31 EventDto (org.sonar.db.event.EventDto)30 ProjectDto (org.sonar.db.project.ProjectDto)27 DbSession (org.sonar.db.DbSession)26 SnapshotTesting.newAnalysis (org.sonar.db.component.SnapshotTesting.newAnalysis)21 BranchDto (org.sonar.db.component.BranchDto)20 SearchEventsWsResponse (org.sonarqube.ws.Developers.SearchEventsWsResponse)20 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)17 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 DbClient (org.sonar.db.DbClient)14 NotFoundException (org.sonar.server.exceptions.NotFoundException)14 List (java.util.List)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 Rule (org.junit.Rule)13 UserRole (org.sonar.api.web.UserRole)13 DbTester (org.sonar.db.DbTester)13