Search in sources :

Example 6 with PULL_REQUEST

use of org.sonar.db.component.BranchType.PULL_REQUEST 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 project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        List<BranchDto> pullRequests = dbClient.branchDao().selectByProject(dbSession, project).stream().filter(b -> b.getBranchType() == PULL_REQUEST).collect(toList());
        List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao().selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())).stream().collect(uniqueIndex(BranchDto::getUuid));
        Map<String, PrStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream().collect(uniqueIndex(PrStatistics::getBranchUuid, Function.identity()));
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
        pullRequests.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        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) StringUtils(org.apache.commons.lang.StringUtils) ProjectPullRequests(org.sonarqube.ws.ProjectPullRequests) GlobalPermission(org.sonar.db.permission.GlobalPermission) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) DbProjectBranches(org.sonar.db.protobuf.DbProjectBranches) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) IssueIndex(org.sonar.server.issue.index.IssueIndex) PullRequestsWs.addProjectParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) PrStatistics(org.sonar.server.issue.index.PrStatistics) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) PARAM_PROJECT(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) PrStatistics(org.sonar.server.issue.index.PrStatistics) DbSession(org.sonar.db.DbSession) Objects(java.util.Objects)

Example 7 with PULL_REQUEST

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

the class ComponentTreeActionTest method fix_pull_request_new_issue_count_metrics.

@Test
public void fix_pull_request_new_issue_count_metrics() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
    MetricDto newBug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.NEW_BUGS_KEY));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, bug, m -> m.setValue(12.0d));
    ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, newBug.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
    assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
    assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getPeriods().getPeriodsValue(0).getValue()), Measure::getValue).containsExactlyInAnyOrder(tuple(newBug.getKey(), measure.getValue(), ""));
    assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getPeriod().getValue()), Measure::getValue).containsExactlyInAnyOrder(tuple(newBug.getKey(), measure.getValue(), ""));
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) SORT(org.sonar.api.server.ws.WebService.Param.SORT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MetricTesting(org.sonar.db.metric.MetricTesting) FILE(org.sonar.api.resources.Qualifiers.FILE) DEPRECATED_ADDITIONAL_PERIODS(org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_ADDITIONAL_PERIODS) LEAVES_STRATEGY(org.sonar.server.measure.ws.ComponentTreeAction.LEAVES_STRATEGY) DbSession(org.sonar.db.DbSession) PARAM_QUALIFIERS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_QUALIFIERS) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) NEW_SECURITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY) Measure(org.sonarqube.ws.Measures.Measure) PARAM_METRIC_SORT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT) Param(org.sonar.api.server.ws.WebService.Param) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) ComponentTesting(org.sonar.db.component.ComponentTesting) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) APP(org.sonar.api.resources.Qualifiers.APP) System2(org.sonar.api.utils.System2) NAME_SORT(org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT) METRIC_SORT(org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT) CoreMetrics(org.sonar.api.measures.CoreMetrics) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) ComponentTesting.newDirectory(org.sonar.db.component.ComponentTesting.newDirectory) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) PARAM_METRIC_SORT_FILTER(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT_FILTER) FLOAT(org.sonar.api.measures.Metric.ValueType.FLOAT) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) DISTRIB(org.sonar.api.measures.Metric.ValueType.DISTRIB) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) RATING(org.sonar.api.measures.Metric.ValueType.RATING) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) WITH_MEASURES_ONLY_METRIC_SORT_FILTER(org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) PARAM_METRIC_PERIOD_SORT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_PERIOD_SORT) UserSessionRule(org.sonar.server.tester.UserSessionRule) PARAM_STRATEGY(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_STRATEGY) Assertions.tuple(org.assertj.core.api.Assertions.tuple) PeriodValue(org.sonarqube.ws.Measures.PeriodValue) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) DIRECTORY(org.sonar.api.resources.Qualifiers.DIRECTORY) Test(org.junit.Test) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) INT(org.sonar.api.measures.Metric.ValueType.INT) UNIT_TEST_FILE(org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) METRIC_PERIOD_SORT(org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT) I18nRule(org.sonar.server.l18n.I18nRule) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) Metric(org.sonar.api.measures.Metric) ADDITIONAL_PERIOD(org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD) MetricDto(org.sonar.db.metric.MetricDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ComponentTreeWsResponse(org.sonarqube.ws.Measures.ComponentTreeWsResponse) Test(org.junit.Test)

Example 8 with PULL_REQUEST

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

the class SearchHistoryActionTest method pull_request.

@Test
public void pull_request() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d));
    SearchHistoryResponse result = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRICS, "ncloc").executeProtobuf(SearchHistoryResponse.class);
    assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(1);
    HistoryMeasure historyMeasure = result.getMeasures(0);
    assertThat(historyMeasure.getMetric()).isEqualTo(nclocMetric.getKey());
    assertThat(historyMeasure.getHistoryList()).extracting(m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(measure.getValue());
}
Also used : MeasureDto(org.sonar.db.measure.MeasureDto) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Arrays.asList(java.util.Arrays.asList) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) PARAM_TO(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO) HistoryValue(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryValue) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) PARAM_FROM(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) TestComponentFinder(org.sonar.server.component.TestComponentFinder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) LongStream(java.util.stream.LongStream) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Paging(org.sonarqube.ws.Common.Paging) Test(org.junit.Test) STATUS_UNPROCESSED(org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) ValueType(org.sonar.api.measures.Metric.ValueType) PARAM_METRICS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) ComponentDto(org.sonar.db.component.ComponentDto) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) Test(org.junit.Test)

