public NotificationChecker create(ServerConfiguration serverConfiguration) { SonarLintWsClient client = new SonarLintWsClient(serverConfiguration); return new NotificationChecker(client); } }
/** * Checks whether a server supports notifications */ public boolean isSupported() { String path = getWsPath(Collections.emptyMap()); WsResponse wsResponse = wsClient.rawGet(path); return wsResponse.isSuccessful(); }
/** * Checks if a server supports notifications */ public boolean isSupported(ServerConfiguration serverConfig) { NotificationChecker checker = checkerFactory.create(serverConfig); return checker.isSupported(); }
/** * Get all notification events for a set of projects after a given timestamp. * Returns an empty list if an error occurred making the request or parsing the response. */ @CheckForNull public List<SonarQubeNotification> request(Map<String, ZonedDateTime> projectTimestamps) { String path = getWsPath(projectTimestamps); WsResponse wsResponse = wsClient.rawGet(path); if (!wsResponse.isSuccessful()) { LOG.debug("Failed to get notifications: {}, {}", wsResponse.code(), wsResponse.content()); return Collections.emptyList(); } return parseResponse(wsResponse.content()); }
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; }
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); } }