Search in sources :

Example 46 with SnapshotDto

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

the class WebhookQGChangeEventListenerTest method onIssueChangesCallsWebhookOnBranch.

public void onIssueChangesCallsWebhookOnBranch(BranchType branchType) {
    ProjectAndBranch nonMainBranch = insertBranch(branchType, "foo");
    SnapshotDto analysis = insertAnalysisTask(nonMainBranch);
    Configuration configuration = mock(Configuration.class);
    QGChangeEvent qualityGateEvent = newQGChangeEvent(nonMainBranch, analysis, configuration, null);
    mockWebhookEnabled(qualityGateEvent.getProject());
    underTest.onIssueChanges(qualityGateEvent, CHANGED_ISSUES_ARE_IGNORED);
    verifyWebhookCalled(nonMainBranch, analysis, qualityGateEvent.getProject());
}
Also used : Configuration(org.sonar.api.config.Configuration) SnapshotDto(org.sonar.db.component.SnapshotDto) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent)

Example 47 with SnapshotDto

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

the class IssueQueryFactory method setCreatedAfterFromRequest.

private void setCreatedAfterFromRequest(DbSession dbSession, IssueQuery.Builder builder, SearchRequest request, List<ComponentDto> componentUuids, ZoneId timeZone) {
    Date createdAfter = parseStartingDateOrDateTime(request.getCreatedAfter(), timeZone);
    String createdInLast = request.getCreatedInLast();
    if (notInNewCodePeriod(request)) {
        checkArgument(createdAfter == null || createdInLast == null, format("Parameters %s and %s cannot be set simultaneously", PARAM_CREATED_AFTER, PARAM_CREATED_IN_LAST));
        setCreatedAfterFromDates(builder, createdAfter, createdInLast, true);
    } else {
        // If the filter is on leak period
        checkArgument(createdAfter == null, "Parameters '%s' and '%s' or '%s' cannot be set simultaneously", PARAM_CREATED_AFTER, PARAM_IN_NEW_CODE_PERIOD, PARAM_SINCE_LEAK_PERIOD);
        checkArgument(createdInLast == null, format("Parameters '%s' and '%s' or '%s' cannot be set simultaneously", PARAM_CREATED_IN_LAST, PARAM_IN_NEW_CODE_PERIOD, PARAM_SINCE_LEAK_PERIOD));
        checkArgument(componentUuids.size() == 1, "One and only one component must be provided when searching in new code period");
        ComponentDto component = componentUuids.iterator().next();
        if (!QUALIFIERS_WITHOUT_LEAK_PERIOD.contains(component.qualifier()) && request.getPullRequest() == null) {
            Optional<SnapshotDto> snapshot = getLastAnalysis(dbSession, component);
            boolean isLastAnalysisUsingReferenceBranch = isLastAnalysisUsingReferenceBranch(snapshot);
            if (isLastAnalysisUsingReferenceBranch) {
                builder.newCodeOnReference(true);
            } else {
                // if last analysis has no period date, then no issue should be considered new.
                Date createdAfterFromSnapshot = findCreatedAfterFromComponentUuid(snapshot);
                setCreatedAfterFromDates(builder, createdAfterFromSnapshot, null, false);
            }
        }
    }
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Date(java.util.Date) DateUtils.longToDate(org.sonar.api.utils.DateUtils.longToDate)

Example 48 with SnapshotDto

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

the class ShowAction method doHandle.

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);
        return buildResponse(dbSession, component, ancestors, lastAnalysis.orElse(null));
    }
}
Also used : DbSession(org.sonar.db.DbSession) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto)

Example 49 with SnapshotDto

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

the class SearchEventsAction method computeQualityGateChangeEvents.

