Java tutorial
/* * SonarQube Issue Assign Plugin * Copyright (C) 2014 SonarSource * dev@sonar.codehaus.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ package com.github.olivervbk.sonar.devlistnotification.initiatior; import java.util.List; import java.util.NoSuchElementException; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.PostJob; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.notifications.Notification; import org.sonar.api.resources.Project; import org.sonar.server.notification.NotificationManager; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.Comparator; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.Condition; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.ProjectStatus; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.Status; import com.github.olivervbk.sonar.devlistnotification.DevListNotificationPlugin; import com.google.common.annotations.VisibleForTesting; /** * Creates a "my-new-issues" notification for new issues assigned to a developer, and a "my-changed-issues" notificationper assignee for changed issues. */ public class SendNotificationsPostJob extends AbstractWsBlockingPostJob implements PostJob { private static final Logger LOG = LoggerFactory.getLogger(SendNotificationsPostJob.class); public SendNotificationsPostJob(final NotificationManager notificationsManager, final FileSystem fileSystem, final Settings settings) { super(fileSystem, settings); this.notificationManager = notificationsManager; this.settings = settings; } @VisibleForTesting static String conditionsToString(final Status statusToLog, final List<Condition> conditionsList) { final StringBuilder builder = new StringBuilder(); for (final Condition condition : conditionsList) { final Status conditionStatus = condition.getStatus(); if (statusToLog.equals(conditionStatus)) { final String metricName = getMetricName(condition.getMetricKey()); final String actualValue = condition.getActualValue(); final String comparatorSymbol = getComparatorSymbol(condition.getComparator()); final String warningThreshold = condition.getWarningThreshold(); builder.append(String.format("%s: %s %s %s\n", metricName, actualValue, comparatorSymbol, warningThreshold)); } } return builder.toString(); } private static String getComparatorSymbol(final Comparator comparator) { switch (comparator) { case GT: return ">"; case LT: return "<"; case EQ: return "="; case NE: return "!="; default: return comparator.toString(); } } private static String getMetricName(final String metricKey) { try { final Metric metric = CoreMetrics.getMetric(metricKey); return metric.getName(); } catch (final NoSuchElementException e) { // Custom metric -- use key as name } return metricKey; } /** * @return * @see com.github.olivervbk.sonar.devlistnotification.initiatior.AbstractWsBlockingPostJob#getQueryInterval() */ @Override protected int getQueryInterval() { } /** * @return * @see com.github.olivervbk.sonar.devlistnotification.initiatior.AbstractWsBlockingPostJob#getQueryMaxAttempts() */ @Override protected int getQueryMaxAttempts() { } /** * @return * @see com.github.olivervbk.sonar.devlistnotification.initiatior.AbstractWsBlockingPostJob#isSkipEnabled() */ @Override protected boolean isSkipEnabled() { } /** * @param project * @param projectStatus * @see com.github.olivervbk.sonar.devlistnotification.initiatior.AbstractWsBlockingPostJob#onAnalysisFinished(org.sonar.api.resources.Project, org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.ProjectStatus) */ @Override protected void onAnalysisFinished(final Project project, final ProjectStatus projectStatus) { final Status status = projectStatus.getStatus(); if (Status.ERROR.equals(status) || Status.WARN.equals(status)) { sendNotifications(project, projectStatus); } } private void sendNotifications(final Project project, final ProjectStatus projectStatus) { LOG.debug("Generating notifications for {}", project.getName()); final Status status = projectStatus.getStatus(); final List<Condition> conditionsList = projectStatus.getConditionsList(); final String alertName = String.format("Alert on %s: %s", project.getName(), status); final String alertLevel = status.toString(); final StringBuilder alertTextBuilder = new StringBuilder(); if (Status.ERROR.equals(status)) { alertTextBuilder.append("Errors:\n"); alertTextBuilder.append(conditionsToString(Status.ERROR, conditionsList)); alertTextBuilder.append("\n"); } alertTextBuilder.append("Warnings:\n"); alertTextBuilder.append(conditionsToString(Status.WARN, conditionsList)); final String alertText = alertTextBuilder.toString(); final Notification notification = new Notification(DevListNotificationPlugin.NOTIFICATION_TYPE_DEV_LIST); // Retrieve useful values notification.setFieldValue("projectId", String.valueOf(project.getId())); notification.setFieldValue("projectKey", project.getKey()); notification.setFieldValue("projectName", project.getLongName()); notification.setFieldValue("alertName", alertName); notification.setFieldValue("alertText", alertText); notification.setFieldValue("alertLevel", alertLevel); notification.setFieldValue("isNewAlert", String.valueOf(true)); final String toEmail; final String overrideEmail = this.settings.getString(DevListNotificationPlugin.OVERRIDE_MAIL_DEST); if (!StringUtils.isBlank(overrideEmail)) { toEmail = overrideEmail; } else { final String projectMail = this.settings.getString(DevListNotificationPlugin.PROJECT_MAIL_DEST); if (!StringUtils.isBlank(projectMail)) { toEmail = projectMail; } else { final String fallbackMail = this.settings.getString(DevListNotificationPlugin.FALLBACK_MAIL_DEST); if (!StringUtils.isBlank(fallbackMail)) { toEmail = fallbackMail; } else { LOG.warn("No valid email found to use as destination."); return; } } } LOG.debug("Generating notification for email: {}.", toEmail); notification.setFieldValue("userLogin", toEmail); this.notificationManager.scheduleForSending(notification); } private final NotificationManager notificationManager; private final Settings settings; }