Search in sources :

Example 1 with EventComponentChangeDto

use of org.sonar.db.event.EventComponentChangeDto in project sonarqube by SonarSource.

the class SearchResponseBuilder method addQualityGateInformation.

private void addQualityGateInformation(EventDto event) {
    wsQualityGate.clear();
    List<EventComponentChangeDto> eventComponentChangeDtos = searchData.componentChangesByEventUuid.get(event.getUuid());
    if (eventComponentChangeDtos.isEmpty()) {
        return;
    }
    if (event.getData() != null) {
        try {
            Gson gson = new Gson();
            Data data = gson.fromJson(event.getData(), Data.class);
            wsQualityGate.setStillFailing(data.isStillFailing());
            wsQualityGate.setStatus(data.getStatus());
        } catch (JsonSyntaxException ex) {
            LOGGER.error("Unable to retrieve data from event uuid=" + event.getUuid(), ex);
            return;
        }
    }
    wsQualityGate.addAllFailing(eventComponentChangeDtos.stream().map(SearchResponseBuilder::toFailing).collect(toList()));
    wsEvent.setQualityGate(wsQualityGate.build());
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto)

Example 2 with EventComponentChangeDto

use of org.sonar.db.event.EventComponentChangeDto in project sonarqube by SonarSource.

the class SearchResponseBuilder method addBranchChange.

private static Project addBranchChange(Collection<EventComponentChangeDto> changes) {
    if (changes.size() != 2) {
        throw new IllegalStateException(format("Too many changes on same project (%d) for eventComponentChange uuids : %s", changes.size(), changes.stream().map(EventComponentChangeDto::getUuid).collect(Collectors.joining(","))));
    }
    Optional<EventComponentChangeDto> addedChange = changes.stream().filter(c -> c.getCategory().equals(EventComponentChangeDto.ChangeCategory.ADDED)).findFirst();
    Optional<EventComponentChangeDto> removedChange = changes.stream().filter(c -> c.getCategory().equals(EventComponentChangeDto.ChangeCategory.REMOVED)).findFirst();
    if (!addedChange.isPresent() || !removedChange.isPresent() || addedChange.equals(removedChange)) {
        Iterator<EventComponentChangeDto> iterator = changes.iterator();
        // We are missing two different ADDED and REMOVED changes
        EventComponentChangeDto firstChange = iterator.next();
        EventComponentChangeDto secondChange = iterator.next();
        throw new IllegalStateException(format("Incorrect changes : [uuid=%s change=%s, branch=%s] and [uuid=%s, change=%s, branch=%s]", firstChange.getUuid(), firstChange.getCategory().name(), firstChange.getComponentBranchKey(), secondChange.getUuid(), secondChange.getCategory().name(), secondChange.getComponentBranchKey()));
    }
    return new Project().setName(addedChange.get().getComponentName()).setKey(addedChange.get().getComponentKey()).setChangeType("BRANCH_CHANGED").setNewBranch(addedChange.get().getComponentBranchKey()).setOldBranch(removedChange.get().getComponentBranchKey());
}
Also used : ListMultimap(com.google.common.collect.ListMultimap) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto) SearchResponse(org.sonarqube.ws.ProjectAnalyses.SearchResponse) Loggers(org.sonar.api.utils.log.Loggers) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Gson(com.google.gson.Gson) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) ProjectAnalyses(org.sonarqube.ws.ProjectAnalyses) Iterator(java.util.Iterator) QualityGate(org.sonarqube.ws.ProjectAnalyses.QualityGate) JsonSyntaxException(com.google.gson.JsonSyntaxException) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Analysis(org.sonarqube.ws.ProjectAnalyses.Analysis) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) EventCategory.fromLabel(org.sonar.server.projectanalysis.ws.EventCategory.fromLabel) SnapshotDto(org.sonar.db.component.SnapshotDto) EventDto(org.sonar.db.event.EventDto) Event(org.sonarqube.ws.ProjectAnalyses.Event) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto)

Example 3 with EventComponentChangeDto

use of org.sonar.db.event.EventComponentChangeDto in project sonarqube by SonarSource.

the class SearchActionTest method incorrect_eventcomponentchange_two_identical_changes_added_on_same_project.

@Test
public void incorrect_eventcomponentchange_two_identical_changes_added_on_same_project() {
    ComponentDto application = db.components().insertPublicApplication();
    userSession.registerApplication(toProjectDto(application, 1L));
    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
    EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
    EventComponentChangeDto changeDto1 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
    EventComponentChangeDto changeDto2 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
    db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
    db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto2, eventPurgeData);
    db.getSession().commit();
    List<Analysis> result = call(application.getDbKey()).getAnalysesList();
    assertThat(result).hasSize(1);
    List<Event> events = result.get(0).getEventsList();
    assertThat(events).extracting(Event::getName, Event::getCategory, Event::getKey).containsExactly(tuple("", DEFINITION_CHANGE.name(), "E11"));
    assertThat(events.get(0).getDefinitionChange().getProjectsList()).isEmpty();
    assertThat(logTester.getLogs(LoggerLevel.ERROR)).extracting(LogAndArguments::getFormattedMsg).containsExactly(format("Incorrect changes : [uuid=%s change=ADDED, branch=master] and [uuid=%s, change=ADDED, branch=master]", changeDto1.getUuid(), changeDto2.getUuid()));
}
Also used : SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Analysis(org.sonarqube.ws.ProjectAnalyses.Analysis) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) EventDto(org.sonar.db.event.EventDto) EventTesting.newEvent(org.sonar.db.event.EventTesting.newEvent) Event(org.sonarqube.ws.ProjectAnalyses.Event) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto) EventPurgeData(org.sonar.db.event.EventPurgeData) Test(org.junit.Test)

