Skip to content
Permalink
Browse files
IGNITE-11779 Support configurable notifications for non-master branch…
…es (#121)
  • Loading branch information
dspavlov committed Apr 19, 2019
1 parent 3642672 commit 6a80994aded6a08aa47fb7a114f13a7029042139
Showing 18 changed files with 437 additions and 68 deletions.
@@ -42,6 +42,24 @@
"apiUrl": "https://api.github.com/repos/apache/ignite/"
}
],
"notifications": {
/* Email sending settings, only smtp.gmail.com is now supported. */
"email": {
/* Username, equal to from: field */
"username": "ignitetcbot@gmail.com",
/** Email password encoded using Password Encoder. */
//todo specify password
"pwd": ""
},
"channels": [
{
email: "dev@ignite.apache.org",
subscribed: [
"master"
]
}
]
},
"branches": [
{
"id": "master",
@@ -66,6 +66,7 @@ public class HelperConfig {

/** Slack authorization token property name. */
public static final String SLACK_AUTH_TOKEN = "slack.auth_token";
@Deprecated
public static final String SLACK_CHANNEL = "slack.channel";
public static final String LOGS = "logs";
public static final String ENDL = String.format("%n");
@@ -133,6 +134,7 @@ public static File resolveWorkDir() {
return Base64Util.encodeUtf8String(user + ":" + pwd);
}

@Deprecated
public static String getMandatoryProperty(Properties props, String key, String cfgName) {
final String user = props.getProperty(key);
Preconditions.checkState(!isNullOrEmpty(user), key + " property should be filled in " + cfgName);
@@ -95,15 +95,23 @@ public static void main0(String[] args) {
Preconditions.checkState(decode.equals(pass));
}

public static void main1(String[] args) {
String pass = "mmm";
public static void mainEncodeEmailPassword(String[] args) {
String pass = "Enter Password Here";
String encode = encode(pass);
System.err.println("Encoded: " + HelperConfig.ENCODED_PASSWORD + "=" + encode);
System.err.println("\"notifications\": {\n" +
" \"email\": {\n" +
" \"pwd\": \"" + encode+ "\",\n" +
" }\n" +
" } ");
String decode = decode(encode);
Preconditions.checkState(decode.equals(pass));
}

public static void main(String[] args) {
mainEncodeEmailPassword(args);
}

public static void main1(String[] args) {
encodeJiraTok("ignitetcbot", "21313");
}

@@ -24,6 +24,7 @@
import java.util.Set;
import java.util.TreeSet;
import org.apache.ignite.ci.db.Persisted;
import org.apache.ignite.ci.util.TimeUtil;
import org.jetbrains.annotations.Nullable;

/**
@@ -152,16 +153,21 @@ public String getDisplayName() {
return displayName;
}

/** {@inheritDoc} */
@Override public String toString() {
String tsStart = buildStartTs == null ? null : TimeUtil.timestampToDateTimePrintable(buildStartTs);
String tsDetect = detectedTs == null ? null : TimeUtil.timestampToDateTimePrintable(detectedTs);
return MoreObjects.toStringHelper(this)
.add("type", type)
.add("displayType", displayType)
.add("trackedBranchName", trackedBranchName)
.add("issueKey", issueKey)
.add("changes", changes)
.add("addressNotified", addressNotified)
.add("webUrl", webUrl)
.add("displayName", displayName)
.add("detectedTs", detectedTs)
.add("buildStartTs", tsStart)
.add("detectedTs", tsDetect)
.toString();
}

@@ -29,31 +29,20 @@
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.ignite.ci.HelperConfig;
import org.apache.ignite.ci.conf.PasswordEncoder;
import org.apache.ignite.ci.tcbot.conf.NotificationsConfig;

/**
* Class for sending email with configured credentials.
*/
public class EmailSender {
public static void main(String[] args) {
public static boolean sendEmail(String to, String subject, String html, String plainText,
NotificationsConfig notifications) {

// Recipient's email ID needs to be mentioned.
String to = "dpavlov.spb@gmail.com";
String user = notifications.emailUsernameMandatory();

String html = "<p>This is actual message</p>";
String from = user;

String subject = "This is the Subject Line!";

sendEmail(to, subject, html, "This is actual message.");
}

public static void sendEmail(String to, String subject, String html, String plainText) {
Properties cfgProps = HelperConfig.loadEmailSettings();
String from = HelperConfig.getMandatoryProperty(cfgProps, HelperConfig.USERNAME, HelperConfig.MAIL_PROPS);
String enc = HelperConfig.getMandatoryProperty(cfgProps, HelperConfig.ENCODED_PASSWORD, HelperConfig.MAIL_PROPS);

String pwd = PasswordEncoder.decode(enc);
final String pwd = notifications.emailPasswordClearMandatory();

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
@@ -62,16 +51,13 @@ public static void sendEmail(String to, String subject, String html, String plai
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

// Sender's email ID needs to be mentioned
// Setup mail getOrCreateCreds
// Get the default Session object.

Session ses = Session.getInstance(props,
new Authenticator() {
@Override protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pwd);
return new PasswordAuthentication(user, pwd);
}
});

try {
// Create a default MimeMessage object.
MimeMessage msg = new MimeMessage(ses);
@@ -102,9 +88,12 @@ public static void sendEmail(String to, String subject, String html, String plai
Transport.send(msg);

System.out.println("Sent message successfully to [" + to + "]...");

return true;
}
catch (MessagingException mex) {
mex.printStackTrace();
}
return false;
}
}
@@ -17,23 +17,27 @@

package org.apache.ignite.ci.mail;

import com.google.common.base.Preconditions;
import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackMessageHandle;
import com.ullink.slack.simpleslackapi.SlackSession;
import com.ullink.slack.simpleslackapi.SlackUser;
import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;
import java.io.IOException;
import java.util.Properties;
import org.apache.ignite.ci.HelperConfig;
import org.apache.ignite.ci.tcbot.conf.NotificationsConfig;

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

/**
*
*/
public class SlackSender {
public static boolean sendMessage(String addr, String msg) throws IOException {
Properties cfgProps = HelperConfig.loadEmailSettings();
String authTok = HelperConfig.getMandatoryProperty(cfgProps, HelperConfig.SLACK_AUTH_TOKEN, HelperConfig.MAIL_PROPS);
public static boolean sendMessage(String addr, String msg,
NotificationsConfig cfg) throws IOException {
String authTok = cfg.slackAuthToken();
Preconditions.checkState(!isNullOrEmpty(authTok), "notifications:\"{}\" property should be filled in branches.json");

SlackSession ses = SlackSessionFactory.createWebSocketSlackSession(authTok);

@@ -29,6 +29,7 @@
import org.apache.ignite.ci.conf.ChainAtServer;

/**
* TC Bot main JSON config file, Historically.
* Config file for tracked branches.
*/
public class BranchesTracked {
@@ -47,6 +48,9 @@ public class BranchesTracked {
/** JIRA config to be used . */
private List<GitHubConfig> gitHubConfigs = new ArrayList<>();

/** Notifications settings & tokens. */
private NotificationsConfig notifications = new NotificationsConfig();

/**
* @return list of internal identifiers of branch.
*/
@@ -111,4 +115,9 @@ Optional<JiraServerConfig> getJiraConfig(String code) {
public Optional<GitHubConfig> getGitHubConfig(String code) {
return gitHubConfigs.stream().filter(s -> code.equals(s.code())).findAny();
}

@Nullable
public NotificationsConfig notifications() {
return notifications;
}
}
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.ci.tcbot.conf;

public class EmailSettings {
/** Email to send notifications from. */
private String username;

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

/**
* @return Email to send notifications from.
*/
public String username() {
return username;
}

/**
* @param username New email to send notifications from.
*/
public void username(String username) {
this.username = username;
}

/**
* @return Email password, encoded using Password Encoder.
*/
public String password() {
return pwd;
}

/**
* @param pwd New email password, encoded using Password Encoder.
*/
public void password(String pwd) {
this.pwd = pwd;
}
}
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.ci.tcbot.conf;

import javax.annotation.Nullable;

/**
*
*/
public interface INotificationChannel {
public boolean isSubscribed(String trackedBranchId);

public boolean isServerAllowed(String srvCode);

@Nullable
public String email();

@Nullable
public String slack();
}
@@ -24,7 +24,7 @@
*/
public interface ITcBotConfig {
/** Default server code. */
String DEFAULT_SERVER_CODE = "apache";
public String DEFAULT_SERVER_CODE = "apache";

/** */
public String primaryServerCode();
@@ -53,4 +53,9 @@ public default Collection<String> getServerIds() {
public IJiraServerConfig getJiraConfig(String srvCode);

public IGitHubConfig getGitConfig(String srvCode);

/**
* @return notification settings config.
*/
public NotificationsConfig notifications();
}
@@ -70,6 +70,15 @@ public class LocalFilesBasedConfig implements ITcBotConfig {
return cfg;
}

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

return NotificationsConfig.backwardConfig();
}

/** {@inheritDoc} */
@Override public String primaryServerCode() {
String srvCode = getTrackedBranches().primaryServerCode();

0 comments on commit 6a80994

Please sign in to comment.