Skip to content

Commit

Permalink
Merge pull request #9 from viewhang/main
Browse files Browse the repository at this point in the history
feat: 将钉钉消息类型从link 修改为actionCard
  • Loading branch information
xbmlz authored May 28, 2024
2 parents 62b9567 + 2c168ba commit 506ae92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ xbmlz/sonar-dingtalk-plugin

完成上述步骤,就可以将sonarqube扫描结果,推送到钉钉群了

![](https://cdn.jsdelivr.net/gh/xbmlz/static@main/img/202206181406084.png)
<img width="451" alt="iShot" src="https://github.com/viewhang/sonar-dingtalk-plugin/assets/26957298/a41f7be5-509c-4015-bf88-ef8e681df40b">

31 changes: 17 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"net/url"
)

// dingtalkHandler
Expand Down Expand Up @@ -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, "")
}
Expand Down Expand Up @@ -67,35 +68,37 @@ 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 = "<font color=\"#008000\">正常</font>"
} else {
picUrl = "http://s1.ax1x.com/2020/10/29/BGMZwD.png"
statusText = "<font color=\"#f90202\">异常</font>"
}
// 发送钉钉消息
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 <br/>\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)
dingTalkRsp, _ := http.Post(msgUrl, "application/json", bytes.NewBuffer(paramBytes))
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, "消息推送成功")
Expand Down

0 comments on commit 506ae92

Please sign in to comment.