Search in sources :

Example 1 with SonarQubeNotification

use of org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification in project sonarlint-core by SonarSource.

the class NotificationChecker method parseResponse.

private static List<SonarQubeNotification> parseResponse(String contents) {
    List<SonarQubeNotification> notifications = new ArrayList<>();
    try {
        JsonParser parser = new JsonParser();
        JsonObject root = parser.parse(contents).getAsJsonObject();
        JsonArray events = root.get("events").getAsJsonArray();
        for (JsonElement el : events) {
            JsonObject event = el.getAsJsonObject();
            String category = getOrFail(event, "category");
            String message = getOrFail(event, "message");
            String link = getOrFail(event, "link");
            String projectKey = getOrFail(event, "project");
            String dateTime = getOrFail(event, "date");
            ZonedDateTime time = ZonedDateTime.parse(dateTime, TIME_FORMATTER);
            notifications.add(new DefaultSonarQubeNotification(category, message, link, projectKey, time));
        }
    } catch (Exception e) {
        LOG.error("Failed to parse SonarQube notifications response", e);
        return Collections.emptyList();
    }
    return notifications;
}
Also used : JsonArray(com.google.gson.JsonArray) ZonedDateTime(java.time.ZonedDateTime) JsonElement(com.google.gson.JsonElement) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) DefaultSonarQubeNotification(org.sonarsource.sonarlint.core.container.model.DefaultSonarQubeNotification) DefaultSonarQubeNotification(org.sonarsource.sonarlint.core.container.model.DefaultSonarQubeNotification) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 2 with SonarQubeNotification

use of org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification in project sonarlint-core by SonarSource.

the class NotificationTimerTask method requestForServer.

private void requestForServer(ServerConfiguration serverConfiguration, List<NotificationConfiguration> configs) {
    try {
        Map<String, ZonedDateTime> request = configs.stream().collect(Collectors.toMap(NotificationConfiguration::projectKey, NotificationTimerTask::getLastNotificationTime, MERGE_TIMES));
        NotificationChecker notificationChecker = checkerFactory.create(serverConfiguration);
        List<SonarQubeNotification> notifications = notificationChecker.request(request);
        for (SonarQubeNotification n : notifications) {
            Stream<NotificationConfiguration> matchingConfStream = configs.stream();
            if (n.projectKey() != null) {
                matchingConfStream = matchingConfStream.filter(c -> c.projectKey().equals(n.projectKey()));
            }
            matchingConfStream.forEach(c -> {
                c.listener().handle(n);
                c.lastNotificationTime().set(n.time());
            });
        }
    } catch (Exception e) {
        LOG.warn("Failed to request SonarQube events to " + serverConfiguration.getUrl(), e);
    }
}
Also used : Logger(org.slf4j.Logger) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) NotificationConfiguration(org.sonarsource.sonarlint.core.client.api.common.NotificationConfiguration) Map(java.util.Map) TimerTask(java.util.TimerTask) Collections(java.util.Collections) ZonedDateTime(java.time.ZonedDateTime) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) NotificationConfiguration(org.sonarsource.sonarlint.core.client.api.common.NotificationConfiguration)

Example 3 with SonarQubeNotification

use of org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification in project sonarlint-core by SonarSource.

the class NotificationCheckerTest method testFailCode.

@Test
public void testFailCode() {
    ZonedDateTime timestamp = ZonedDateTime.of(2017, 06, 04, 20, 0, 0, 0, ZoneOffset.ofHours(0));
    String expectedUrl = "api/developers/search_events?projects=myproject&from=2017-06-04T20%3A00%3A00%2B0000";
    SonarLintWsClient client = WsClientTestUtils.createMock();
    WsClientTestUtils.addFailedResponse(client, expectedUrl, 404, "failed");
    NotificationChecker checker = new NotificationChecker(client);
    List<SonarQubeNotification> notifications = checker.request(Collections.singletonMap("myproject", timestamp));
    assertThat(notifications).isEmpty();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) SonarLintWsClient(org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient) Test(org.junit.Test)

Example 4 with SonarQubeNotification

use of org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification in project sonarlint-core by SonarSource.

the class NotificationCheckerTest method testFailParsing.

@Test
public void testFailParsing() {
    ZonedDateTime timestamp = ZonedDateTime.of(2017, 06, 04, 20, 0, 0, 0, ZoneOffset.ofHours(0));
    String expectedUrl = "api/developers/search_events?projects=myproject&from=2017-06-04T20%3A00%3A00%2B0000";
    SonarLintWsClient client = WsClientTestUtils.createMockWithResponse(expectedUrl, INVALID_RESPONSE);
    NotificationChecker checker = new NotificationChecker(client);
    List<SonarQubeNotification> notifications = checker.request(Collections.singletonMap("myproject", timestamp));
    assertThat(notifications).isEmpty();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) SonarLintWsClient(org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient) Test(org.junit.Test)

Example 5 with SonarQubeNotification

use of org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification in project sonarlint-core by SonarSource.

the class NotificationTimerTaskTest method testRepeatedProject.

@Test
public void testRepeatedProject() {
    // return one notification for our project
    SonarQubeNotification notif = mock(SonarQubeNotification.class);
    when(notif.projectKey()).thenReturn("myproject");
    when(notificationChecker.request(Collections.singletonMap("myproject", time))).thenReturn(Collections.singletonList(notif));
    // execute with one project
    NotificationConfiguration project = createProject("myproject");
    NotificationConfiguration project2 = createProject("myproject");
    LastNotificationTime notificationTime = mock(LastNotificationTime.class);
    when(notificationTime.get()).thenReturn(ZonedDateTime.now().minusHours(2));
    when(project2.lastNotificationTime()).thenReturn(notificationTime);
    timerTask.setProjects(Collections.singleton(project));
    timerTask.run();
    // verify checker used once and notification was returned through the listener
    verify(notificationCheckerFactory, times(1)).create(any(ServerConfiguration.class));
    // should use the most recent time
    verify(notificationChecker).request(Collections.singletonMap("myproject", time));
    verify(listener).handle(notif);
}
Also used : LastNotificationTime(org.sonarsource.sonarlint.core.client.api.notifications.LastNotificationTime) SonarQubeNotification(org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) NotificationConfiguration(org.sonarsource.sonarlint.core.client.api.common.NotificationConfiguration) Test(org.junit.Test)

Aggregations

SonarQubeNotification (org.sonarsource.sonarlint.core.client.api.notifications.SonarQubeNotification)8 ZonedDateTime (java.time.ZonedDateTime)6 Test (org.junit.Test)6 NotificationConfiguration (org.sonarsource.sonarlint.core.client.api.common.NotificationConfiguration)4 ServerConfiguration (org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration)4 SonarLintWsClient (org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 LastNotificationTime (org.sonarsource.sonarlint.core.client.api.notifications.LastNotificationTime)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 TimerTask (java.util.TimerTask)1 BinaryOperator (java.util.function.BinaryOperator)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1