/** * Checks if a server supports notifications */ public boolean isSupported(ServerConfiguration serverConfig) { NotificationChecker checker = checkerFactory.create(serverConfig); return checker.isSupported(); }
public NotificationTimerTask() { this(new NotificationCheckerFactory()); }
public static boolean checkNotificationsSupported(String url, @Nullable String organization, String username, String password) { Builder builder = Server.getConfigBuilderNoCredentials(url, organization); if (StringUtils.isNotBlank(username) || StringUtils.isNotBlank(password)) { builder.credentials(username, password); } return SonarQubeNotifications.get().isSupported(builder.build()); }
private void register(SonarLintProjectSettings settings) { unregister(); if (settings.isBindingEnabled()) { SonarQubeServer server; try { server = bindingManager.getSonarQubeServer(); } catch (InvalidBindingException e) { // do nothing return; } if (server.enableNotifications()) { NotificationConfiguration config = createConfiguration(settings, server); SonarQubeNotifications.get().register(config); } } }
public static SonarQubeNotifications get() { synchronized (LOCK) { if (singleton == null) { Timer timer = new Timer("Notifications timer", true); NotificationTimerTask timerTask = new NotificationTimerTask(); singleton = new SonarQubeNotifications(timer, timerTask, new NotificationCheckerFactory()); } return singleton; } }
@Override public void run() { Map<ServerConfiguration, List<NotificationConfiguration>> mapByServer = groupByServer(); for (Map.Entry<ServerConfiguration, List<NotificationConfiguration>> entry : mapByServer.entrySet()) { requestForServer(entry.getKey(), entry.getValue()); } }
/** * 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 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); } }
/** * Register a project to receive notifications about it. */ public void register(NotificationConfiguration configuration) { configuredNotifications.add(configuration); task.setProjects(configuredNotifications); }
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(); }
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; }
@Override public void run(@NotNull ProgressIndicator indicator) { indicator.setText("Connecting to " + server.getHostUrl() + "..."); indicator.setIndeterminate(false); try { ServerConfiguration serverConfiguration = SonarLintUtils.getServerConfiguration(server); indicator.setText("Checking support of notifications"); notificationsSupported = SonarQubeNotifications.get().isSupported(serverConfiguration); WsHelper wsHelper = new WsHelperImpl(); organizations = wsHelper.listUserOrganizations(serverConfiguration, new TaskProgressMonitor(indicator)); } catch (UnsupportedServerException e) { organizations = Collections.emptyList(); } catch (Exception e) { LOGGER.info("Failed to fetch information", e); exception = e; } }
public boolean subscribe(ISonarLintProject project, SonarLintProjectConfiguration config, SonarQubeNotificationListener listener) { Optional<IServer> server = SonarLintCorePlugin.getServersManager().forProject(project, config); if (!server.isPresent() || !server.get().areNotificationsEnabled()) { return false; } LastNotificationTime notificationTime = new ProjectNotificationTime(project); NotificationConfiguration configuration = new NotificationConfiguration(listener, notificationTime, config.getProjectBinding().get().serverId(), ((Server) server.get()).getConfig()); SonarQubeNotifications.get().register(configuration); return true; }
/** * Removes any previously registered projects attached to a listener */ public void remove(SonarQubeNotificationListener listener) { configuredNotifications.removeIf(p -> p.listener().equals(listener)); task.setProjects(configuredNotifications); }