Skip to content

Commit

Permalink
Merge pull request #313 from stephenwang1011/slacknotify
Browse files Browse the repository at this point in the history
feat 后端服务添加slack机器人
  • Loading branch information
ZhouYixun authored Feb 15, 2023
2 parents 0b2dcb4 + a79936c commit db32d48
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface RobotType {
int YouSpace = 4;
int Telegram = 5;
int LineNotify = 6;
int SlackNotify = 7;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public RobotMessenger getRobotMessenger(int robotType) {
case RobotType.YouSpace -> robotMessenger = context.getBean(YouSpaceImpl.class);
case RobotType.Telegram -> robotMessenger = context.getBean(TelegramImpl.class);
case RobotType.LineNotify -> robotMessenger = context.getBean(LineNotifyImpl.class);
case RobotType.SlackNotify -> robotMessenger = context.getBean(SlackNotifyImpl.class);
default -> throw new SonicException("Unsupported robot type");
}
return robotMessenger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
* sonic-server Sonic Cloud Real Machine Platform.
* Copyright (C) 2022 SonicCloudOrg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.cloud.sonic.controller.tools.robot.vendor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.cloud.sonic.controller.tools.robot.RobotMessenger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.Map;


/**
* @author young(stephenwang1011)
* @des Slack robot消息推送
* @date 2023/2/10
*/
@Slf4j
@Service("SlackNotifyImpl")
public class SlackNotifyImpl implements RobotMessenger {

//从配置文件获取前端部署的host
@Value("${robot.client.host}")
private String clientHost;

/**
* @param restTemplate RestTemplate
* @param token 机器人token
* @param secret 机器人密钥
* @param jsonObject 通知内容
* @author young(stephenwang1011)
* @des SLACK发送测试通知
* @date 2023/2/14
*/
private void signAndSend(RestTemplate restTemplate, String token, String secret, JSONObject jsonObject) {
clientHost = clientHost.replace(":80/", "/");
try {
ResponseEntity<JSONObject> responseEntity =
restTemplate.postForEntity(token, jsonObject, JSONObject.class);
log.info("robot result: " + responseEntity.getBody());
} catch (Exception e) {
log.warn("robot send failed, cause: " + e.getMessage());
}
}

/**
* @param restTemplate RestTemplate
* @param token 机器人token
* @param secret 机器人密钥
* @param suiteName 套件名称
* @param passCount 通过数量
* @param warnCount 警告数量
* @param failCount 失败数量
* @param projectId 项目id
* @param resultId 结果id
* @return void
* @author young(stephenwang1011)
* @des 发送每次测试结果到SLACK
* @date 2023/2/14
*/
@Override
public void sendResultFinishReport(RestTemplate restTemplate, String token, String secret, String suiteName, int passCount, int warnCount, int failCount, int projectId, int resultId) {
JSONObject slackObjs = new JSONObject(true);

String reportLink = clientHost + "/Home/" + projectId + "/ResultDetail/" + resultId;
SlackMessage slackMessage = new SlackMessage();
slackMessage.setType("mrkdwn");
slackMessage.setText("*测试套件:" + suiteName + " 运行完毕* \n" +
"通过数:" + passCount + "\n" +
"异常数:" + warnCount + "\n" +
"失败数:" + failCount + "\n" +
"测试报告:" + reportLink);

this.slackMessage(slackMessage, restTemplate, token, secret, slackObjs, suiteName);
}

/**
* @param restTemplate RestTemplate
* @param token 机器人token
* @param secret 机器人密钥
* @param projectId 项目id
* @param projectName 项目名称
* @param yesterday 昨天的起始时间
* @param today 今天的起始时间
* @return void
* @author young(stephenwang1011)
* @des 通过slack发送day report
* @date 2022/12/20
*/
@Override
public void sendDayReportMessage(RestTemplate restTemplate, String token, String secret, int projectId, String projectName, String yesterday, String today, int passCount, int warnCount, int failCount) {
JSONObject slackObjs = new JSONObject(true);

int total = passCount + warnCount + failCount;
String reportLink = clientHost + "/Home/" + projectId;

SlackMessage slackMessage = new SlackMessage();
slackMessage.setType("mrkdwn");
slackMessage.setText("*Sonic云真机测试平台日报* \n" +
"项目:" + projectName + "\n" +
"时间:" + yesterday + " - " + today + "\n" +
"通过数:" + passCount + "\n" +
"异常数:" + warnCount + "\n" +
"失败数:" + failCount + "\n" +
"测试通过率:" + (total > 0 ?
new BigDecimal(((float) passCount / total) * 100).setScale(2, RoundingMode.HALF_UP).doubleValue() : 0) + "%\n" +
"详细统计:" + reportLink);

this.slackMessage(slackMessage, restTemplate, token, secret, slackObjs, projectName);

}


/**
* @param restTemplate RestTemplate
* @param token 机器人token
* @param secret 机器人密钥
* @param errorType errorType
* @param tem 温度
* @param udId 设备Id
* @return void
* @author young(stephenwang1011)
* @des 向slack发送设备错误讯息
* @date 2022/12/20
*/
@Override
public void sendErrorDevice(RestTemplate restTemplate, String token, String secret, int errorType, int tem, String udId) {

JSONObject slackObjs = new JSONObject(true);
SlackMessage slackMessage = new SlackMessage();
slackMessage.setType("mrkdwn");

if (errorType == 1) {
slackMessage.setText("*Sonic设备高温预警* \n" +
"设备序列号:" + udId + "\n" +
"电池温度:" + (tem / 10) + "℃");

//这个是slack消息的通知title
slackObjs.put("text", "Sonic设备:" + udId + "温度过高!");
} else {
slackMessage.setText("*Sonic设备高温超时,已关机* \n" +
"设备序列号:" + udId + "\n" +
"电池温度:" + (tem / 10) + "℃");

//这个是slack消息的通知title
slackObjs.put("text", "Sonic设备:" + udId + "高温超时,已关机");
}

this.slackMessage(slackMessage, restTemplate, token, secret, slackObjs, udId);
}

/**
* @param restTemplate RestTemplate
* @param token 机器人token
* @param secret 机器人密钥
* @param projectId 项目id
* @param projectName 项目名称
* @param yesterday 昨天的起始时间
* @param today 今天的起始时间
* @param passCount 通过数量
* @param warnCount 警告数量
* @param failCount 失败数量
* @param count 测试数量
* @return void
* @author young(stephenwang1011)
* @des 发送周报到slack
* @date 2022/12/20
*/
@Override
public void sendWeekReportMessage(RestTemplate restTemplate, String token, String secret, int projectId, String projectName, String yesterday, String today, int passCount, int warnCount, int failCount, int count) {
JSONObject slackObjs = new JSONObject(true);
int total = passCount + warnCount + failCount;
String reportLink = clientHost + "/Home/" + projectId;

SlackMessage slackMessage = new SlackMessage();
slackMessage.setType("mrkdwn");
slackMessage.setText("*Sonic云真机测试平台周报* \n" +
"项目:" + projectName + "\n" +
"时间:" + yesterday + " - " + today + "\n" +
"共测试:" + count + "次" + "\n" +
"通过数:" + passCount + "\n" +
"异常数:" + warnCount + "\n" +
"失败数:" + failCount + "\n" +
"测试通过率:" + (total > 0 ?
new BigDecimal(((float) passCount / total) * 100).setScale(2, RoundingMode.HALF_UP).doubleValue() : 0) + "%\n" +
"详细统计:" + reportLink);

this.slackMessage(slackMessage, restTemplate, token, secret, slackObjs, projectName);
}

public void slackMessage(SlackMessage slackMessage, RestTemplate restTemplate, String token, String secret, JSONObject slackObjs, String name) {

Map<Object, Object> slackMap = new LinkedHashMap<>();
slackMap.put("type", "section");
slackMap.put("text", slackMessage);
JSONArray blocksArray = new JSONArray();
blocksArray.add(slackMap);

//这个是slack消息的通知title
slackObjs.put("text", name + "测试完成,请查看!");
//这个是slack消息的具体内容
slackObjs.put("blocks", blocksArray);

this.signAndSend(restTemplate, token, secret, slackObjs);
}


static class SlackMessage {
private String type;
private String text;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,35 @@ public class RobotMsgToolTest {

private String lineToken = "XXXXX";

private String slackWebhook = "https://hooks.slack.com/services/TAMNTVCAH/B04PURURLEM/XXXXXXXXX";

@Test
public void sendDayReportMessage() {
robotMsgTool.sendDayReportMessage(wechatToken, "", 1, "2",
"1", "2", 1, 2, 3, RobotType.WeChat);
}

@Test
public void sendSlackDayReportMessage() {
robotMsgTool.sendDayReportMessage(slackWebhook, "", 1, "Operator App",
"2023-2-14 14:52:01", "2023-2-14 14:59:21", 40, 0, 0, RobotType.SlackNotify);
}


@Test
public void sendDayReportMessage2() {
robotMsgTool.sendDayReportMessage(wechatToken, "", 1, "2",
"1", "2", 1, 0, 0, RobotType.WeChat);
}

@Test
public void testSendErrorDevice1() {
robotMsgTool.sendErrorDevice(wechatToken, "", RobotType.WeChat, 1, 80, "111");
public void testSendErrorDeviceSlack() {
robotMsgTool.sendErrorDevice(slackWebhook, "", RobotType.SlackNotify, 0, 80, "111");
}

@Test
public void testSendErrorDeviceSlack2() {
robotMsgTool.sendErrorDevice(slackWebhook, "", RobotType.SlackNotify, 1, 80, "111");
}


Expand All @@ -81,12 +95,24 @@ public void sendWeekReportMessage() {
"1", "2", 1, 0, 0, 100, RobotType.WeChat);
}

@Test
public void sendWeekReportMessageSlack() {
robotMsgTool.sendWeekReportMessage(slackWebhook, "", 1, "2",
"1", "2", 1, 0, 0, 100, RobotType.SlackNotify);
}

@Test
public void sendResultFinishReport() {
robotMsgTool.sendResultFinishReport(wechatToken, "", "111", 1, 1, 1, 1,
1, RobotType.WeChat);
}

@Test
public void sendResultFinishReportSlack() {
robotMsgTool.sendResultFinishReport(slackWebhook, "", "suite-01", 1, 1, 1, 1,
1, RobotType.SlackNotify);
}

@Test
public void sendDayReportMessageForTelegram() {
robotMsgTool.sendDayReportMessage(telegramToken, "", 1, "2",
Expand Down

0 comments on commit db32d48

Please sign in to comment.