Skip to content
Permalink
Browse files
IGNITE-16829 Support configurable mail smtp host, disabling auth and …
…SSL (#190)
  • Loading branch information
dspavlov committed Apr 15, 2022
1 parent c4c1839 commit 95c25a87e5f0a7987f282352e1a5cf2a381c040f
Showing 12 changed files with 244 additions and 145 deletions.
@@ -95,13 +95,22 @@
}
],
"notifications": {
/* Email sending settings, only smtp.gmail.com is now supported. */
/* Email sending settings */
"email": {
/* Username, equal to from: field */
/* Username, and 'from' field for emails. */
"username": "ignitetcbot@gmail.com",
"auth": true,
/** Email password encoded using Password Encoder. */
"pwd": ""
//todo ^ specify password
"pwd": "",
//todo ^ specify password or set auth to false
"smtp": {
/** Host or IP */
"host": "127.0.0.1",
/** Default is 25 for non-ssl, 465 for ssl enabled */
"port": 465,
/** SSL enabled, default true */
"ssl": true
}
},
"channels": [
{
@@ -102,11 +102,7 @@ protected TcBotJsonConfig getConfig() {

/** {@inheritDoc} */
@Override public NotificationsConfig notifications() {
NotificationsConfig notifications = getConfig().notifications();
if (notifications != null && !notifications.isEmpty())
return notifications;

return NotificationsConfig.backwardConfig();
return getConfig().notifications();
}

/** {@inheritDoc} */
@@ -299,7 +299,7 @@ protected String sendNewNotificationsEx() {
String builds = next.buildIdToIssue.keySet().toString();
String subj = "[MTCGA]: " + next.countIssues() + " new failures in builds " + builds + " needs to be handled";

emailSender.sendEmail(addr, subj, next.toHtml(), next.toPlainText(), notifications);
emailSender.sendEmail(addr, subj, next.toHtml(), next.toPlainText(), notifications.email());

sndStat.computeIfAbsent(addr, k -> new AtomicInteger()).incrementAndGet();
}
@@ -28,7 +28,7 @@
public static final String GITHUB_REF = "https://github.com/apache/ignite-teamcity-bot";

/** TC Bot Version. */
public static final String VERSION = "20211207";
public static final String VERSION = "20220414";

/** Java version, where Web App is running. */
public String javaVer;
@@ -27,6 +27,8 @@
import org.apache.ignite.tcbot.engine.conf.INotificationChannel;
import org.apache.ignite.tcbot.engine.conf.ITcBotConfig;
import org.apache.ignite.tcbot.engine.conf.NotificationsConfig;
import org.apache.ignite.tcbot.notify.IEmailSender;
import org.apache.ignite.tcbot.notify.ISendEmailConfig;
import org.apache.ignite.tcbot.notify.ISlackSender;

import javax.annotation.security.PermitAll;
@@ -37,7 +39,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
@@ -120,13 +121,35 @@ public SimpleResult testSlackNotification() {
slackSender.sendMessage(channel.slack(), "Test Slack notification message!", notifications);
}
}
catch (IOException e) {
catch (Exception e) {
return new SimpleResult("Failed to send test Slack message: " + e.getMessage());
}

return new SimpleResult("Ok");
}

@POST
@PermitAll
@Path("testEmailNotification")
public SimpleResult testEmailNotification(String address) {
IEmailSender emailSender = CtxListener.getInjector(ctx).getInstance(IEmailSender.class);

ITcBotConfig tcBotConfig = CtxListener.getInjector(ctx).getInstance(ITcBotConfig.class);

try {
NotificationsConfig notifications = tcBotConfig.notifications();
String subj = "[MTCGA]: test email notification";

ISendEmailConfig email = notifications.email();
String plainText = "Test Email notification message!";
emailSender.sendEmail(address, subj, plainText, plainText, email);
} catch (Exception e) {
return new SimpleResult("Failed to send test Email message: " + e.getMessage());
}

return new SimpleResult("Ok");
}


@GET
@PermitAll
@@ -157,6 +157,24 @@
alert(JSON.stringify(result));
}
});

return false;
}

function testEmailNotification() {
$.ajax({
url: "rest/monitoring/testEmailNotification",
method: "post",
data: $("#emailForm").serialize(),
success: function(result) {
alert(JSON.stringify(result));
},
error: function(result) {
alert(JSON.stringify(result));
}
});

return false;
}

</script>
@@ -178,6 +196,14 @@

<hr>
<b>Test Slack notification:</b> <button onclick="testSlackNotification()">Send</button>
<b>Test Email notification:</b>

<form id="emailForm"><b>Email Address:</b>
<input type="text" placeholder="Enter Address" name="address" required>
</form>

<button onclick="testEmailNotification()">Send</button>

<br>

<div id="loadStatus"></div>
@@ -16,13 +16,29 @@
*/
package org.apache.ignite.tcbot.engine.conf;

public class EmailSettings {
import com.google.common.base.Preconditions;
import org.apache.ignite.tcbot.common.conf.PasswordEncoder;
import org.apache.ignite.tcbot.notify.ISendEmailConfig;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Strings.isNullOrEmpty;

@SuppressWarnings("unused")
public class EmailSettings implements ISendEmailConfig {
/** Email to send notifications from. */
private String username;

/** Email password, encoded using Password Encoder. */
private String pwd;

/** Custom smtp server, default is gmail */
private SmtpSettings smtp;

/** Default is true. */
private Boolean auth;

/**
* @return Email to send notifications from.
*/
@@ -50,4 +66,63 @@ public String password() {
public void password(String pwd) {
this.pwd = pwd;
}

@Nullable
public SmtpSettings smtp() {
return smtp;
}

@Nullable public Boolean isAuthRequired() {
return auth;
}

@Nullable
@Override
public Boolean isSmtpSsl() {
SmtpSettings smtp = smtp();

return smtp != null ? smtp.ssl() : null;
}

@Nullable
@Override
public Integer smtpPort() {
SmtpSettings smtp = smtp();

return smtp != null ? smtp.port() : null;
}

/**
*
*/
@Nonnull
@Override public String usernameMandatory() {
String username = username();

Preconditions.checkState(!isNullOrEmpty(username),
"notifications/email/username property should be filled in branches.json");

return username;
}

/**
* @return Email password.
*/
@Nonnull
@Override public String passwordClearMandatory() {
Preconditions.checkState(!isNullOrEmpty(password()),
"notifications/email/pwd property should be filled in branches.json");

return PasswordEncoder.decode(password());
}

@Nullable
@Override
public String smtpHost() {
SmtpSettings smtp = smtp();

return smtp != null ? smtp.host() : null;
}


}
@@ -26,7 +26,7 @@
*
*/
public class NotificationChannel implements INotificationChannel {
/** (Destionation) Email. */
/** (Destination) Email. */
private String email;

/** Slack. */
@@ -74,14 +74,6 @@ public class NotificationChannel implements INotificationChannel {
return (tagsFilter != null) && !tagsFilter.isEmpty();
}

public void slack(String slack) {
this.slack = slack;
}

public void subscribe(String trackedBranchName) {
this.subscribed.add(trackedBranchName);
}

/** {@inheritDoc} */
@Override public String toString() {
return MoreObjects.toStringHelper(this)

0 comments on commit 95c25a8

Please sign in to comment.