Example 4 with EventComponentChangeDto

use of org.sonar.db.event.EventComponentChangeDto in project sonarqube by SonarSource.

the class SearchActionTest method return_definition_change_events_on_application_analyses.

@Test
public void return_definition_change_events_on_application_analyses() {
    ComponentDto application = db.components().insertPublicApplication();
    userSession.registerApplication(toProjectDto(application, 1L));
    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
    EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
    EventComponentChangeDto changeDto1 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
    EventComponentChangeDto changeDto2 = generateEventComponentChange(event, REMOVED, "Another project", "app2", "master", uuidFactoryFast.create());
    insertEventComponentChanges(application, firstAnalysis, changeDto1, changeDto2);
    List<Analysis> result = call(application.getDbKey()).getAnalysesList();
    assertThat(result).hasSize(1);
    List<Event> events = result.get(0).getEventsList();
    assertThat(events).extracting(Event::getName, Event::getCategory, Event::getKey).containsExactly(tuple("", DEFINITION_CHANGE.name(), "E11"));
    assertThat(events.get(0).getDefinitionChange().getProjectsList()).extracting(Project::getChangeType, Project::getName, Project::getKey, Project::getNewBranch, Project::getOldBranch).containsExactly(tuple("ADDED", "My project", "app1", "", ""), tuple("REMOVED", "Another project", "app2", "", ""));
}
Also used : SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Analysis(org.sonarqube.ws.ProjectAnalyses.Analysis) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) EventDto(org.sonar.db.event.EventDto) EventTesting.newEvent(org.sonar.db.event.EventTesting.newEvent) Event(org.sonarqube.ws.ProjectAnalyses.Event) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto) Test(org.junit.Test)

Example 5 with EventComponentChangeDto

use of org.sonar.db.event.EventComponentChangeDto in project sonarqube by SonarSource.

the class SearchActionTest method incorrect_eventcomponentchange_incorrect_category.

@Test
public void incorrect_eventcomponentchange_incorrect_category() {
    ComponentDto application = db.components().insertPublicApplication();
    userSession.registerApplication(toProjectDto(application, 1L));
    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
    EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
    EventComponentChangeDto changeDto1 = generateEventComponentChange(event, FAILED_QUALITY_GATE, "My project", "app1", "master", uuidFactoryFast.create());
    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
    db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
    db.getSession().commit();
    List<Analysis> result = call(application.getDbKey()).getAnalysesList();
    assertThat(result).hasSize(1);
    List<Event> events = result.get(0).getEventsList();
    assertThat(events).extracting(Event::getName, Event::getCategory, Event::getKey).containsExactly(tuple("", DEFINITION_CHANGE.name(), "E11"));
    assertThat(events.get(0).getDefinitionChange().getProjectsList()).isEmpty();
    assertThat(logTester.getLogs(LoggerLevel.ERROR)).extracting(LogAndArguments::getFormattedMsg).containsExactly("Unknown change FAILED_QUALITY_GATE for eventComponentChange uuid: " + changeDto1.getUuid());
}
Also used : SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Analysis(org.sonarqube.ws.ProjectAnalyses.Analysis) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) EventDto(org.sonar.db.event.EventDto) EventTesting.newEvent(org.sonar.db.event.EventTesting.newEvent) Event(org.sonarqube.ws.ProjectAnalyses.Event) EventComponentChangeDto(org.sonar.db.event.EventComponentChangeDto) EventPurgeData(org.sonar.db.event.EventPurgeData) Test(org.junit.Test)

Aggregations

EventComponentChangeDto (org.sonar.db.event.EventComponentChangeDto)10 SnapshotDto (org.sonar.db.component.SnapshotDto)8 EventDto (org.sonar.db.event.EventDto)8 Test (org.junit.Test)7 ComponentDto (org.sonar.db.component.ComponentDto)7 Analysis (org.sonarqube.ws.ProjectAnalyses.Analysis)7 Event (org.sonarqube.ws.ProjectAnalyses.Event)7 SnapshotTesting.newAnalysis (org.sonar.db.component.SnapshotTesting.newAnalysis)6 EventTesting.newEvent (org.sonar.db.event.EventTesting.newEvent)6 EventPurgeData (org.sonar.db.event.EventPurgeData)5 Gson (com.google.gson.Gson)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 ListMultimap (com.google.common.collect.ListMultimap)1 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 String.format (java.lang.String.format)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1