diff --git a/README.md b/README.md
index 7f4d7ec..cb38cea 100644
--- a/README.md
+++ b/README.md
@@ -72,4 +72,5 @@ xbmlz/sonar-dingtalk-plugin
完成上述步骤,就可以将sonarqube扫描结果,推送到钉钉群了
-![](https://cdn.jsdelivr.net/gh/xbmlz/static@main/img/202206181406084.png)
+
+
diff --git a/main.go b/main.go
index 10a39bc..248517d 100644
--- a/main.go
+++ b/main.go
@@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
+ "net/url"
)
// dingtalkHandler
@@ -35,9 +36,9 @@ func dingtalkHandler(w http.ResponseWriter, r *http.Request) {
},
}
// get measures info
- url := fmt.Sprintf("%s/api/measures/search?projectKeys=%s&metricKeys=alert_status,bugs,reliability_rating,vulnerabilities,security_rating,code_smells,sqale_rating,duplicated_lines_density,coverage,ncloc,ncloc_language_distribution",
+ sonarUrl := fmt.Sprintf("%s/api/measures/search?projectKeys=%s&metricKeys=alert_status,bugs,reliability_rating,vulnerabilities,security_rating,code_smells,sqale_rating,duplicated_lines_density,coverage,ncloc,ncloc_language_distribution",
serverUrl, projectKey)
- req, _ := http.NewRequest("GET", url, nil)
+ req, _ := http.NewRequest("GET", sonarUrl, nil)
if sonarToken != "" {
req.SetBasicAuth(sonarToken, "")
}
@@ -67,27 +68,29 @@ func dingtalkHandler(w http.ResponseWriter, r *http.Request) {
vulnerabilities := (measures[10].(map[string]interface{}))["value"].(string)
// 成功失败标志
- var picUrl string
+ var statusText string
if alertStatus == "OK" {
- picUrl = "http://s1.ax1x.com/2020/10/29/BGMeTe.png"
+ statusText = "正常"
} else {
- picUrl = "http://s1.ax1x.com/2020/10/29/BGMZwD.png"
+ statusText = "异常"
}
// 发送钉钉消息
msgUrl := fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s", accessToken)
messageUrl := fmt.Sprintf("%s/dashboard?id=%s", serverUrl, projectKey)
- link := make(map[string]string)
- link["title"] = fmt.Sprintf("%s[%s]代码扫描报告", projectName, branch)
- link["text"] = fmt.Sprintf("Bugs: %s | 漏洞: %s | 异味: %s\r覆盖率: %s%%\r重复率: %s%%",
- bugs, vulnerabilities, codeSmells, coverage, duplicatedLinesDensity)
- link["messageUrl"] = messageUrl
- link["picUrl"] = picUrl
+ actionCard := make(map[string]string)
+ title := fmt.Sprintf("### %s[%s]代码扫描报告", projectName, branch)
+ actionCard["title"] = title
+ actionCard["text"] = fmt.Sprintf("%s
\n- **状态:** %s \n - **Bugs:** %s \n- **漏洞:** %s \n- **异味:** %s \n- **覆盖率:** %s%% \n- **重复率:** %s%%",
+ title, statusText, bugs, vulnerabilities, codeSmells, coverage, duplicatedLinesDensity)
+ actionCard["btnOrientation"] = "0"
+ actionCard["singleURL"] = fmt.Sprintf("dingtalk://dingtalkclient/page/link?url=%s&&pc_slide=false", url.QueryEscape(messageUrl))
+ actionCard["singleTitle"] = "查看详情"
param := make(map[string]interface{})
- param["msgtype"] = "link"
- param["link"] = link
+ param["msgtype"] = "actionCard"
+ param["actionCard"] = actionCard
// send dingtalk message
paramBytes, _ := json.Marshal(param)
@@ -95,7 +98,7 @@ func dingtalkHandler(w http.ResponseWriter, r *http.Request) {
dingTalkObj := make(map[string]interface{})
json.NewDecoder(dingTalkRsp.Body).Decode(&dingTalkObj)
if dingTalkObj["errcode"].(float64) != 0 {
- fmt.Fprint(w, "消息推送失败,请检查钉钉机器人配置")
+ fmt.Fprint(w, "消息推送失败,请检查钉钉机器人配置,错误信息:"+dingTalkObj["errmsg"].(string))
return
}
fmt.Fprint(w, "消息推送成功")