Search in sources :

Example 1 with Event

use of org.sonarqube.ws.Developers.SearchEventsWsResponse.Event 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 2 with Event

use of org.sonarqube.ws.Developers.SearchEventsWsResponse.Event in project sonarqube by SonarSource.

the class SearchEventsActionTest method events.

@Test
public void events() {
    userSession.logIn().setRoot();
    when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io");
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project);
    SnapshotDto projectAnalysis = insertAnalysis(project, 1_500_000_000_000L);
    db.events().insertEvent(newQualityGateEvent(projectAnalysis).setDate(projectAnalysis.getCreatedAt()).setName("Passed"));
    insertIssue(project, projectAnalysis);
    insertIssue(project, projectAnalysis);
    SnapshotDto branchAnalysis = insertAnalysis(branch, 1_501_000_000_000L);
    db.events().insertEvent(newQualityGateEvent(branchAnalysis).setDate(branchAnalysis.getCreatedAt()).setName("Failed"));
    insertIssue(branch, branchAnalysis);
    issueIndexer.indexAllIssues();
    SearchEventsWsResponse result = ws.newRequest().setParam(PARAM_PROJECTS, project.getKey()).setParam(PARAM_FROM, formatDateTime(1_499_000_000_000L)).executeProtobuf(SearchEventsWsResponse.class);
    assertThat(result.getEventsList()).extracting(Event::getCategory, Event::getProject, Event::getMessage).containsOnly(tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' changed to 'Passed'", project.name())), tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branch.getBranch())), tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s'", project.name())), tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branch.getBranch())));
    verify(issueIndexSyncProgressChecker).checkIfAnyComponentsNeedIssueSync(any(), argThat(arg -> arg.contains(project.getKey())));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CeTaskTypes(org.sonar.db.ce.CeTaskTypes) EventCategory(org.sonar.server.projectanalysis.ws.EventCategory) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Date(java.util.Date) EsTester(org.sonar.server.es.EsTester) EventTesting.newEvent(org.sonar.db.event.EventTesting.newEvent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Server(org.sonar.api.platform.Server) RuleType(org.sonar.api.rules.RuleType) CeActivityDto(org.sonar.db.ce.CeActivityDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) IssueIndex(org.sonar.server.issue.index.IssueIndex) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) RandomUtils.nextLong(org.apache.commons.lang.math.RandomUtils.nextLong) UserSessionRule(org.sonar.server.tester.UserSessionRule) PARAM_PROJECTS(org.sonar.server.developers.ws.SearchEventsAction.PARAM_PROJECTS) DbTester(org.sonar.db.DbTester) RandomUtils.nextInt(org.apache.commons.lang.math.RandomUtils.nextInt) Assertions.tuple(org.assertj.core.api.Assertions.tuple) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) KeyExamples(org.sonar.server.ws.KeyExamples) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) IssueIndexSyncProgressChecker(org.sonar.server.issue.index.IssueIndexSyncProgressChecker) CeQueueDto(org.sonar.db.ce.CeQueueDto) PARAM_FROM(org.sonar.server.developers.ws.SearchEventsAction.PARAM_FROM) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchEventsWsResponse(org.sonarqube.ws.Developers.SearchEventsWsResponse) EventDto(org.sonar.db.event.EventDto) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) Event(org.sonarqube.ws.Developers.SearchEventsWsResponse.Event) Mockito.mock(org.mockito.Mockito.mock) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) SearchEventsWsResponse(org.sonarqube.ws.Developers.SearchEventsWsResponse) Test(org.junit.Test)

Aggregations

String.format (java.lang.String.format)2 Date (java.util.Date)2 IntStream (java.util.stream.IntStream)2 Stream (java.util.stream.Stream)2 Server (org.sonar.api.platform.Server)2 WebService (org.sonar.api.server.ws.WebService)2 DateUtils.formatDateTime (org.sonar.api.utils.DateUtils.formatDateTime)2 UserRole (org.sonar.api.web.UserRole)2 ComponentDto (org.sonar.db.component.ComponentDto)2 SnapshotDto (org.sonar.db.component.SnapshotDto)2 EventDto (org.sonar.db.event.EventDto)2 IssueIndex (org.sonar.server.issue.index.IssueIndex)2 IssueIndexSyncProgressChecker (org.sonar.server.issue.index.IssueIndexSyncProgressChecker)2 EventCategory (org.sonar.server.projectanalysis.ws.EventCategory)2 KeyExamples (org.sonar.server.ws.KeyExamples)2 SearchEventsWsResponse (org.sonarqube.ws.Developers.SearchEventsWsResponse)2 Event (org.sonarqube.ws.Developers.SearchEventsWsResponse.Event)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1