private Stream<Event> computeQualityGateChangeEvents(DbSession dbSession, Map<String, ComponentDto> projectsByUuid, Map<String, BranchDto> branchesByUuids, List<SnapshotDto> analyses) {
    Map<String, EventDto> eventsByComponentUuid = new HashMap<>();
    dbClient.eventDao().selectByAnalysisUuids(dbSession, analyses.stream().map(SnapshotDto::getUuid).collect(toList(analyses.size()))).stream().sorted(comparing(EventDto::getDate)).filter(e -> EventCategory.QUALITY_GATE.getLabel().equals(e.getCategory())).forEach(e -> eventsByComponentUuid.put(e.getComponentUuid(), e));
    Predicate<EventDto> branchPredicate = e -> branchesByUuids.get(e.getComponentUuid()).getBranchType() == BRANCH;
    return eventsByComponentUuid.values().stream().sorted(comparing(EventDto::getDate)).filter(branchPredicate).map(e -> {
        BranchDto branch = branchesByUuids.get(e.getComponentUuid());
        ComponentDto project = projectsByUuid.get(branch.getProjectUuid());
        checkState(project != null, "Found event '%s', for a component that we did not search for", e.getUuid());
        return Event.newBuilder().setCategory(EventCategory.fromLabel(e.getCategory()).name()).setProject(project.getKey()).setMessage(branch.isMain() ? format("Quality Gate status of project '%s' changed to '%s'", project.name(), e.getName()) : format("Quality Gate status of project '%s' on branch '%s' changed to '%s'", project.name(), branch.getKey(), e.getName())).setLink(computeDashboardLink(project, branch)).setDate(formatDateTime(e.getDate())).build();
    });
}
Also used : IntStream(java.util.stream.IntStream) BranchDto(org.sonar.db.component.BranchDto) EventCategory(org.sonar.server.projectanalysis.ws.EventCategory) Date(java.util.Date) UuidFromPairs.fromDates(org.sonar.server.developers.ws.UuidFromPairs.fromDates) HashMap(java.util.HashMap) Server(org.sonar.api.platform.Server) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Request(org.sonar.api.server.ws.Request) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) WebService(org.sonar.api.server.ws.WebService) String.join(java.lang.String.join) IssueIndex(org.sonar.server.issue.index.IssueIndex) Map(java.util.Map) Response(org.sonar.api.server.ws.Response) Comparator.comparing(java.util.Comparator.comparing) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) Predicate(java.util.function.Predicate) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collectors(java.util.stream.Collectors) KeyExamples(org.sonar.server.ws.KeyExamples) BRANCH(org.sonar.db.component.BranchType.BRANCH) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DbClient(org.sonar.db.DbClient) BadRequestException.checkRequest(org.sonar.server.exceptions.BadRequestException.checkRequest) URLEncoder(java.net.URLEncoder) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) UserRole(org.sonar.api.web.UserRole) IssueIndexSyncProgressChecker(org.sonar.server.issue.index.IssueIndexSyncProgressChecker) UuidFromPairs.componentUuids(org.sonar.server.developers.ws.UuidFromPairs.componentUuids) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) ProjectStatistics(org.sonar.server.issue.index.ProjectStatistics) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchEventsWsResponse(org.sonarqube.ws.Developers.SearchEventsWsResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EventDto(org.sonar.db.event.EventDto) DateUtils.parseDateTimeQuietly(org.sonar.api.utils.DateUtils.parseDateTimeQuietly) Event(org.sonarqube.ws.Developers.SearchEventsWsResponse.Event) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) HashMap(java.util.HashMap) SnapshotDto(org.sonar.db.component.SnapshotDto) EventDto(org.sonar.db.event.EventDto) ComponentDto(org.sonar.db.component.ComponentDto)

Example 50 with SnapshotDto

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

the class SetAction method getAnalysis.

private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid, ProjectDto project, BranchDto branch) {
    SnapshotDto snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid).orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", analysisUuid)));
    checkAnalysis(dbSession, project, branch, snapshotDto);
    return snapshotDto;
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) NotFoundException(org.sonar.server.exceptions.NotFoundException)

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