Example 9 with PULL_REQUEST

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

the class ComponentActionTest method pull_request.

@Test
public void pull_request() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
    LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d));
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, complexity.getKey()).executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
}
Also used : ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) TestComponentFinder(org.sonar.server.component.TestComponentFinder) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) Double.parseDouble(java.lang.Double.parseDouble) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) USER(org.sonar.api.web.UserRole.USER) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Measures(org.sonarqube.ws.Measures) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Test(org.junit.Test)

Example 10 with PULL_REQUEST

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

the class ComponentActionTest method new_issue_count_measures_are_transformed_in_pr.

@Test
public void new_issue_count_measures_are_transformed_in_pr() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
    MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setValueType("INT"));
    MetricDto violations = db.measures().insertMetric(m1 -> m1.setKey("violations").setValueType("INT"));
    MetricDto newViolations = db.measures().insertMetric(m1 -> m1.setKey("new_violations").setValueType("INT"));
    LiveMeasureDto bugMeasure = db.measures().insertLiveMeasure(file, bugs, m -> m.setValue(12.0d).setVariation(null));
    LiveMeasureDto newBugMeasure = db.measures().insertLiveMeasure(file, newBugs, m -> m.setVariation(1d).setValue(null));
    LiveMeasureDto violationMeasure = db.measures().insertLiveMeasure(file, violations, m -> m.setValue(20.0d).setVariation(null));
    ComponentWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, newBugs.getKey() + "," + bugs.getKey() + "," + newViolations.getKey()).executeProtobuf(ComponentWsResponse.class);
    assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
    Function<Measures.Measure, Double> extractVariation = m -> {
        if (m.getPeriods().getPeriodsValueCount() > 0) {
            return parseDouble(m.getPeriods().getPeriodsValue(0).getValue());
        }
        return null;
    };
    assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric, extractVariation, m -> m.getValue().isEmpty() ? null : parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(newBugs.getKey(), bugMeasure.getValue(), null), tuple(bugs.getKey(), null, bugMeasure.getValue()), tuple(newViolations.getKey(), violationMeasure.getValue(), null));
}
Also used : ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) BadRequestException(org.sonar.server.exceptions.BadRequestException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_METRIC_KEYS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS) Component(org.sonarqube.ws.Measures.Component) TestComponentFinder(org.sonar.server.component.TestComponentFinder) ComponentTesting.newProjectCopy(org.sonar.db.component.ComponentTesting.newProjectCopy) Double.parseDouble(java.lang.Double.parseDouble) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) USER(org.sonar.api.web.UserRole.USER) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Measures(org.sonarqube.ws.Measures) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) PARAM_ADDITIONAL_FIELDS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS) MetricDto(org.sonar.db.metric.MetricDto) ComponentWsResponse(org.sonarqube.ws.Measures.ComponentWsResponse) Measures(org.sonarqube.ws.Measures) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Double.parseDouble(java.lang.Double.parseDouble) Test(org.junit.Test)

Aggregations

PULL_REQUEST (org.sonar.db.component.BranchType.PULL_REQUEST)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Rule (org.junit.Rule)8 Test (org.junit.Test)8 System2 (org.sonar.api.utils.System2)8 UserRole (org.sonar.api.web.UserRole)8 DbTester (org.sonar.db.DbTester)8 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)7 Assertions.tuple (org.assertj.core.api.Assertions.tuple)7 DbClient (org.sonar.db.DbClient)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 NotFoundException (org.sonar.server.exceptions.NotFoundException)7 String.format (java.lang.String.format)6 List (java.util.List)6 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)6 ComponentDto (org.sonar.db.component.ComponentDto)6 MetricDto (org.sonar.db.metric.MetricDto)6 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)6 UserSessionRule (org.sonar.server.tester.UserSessionRule)6 WsActionTester (org.sonar.server.ws.WsActionTester)6