Skip to content

Commit

Permalink
ping a user or group in slack channel (#1227)
Browse files Browse the repository at this point in the history
* ping a user or group in slack channel

* fix a typo

* fix a typo

* fix a typo
  • Loading branch information
liyaqin1 authored Aug 3, 2023
1 parent 6a9b5b9 commit 19f0f8c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
31 changes: 22 additions & 9 deletions deploy-board/deploy_board/templates/configs/env_config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,17 @@
type="text" value="{{ env.chatroom |default_if_none:'' }}"
maxlength="128"/>
</div>
<label for="notify" class="deployToolTip control-label col-xs-2"

<label for="mentionRecipients" class="deployToolTip control-label col-xs-2"
data-toggle="tooltip"
title="Whether or not to notify commiters when deploy">
Notify Authors
title="Ping slack user or slack user group in the chat rooms (slack channels), such as user1,userGroup2">
Ping User or User Group
</label>

<div class="col-xs-4">
{% if env.notifyAuthors %}
<input class="" id="notify_author" name="notify_author" type="checkbox" value="" checked>
{% else %}
<input class="" id="notify_author" name="notify_author" type="checkbox" value="">
{% endif %}
</div>
<input class="form-control" name="mention_recipients" required="false"
type="text" value="{{ env.mentionRecipients |default_if_none:'' }}"/>
</div>
</div>
<div class="form-group">
<label for="chatRecipients" class="deployToolTip control-label col-xs-2"
Expand All @@ -116,6 +114,21 @@
<input class="form-control" name="watch_recipients" required="false"
type="text" value="{{ env.watchRecipients |default_if_none:'' }}"/>
</div>
</div>
<div>
<label for="notify" class="deployToolTip control-label col-xs-2"
data-toggle="tooltip"
title="Whether or not to notify commiters when deploy">
Notify Authors
</label>

<div class="col-xs-4">
{% if env.notifyAuthors %}
<input class="" id="notify_author" name="notify_author" type="checkbox" value="" checked>
{% else %}
<input class="" id="notify_author" name="notify_author" type="checkbox" value="">
{% endif %}
</div>

<label for="emailRecipients" class="deployToolTip control-label col-xs-2"
data-toggle="tooltip"
Expand Down
1 change: 1 addition & 0 deletions deploy-board/deploy_board/webapp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def clone_from_stage_name(request, env_name, stage_name, from_env_name, from_sta
new_data['emailRecipients'] = from_stage['emailRecipients']
new_data['notifyAuthors'] = from_stage['notifyAuthors']
new_data['watchRecipients'] = from_stage['watchRecipients']
new_data['mentionRecipients'] = from_stage['mentionRecipients']
new_data['maxDeployNum'] = from_stage['maxDeployNum']
new_data['maxDeployDay'] = from_stage['maxDeployDay']
new_data['overridePolicy'] = from_stage['overridePolicy']
Expand Down
1 change: 1 addition & 0 deletions deploy-board/deploy_board/webapp/env_config_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def post(self, request, name, stage):
data["branch"] = query_dict["branch"]
data["emailRecipients"] = query_dict["email_recipients"]
data["watchRecipients"] = query_dict["watch_recipients"]
data["mentionRecipients"] = query_dict["mention_recipients"]
data["maxDeployNum"] = int(query_dict["maxDeployNum"])
data["maxDeployDay"] = int(query_dict["maxDeployDay"])
data["maxParallelRp"] = int(query_dict["maxParallelRp"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public class EnvironBean implements Updatable, Serializable {
@JsonProperty("watchRecipients")
private String watch_recipients;

@JsonProperty("mentionRecipients")
private String mention_recipients;

@JsonProperty("metricsConfigId")
private String metrics_config_id;

Expand Down Expand Up @@ -420,6 +423,14 @@ public void setWatch_recipients(String watch_recipients) {
this.watch_recipients = watch_recipients;
}

public String getMention_recipients() {
return mention_recipients;
}

public void setMention_recipients(String mention_recipients) {
this.mention_recipients = mention_recipients;
}

public String getMetrics_config_id() {
return metrics_config_id;
}
Expand Down Expand Up @@ -590,6 +601,7 @@ public SetClause genSetClause() {
clause.addColumn("email_recipients", email_recipients);
clause.addColumn("notify_authors", notify_authors);
clause.addColumn("watch_recipients", watch_recipients);
clause.addColumn("mention_recipients", mention_recipients);
clause.addColumn("metrics_config_id", metrics_config_id);
clause.addColumn("alarm_config_id", alarm_config_id);
clause.addColumn("webhooks_config_id", webhooks_config_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.slack.api.methods.SlackApiException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class SlackChatManager implements ChatManager {
private static final Logger LOG = LoggerFactory.getLogger(SlackChatManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Void call() {

if (!StringUtils.isEmpty(chatrooms)) {
LOG.info(String.format("Send message to %s", chatrooms));
commonHandler.sendChatMessage(Constants.SYSTEM_OPERATOR, chatrooms, message, "yellow");
commonHandler.sendChatMessage(Constants.SYSTEM_OPERATOR, chatrooms, message, "yellow", "");
}
} catch (Throwable t) {
LOG.error(String.format("%s: Failed to send notifications.", subject), t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void sendMessage() {
}

sendWatcherMessage(operator, envBean.getWatch_recipients(), message, color);
sendChatMessage(operator, envBean.getChatroom(), message, color);
sendChatMessage(operator, envBean.getChatroom(), message, color, envBean.getMention_recipients());

if (state == DeployState.FAILING) {
String recipients = envBean.getEmail_recipients();
Expand Down Expand Up @@ -196,14 +196,20 @@ String generateMessage(String buildId, EnvironBean envBean, DeployState state, D
}
}

public void sendChatMessage(String from, String rooms, String message, String color) {
public void sendChatMessage(String from, String rooms, String message, String color, String recipients) {
if (StringUtils.isEmpty(rooms)) {
return;
}
List<String> chatrooms = Arrays.asList(rooms.split(","));

for (String chatroom : chatrooms) {
try {
if (!StringUtils.isEmpty(recipients)) {
List<String> targets = Arrays.asList(recipients.split(","));
for (String target : targets) {
message = "<@" + target + "> " + message;
}
}
chatManager.send(from, chatroom.trim(), message, color);
} catch (Exception e) {
LOG.error(String.format("Failed to send message '%s' to chatroom %s", message, chatroom), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ void sendStartDeployMessage(String additionalMessage) {
buildBean.getScm_commit_7(),
WebLink);

commonHandler.sendChatMessage(operator, envBean.getChatroom(), message, "yellow");
commonHandler.sendChatMessage(operator, envBean.getChatroom(), message, "yellow", envBean.getMention_recipients());
if (StringUtils.isNotEmpty(additionalMessage)) {
LOG.debug(String.format("Sending additional message: %s to chat", additionalMessage));
commonHandler.sendChatMessage(operator, envBean.getChatroom(), additionalMessage, "yellow");
commonHandler.sendChatMessage(operator, envBean.getChatroom(), additionalMessage, "yellow", envBean.getMention_recipients());
}
} catch (Exception e) {
LOG.error("Failed to send start notification!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,21 @@ void transition(HotfixBean hotBean) throws Exception {
// Send chat message
List<EnvironBean> environBeans = environDAO.getByName(hotBean.getEnv_name());
Set<String> chatroomSet = new HashSet<String>();
Set<String> mentionRecipientSet = new HashSet<String>();
for (EnvironBean environBean : environBeans) {
chatroomSet.add(environBean.getChatroom());
mentionRecipientSet.add(environBean.getMention_recipients());
}
String chatrooms = StringUtils.join(chatroomSet, ",");
String mentionRecipients = StringUtils.join(mentionRecipientSet, ",");
DeployBean deployBean = deployDAO.getById(hotBean.getBase_deploy());
BuildBean buildBean = buildDAO.getById(deployBean.getBuild_id());
String commit = buildBean.getScm_commit();
String name = hotBean.getOperator();
String branch = "hotfix_" + name;
String message = name + " just created a hotfix in " + branch + " branch, and including commit(s) " +
hotBean.getCommits() + " on top of commit " + commit;
commonHandler.sendChatMessage(Constants.SYSTEM_OPERATOR, chatrooms, message, "yellow");
commonHandler.sendChatMessage(Constants.SYSTEM_OPERATOR, chatrooms, message, "yellow", mentionRecipients);

LOG.info("Hotfix Id {} is finished and now in the SUCCEEDED state.", hotfixId);
} else {
Expand Down

0 comments on commit 19f0f8c

Please sign in to comment.