Search in sources :

Example 1 with EmailMessage

use of org.sonar.plugins.emailnotifications.api.EmailMessage in project sonarqube by SonarSource.

the class IssueChangesEmailTemplate method format.

@Override
public EmailMessage format(Notification notif) {
    if (!IssueChangeNotification.TYPE.equals(notif.getType())) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    appendHeader(notif, sb);
    sb.append(NEW_LINE);
    appendChanges(notif, sb);
    sb.append(NEW_LINE);
    appendFooter(sb, notif);
    String projectName = notif.getFieldValue("projectName");
    String issueKey = notif.getFieldValue("key");
    String author = notif.getFieldValue("changeAuthor");
    EmailMessage message = new EmailMessage().setMessageId("issue-changes/" + issueKey).setSubject(projectName + ", change on issue #" + issueKey).setMessage(sb.toString());
    if (author != null) {
        message.setFrom(getUserFullName(author));
    }
    return message;
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage)

Example 2 with EmailMessage

use of org.sonar.plugins.emailnotifications.api.EmailMessage in project sonarqube by SonarSource.

the class AlertsEmailTemplate method format.

@Override
public EmailMessage format(Notification notification) {
    if (!"alerts".equals(notification.getType())) {
        return null;
    }
    // Retrieve useful values
    String projectId = notification.getFieldValue("projectId");
    String projectKey = notification.getFieldValue("projectKey");
    String projectName = notification.getFieldValue("projectName");
    String alertName = notification.getFieldValue("alertName");
    String alertText = notification.getFieldValue("alertText");
    String alertLevel = notification.getFieldValue("alertLevel");
    boolean isNewAlert = Boolean.parseBoolean(notification.getFieldValue("isNewAlert"));
    // Generate text
    String subject = generateSubject(projectName, alertLevel, isNewAlert);
    String messageBody = generateMessageBody(projectName, projectKey, alertName, alertText, isNewAlert);
    // And finally return the email that will be sent
    return new EmailMessage().setMessageId("alerts/" + projectId).setSubject(subject).setMessage(messageBody);
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage)

Example 3 with EmailMessage

use of org.sonar.plugins.emailnotifications.api.EmailMessage in project sonarqube by SonarSource.

the class EmailNotificationChannel method deliver.

@Override
public void deliver(Notification notification, String username) {
    User user = userFinder.findByLogin(username);
    if (user == null || StringUtils.isBlank(user.email())) {
        LOG.debug("User does not exist or has no email: {}", username);
        return;
    }
    EmailMessage emailMessage = format(notification);
    if (emailMessage != null) {
        emailMessage.setTo(user.email());
        deliver(emailMessage);
    }
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) User(org.sonar.api.user.User)

Example 4 with EmailMessage

use of org.sonar.plugins.emailnotifications.api.EmailMessage in project sonarqube by SonarSource.

the class EmailNotificationChannel method sendTestEmail.

/**
   * Send test email.
   *
   * @throws EmailException when unable to send
   */
public void sendTestEmail(String toAddress, String subject, String message) throws EmailException {
    try {
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.setTo(toAddress);
        emailMessage.setSubject(subject);
        emailMessage.setMessage(message);
        send(emailMessage);
    } catch (EmailException e) {
        LOG.debug("Fail to send test email to: " + toAddress, e);
        throw e;
    }
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) EmailException(org.apache.commons.mail.EmailException)

Example 5 with EmailMessage

use of org.sonar.plugins.emailnotifications.api.EmailMessage in project sonarqube by SonarSource.

the class NewIssuesEmailTemplateTest method message_id.

@Test
public void message_id() {
    Notification notification = newNotification();
    EmailMessage message = template.format(notification);
    assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Aggregations

EmailMessage (org.sonar.plugins.emailnotifications.api.EmailMessage)34 Test (org.junit.Test)29 Notification (org.sonar.api.notifications.Notification)25 MimeMessage (javax.mail.internet.MimeMessage)2 Matchers.anyString (org.mockito.Matchers.anyString)2 WiserMessage (org.subethamail.wiser.WiserMessage)2 EmailException (org.apache.commons.mail.EmailException)1 User (org.sonar.api.user.User)1