You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by dp...@apache.org on 2022/04/08 16:41:15 UTC
[ignite-teamcity-bot] 01/01: IGNITE-16829 Support configurable mail smtp host
This is an automated email from the ASF dual-hosted git repository.
dpavlov pushed a commit to branch ignite-16829
in repository https://gitbox.apache.org/repos/asf/ignite-teamcity-bot.git
commit 949d1c031f853437407c17ca488b1c4fccc0fb93
Author: Dmitriy Pavlov <dp...@apache.org>
AuthorDate: Fri Apr 8 19:39:45 2022 +0300
IGNITE-16829 Support configurable mail smtp host
---
conf/branches.json | 5 +-
.../ci/tcbot/conf/LocalFilesBasedConfig.java | 6 +-
.../ignite/tcbot/engine/conf/EmailSettings.java | 8 +++
.../tcbot/engine/conf/NotificationChannel.java | 10 +--
.../tcbot/engine/conf/NotificationsConfig.java | 82 ++++------------------
.../ignite/tcbot/engine/conf/SmtpSettings.java | 18 +++++
.../apache/ignite/tcbot/notify/EmailSender.java | 10 +--
.../ignite/tcbot/notify/ISendEmailConfig.java | 4 ++
8 files changed, 54 insertions(+), 89 deletions(-)
diff --git a/conf/branches.json b/conf/branches.json
index a2f7f1bf..3556bc02 100644
--- a/conf/branches.json
+++ b/conf/branches.json
@@ -100,8 +100,11 @@
/* Username, equal to from: field */
"username": "ignitetcbot@gmail.com",
/** Email password encoded using Password Encoder. */
- "pwd": ""
+ "pwd": "",
//todo ^ specify password
+ "smtp": {
+ "host": "127.0.0.1"
+ }
},
"channels": [
{
diff --git a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/tcbot/conf/LocalFilesBasedConfig.java b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/tcbot/conf/LocalFilesBasedConfig.java
index d6ff6ec1..313ce2e0 100644
--- a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/tcbot/conf/LocalFilesBasedConfig.java
+++ b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/tcbot/conf/LocalFilesBasedConfig.java
@@ -102,11 +102,7 @@ public class LocalFilesBasedConfig implements ITcBotConfig {
/** {@inheritDoc} */
@Override public NotificationsConfig notifications() {
- NotificationsConfig notifications = getConfig().notifications();
- if (notifications != null && !notifications.isEmpty())
- return notifications;
-
- return NotificationsConfig.backwardConfig();
+ return getConfig().notifications();
}
/** {@inheritDoc} */
diff --git a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/EmailSettings.java b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/EmailSettings.java
index 0c130f9d..7cf9a8eb 100644
--- a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/EmailSettings.java
+++ b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/EmailSettings.java
@@ -16,6 +16,7 @@
*/
package org.apache.ignite.tcbot.engine.conf;
+@SuppressWarnings("unused")
public class EmailSettings {
/** Email to send notifications from. */
private String username;
@@ -23,6 +24,9 @@ public class EmailSettings {
/** Email password, encoded using Password Encoder. */
private String pwd;
+ /** Custom smtp server, default is gmail */
+ private SmtpSettings smtp;
+
/**
* @return Email to send notifications from.
*/
@@ -50,4 +54,8 @@ public class EmailSettings {
public void password(String pwd) {
this.pwd = pwd;
}
+
+ public SmtpSettings smtp() {
+ return smtp;
+ }
}
diff --git a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationChannel.java b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationChannel.java
index 9f1b57df..3ad0b163 100644
--- a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationChannel.java
+++ b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationChannel.java
@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
*
*/
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)
diff --git a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationsConfig.java b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationsConfig.java
index 3d4da024..544ffca6 100644
--- a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationsConfig.java
+++ b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/NotificationsConfig.java
@@ -19,21 +19,16 @@ package org.apache.ignite.tcbot.engine.conf;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
-import org.apache.ignite.tcbot.common.conf.ITcServerConfig;
import org.apache.ignite.tcbot.common.conf.PasswordEncoder;
-import org.apache.ignite.tcbot.common.conf.TcBotWorkDir;
+import org.apache.ignite.tcbot.notify.ISendEmailConfig;
+import org.apache.ignite.tcbot.notify.ISlackBotConfig;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import java.util.Properties;
-import javax.annotation.Nonnull;
-import org.apache.ignite.tcbot.notify.ISendEmailConfig;
-import org.apache.ignite.tcbot.notify.ISlackBotConfig;
import static com.google.common.base.Strings.isNullOrEmpty;
@@ -48,60 +43,9 @@ public class NotificationsConfig implements ISendEmailConfig, ISlackBotConfig {
private String slackAuthTok;
/** Channels to send notifications to. */
+ @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private List<NotificationChannel> channels = new ArrayList<>();
- private static final String MAIL_PROPS = "mail.auth.properties";
- private static final String USERNAME = "username";
- private static final String ENCODED_PASSWORD = "encoded_password";
- /** Slack authorization token property name. */
- private static final String SLACK_AUTH_TOKEN = "slack.auth_token";
- @Deprecated
- private static final String SLACK_CHANNEL = "slack.channel";
-
- @Nonnull
- public static NotificationsConfig backwardConfig() {
- Properties cfgProps = loadEmailSettings();
-
- NotificationsConfig cfg = new NotificationsConfig();
-
- cfg.slackAuthTok = cfgProps.getProperty(SLACK_AUTH_TOKEN);
-
- cfg.email.username(cfgProps.getProperty(USERNAME));
-
- cfg.email.password(cfgProps.getProperty(ENCODED_PASSWORD));
-
- String slackCh = cfgProps.getProperty(SLACK_CHANNEL);
- if (!Strings.isNullOrEmpty(slackCh)) {
- NotificationChannel ch = new NotificationChannel();
- ch.slack("#" + slackCh);
- ch.subscribe(ITcServerConfig.DEFAULT_TRACKED_BRANCH_NAME);
- cfg.channels.add(ch);
- }
-
- return cfg;
- }
-
- public static Properties loadEmailSettings() {
- try {
- return loadProps(new File(TcBotWorkDir.resolveWorkDir(), MAIL_PROPS));
- }
- catch (IOException e) {
- e.printStackTrace();
- return new Properties();
- }
- }
-
- private static Properties loadProps(File file) throws IOException {
- Properties props = new Properties();
-
- try (FileReader reader = new FileReader(file)) {
- props.load(reader);
- }
-
- return props;
- }
-
-
public boolean isEmpty() {
return (email == null || Strings.isNullOrEmpty(email.username()))
&& (email == null || Strings.isNullOrEmpty(email.password()))
@@ -145,17 +89,15 @@ public class NotificationsConfig implements ISendEmailConfig, ISlackBotConfig {
return PasswordEncoder.decode(email.password());
}
- public Collection<? extends INotificationChannel> channels() {
- if (channels == null)
- return Collections.emptyList();
+ @Nullable
+ @Override
+ public String emailSmtpHost() {
+ SmtpSettings smtp = email.smtp();
- return Collections.unmodifiableList(channels);
+ return smtp != null ? smtp.host() : null;
}
- public void addChannel(NotificationChannel ch) {
- if (channels == null)
- this.channels = new ArrayList<>();
-
- channels.add(ch);
+ public Collection<? extends INotificationChannel> channels() {
+ return channels == null ? Collections.emptyList() : Collections.unmodifiableList(channels);
}
}
diff --git a/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/SmtpSettings.java b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/SmtpSettings.java
new file mode 100644
index 00000000..e507c7ca
--- /dev/null
+++ b/tcbot-engine/src/main/java/org/apache/ignite/tcbot/engine/conf/SmtpSettings.java
@@ -0,0 +1,18 @@
+package org.apache.ignite.tcbot.engine.conf;
+
+import javax.annotation.Nullable;
+
+public class SmtpSettings {
+ /** Default is gmail server: "smtp.gmail.com". */
+ private String host;
+
+ /** Default is 465. */
+ private Integer socketFactoryPort;
+
+ /** Default is 465. */
+ private Integer port;
+
+ @Nullable public String host() {
+ return host;
+ }
+}
diff --git a/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/EmailSender.java b/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/EmailSender.java
index 87933d63..68820ce5 100644
--- a/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/EmailSender.java
+++ b/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/EmailSender.java
@@ -30,6 +30,8 @@ import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import static com.google.common.base.Strings.isNullOrEmpty;
+
/**
* Class for sending email with configured credentials.
*/
@@ -40,12 +42,12 @@ class EmailSender implements IEmailSender {
String user = notifications.emailUsernameMandatory();
- String from = user;
+ String pwd = notifications.emailPasswordClearMandatory();
- final String pwd = notifications.emailPasswordClearMandatory();
+ String smtpHost = notifications.emailSmtpHost();
Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
+ props.put("mail.smtp.host", isNullOrEmpty(smtpHost) ? "smtp.gmail.com" : smtpHost);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
@@ -62,7 +64,7 @@ class EmailSender implements IEmailSender {
MimeMessage msg = new MimeMessage(ses);
// Set From: header field of the header.
- msg.setFrom(new InternetAddress(from));
+ msg.setFrom(new InternetAddress(user));
// Set To: header field of the header.
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
diff --git a/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/ISendEmailConfig.java b/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/ISendEmailConfig.java
index 307d042a..d7600ec0 100644
--- a/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/ISendEmailConfig.java
+++ b/tcbot-notify/src/main/java/org/apache/ignite/tcbot/notify/ISendEmailConfig.java
@@ -18,6 +18,7 @@
package org.apache.ignite.tcbot.notify;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
/**
* Source email configuration. Now only gmail server is supported.
@@ -34,4 +35,7 @@ public interface ISendEmailConfig {
*/
@Nonnull
public String emailPasswordClearMandatory() ;
+
+ @Nullable
+ String emailSmtpHost();
}