From c6300f374bbe1ad3f452f56f922c169b1f1e9738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E8=88=AA?= Date: Thu, 23 May 2024 15:26:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=9B=BF=E6=8D=A2=20link=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=93=BE=E6=8E=A5=E4=B8=BAactionCard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) 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, "消息推送成功") From 2c168ba40a474aa17092adfe6299b6d46d5e42f3 Mon Sep 17 00:00:00 2001 From: EthanPeng <78828540@qq.com> Date: Thu, 23 May 2024 15:28:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20README.md=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) +iShot +