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;
}
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);
}
}
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();
}
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();
}
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);
}
Aggregations