@@ -43,6 +43,7 @@ import (
4343 userclient "github.com/koderover/zadig/v2/pkg/shared/client/user"
4444 "github.com/koderover/zadig/v2/pkg/tool/lark"
4545 "github.com/koderover/zadig/v2/pkg/tool/log"
46+ "github.com/koderover/zadig/v2/pkg/tool/sonar"
4647 "github.com/koderover/zadig/v2/pkg/types"
4748 jobspec "github.com/koderover/zadig/v2/pkg/types/job"
4849 "github.com/koderover/zadig/v2/pkg/types/step"
@@ -107,6 +108,13 @@ var (
107108 "testStatusFailed" : "失败" ,
108109 "testTotal" : "总数" ,
109110
111+ "sonarQualityGateStatus" : "质量检查" ,
112+ "sonarNcloc" : "行数" ,
113+ "sonarBugs" : "Bugs" ,
114+ "sonarVulnerabilities" : "代码漏洞" ,
115+ "sonarCodeSmells" : "容易出错" ,
116+ "sonarCoverage" : "覆盖率" ,
117+
110118 "notificationTextWorkflow" : "工作流" ,
111119 "notificationTextWaitingForApproval" : "等待审批" ,
112120 "notificationTextExecutor" : "执行用户" ,
@@ -121,6 +129,7 @@ var (
121129 "notificationTextRepositoryInfo" : "代码信息" ,
122130 "notificationTextImageInfo" : "镜像信息" ,
123131 "notificationTextTestResult" : "测试结果" ,
132+ "notificationTextSonarMetrics" : "扫描结果" ,
124133 }
125134
126135 enTextMap = map [string ]string {
@@ -179,6 +188,13 @@ var (
179188 "testStatusFailed" : "Failed" ,
180189 "testTotal" : "Total" ,
181190
191+ "sonarQualityGateStatus" : "Quality Gate Status" ,
192+ "sonarNcloc" : "Ncloc" ,
193+ "sonarBugs" : "Bugs" ,
194+ "sonarVulnerabilities" : "Vulnerabilities" ,
195+ "sonarCodeSmells" : "Code Smells" ,
196+ "sonarCoverage" : "Coverage" ,
197+
182198 "notificationTextWorkflow" : "Workflow" ,
183199 "notificationTextWaitingForApproval" : "waiting for approval" ,
184200 "notificationTextExecutor" : "Executor" ,
@@ -193,6 +209,7 @@ var (
193209 "notificationTextRepositoryInfo" : "Repository Information" ,
194210 "notificationTextImageInfo" : "Image Information" ,
195211 "notificationTextTestResult" : "Test Result" ,
212+ "notificationTextSonarMetrics" : "Scanning Result" ,
196213 }
197214)
198215
@@ -863,8 +880,21 @@ func (w *Service) getNotificationContent(notify *models.NotifyCtl, task *models.
863880 return "" , "" , nil , nil , fmt .Errorf ("genTestResultText err:%s" , err )
864881 }
865882
866- jobTplcontent += fmt .Sprintf ("{{if eq .WebHookType \" dingding\" }}##### {{end}}**{{getText \" notificationTextTestResult\" }}**:\n %s \n " , testResult )
867- mailJobTplcontent += fmt .Sprintf ("{{getText \" notificationTextTestResult\" }}:%s \n " , testResult )
883+ jobTplcontent += fmt .Sprintf ("{{if eq .WebHookType \" dingding\" }}##### {{end}}**{{getText \" notificationTextTestResult\" }}**: %s \n " , testResult )
884+ mailJobTplcontent += fmt .Sprintf ("{{getText \" notificationTextTestResult\" }}: %s \n " , testResult )
885+ case string (config .JobZadigScanning ):
886+ jobSpec := & models.JobTaskFreestyleSpec {}
887+ models .IToi (job .Spec , jobSpec )
888+ sonarMetricsText , mailSonarMetricsText , err := genSonartMetricsText (jobSpec , language )
889+ if err != nil {
890+ log .Errorf ("genTestResultText err:%s" , err )
891+ return "" , "" , nil , nil , fmt .Errorf ("genTestResultText err:%s" , err )
892+ }
893+
894+ if sonarMetricsText != "" {
895+ jobTplcontent += fmt .Sprintf ("{{if eq .WebHookType \" dingding\" }}##### {{end}}**{{getText \" notificationTextSonarMetrics\" }}**: %s \n " , sonarMetricsText )
896+ mailJobTplcontent += fmt .Sprintf ("{{getText \" notificationTextSonarMetrics\" }}: %s \n " , mailSonarMetricsText )
897+ }
868898 }
869899 jobNotifaication := & jobTaskNotification {
870900 Job : job ,
@@ -1230,11 +1260,66 @@ func genTestResultText(workflowName, jobTaskName string, taskID int64, language
12301260 totalNum := report .TestCaseNum
12311261 failedNum := report .FailedCaseNum
12321262 successNum := report .SuccessCaseNum
1233- result += fmt .Sprintf ("%d(%s)%d(%s)%d(%s) \n " , successNum , getText ("testStatusSuccess" , language ), failedNum , getText ("testStatusFailed" , language ), totalNum , getText ("testTotal" , language ))
1263+ result += fmt .Sprintf ("%d(%s) %d(%s) %d(%s)\n " , successNum , getText ("testStatusSuccess" , language ), failedNum , getText ("testStatusFailed" , language ), totalNum , getText ("testTotal" , language ))
12341264 }
12351265 return result , nil
12361266}
12371267
1268+ func genSonartMetricsText (jobSpec * models.JobTaskFreestyleSpec , language string ) (string , string , error ) {
1269+ getQualityGateStatusText := func (qualityGateStatus sonar.QualityGateStatus , language string ) string {
1270+ if language == string (config .SystemLanguageEnUS ) {
1271+ if qualityGateStatus == "" {
1272+ return "NONE"
1273+ } else {
1274+ return string (qualityGateStatus )
1275+ }
1276+ }
1277+
1278+ if qualityGateStatus == "OK" {
1279+ return "通过"
1280+ } else if qualityGateStatus == "WARN" {
1281+ return "警告"
1282+ } else if qualityGateStatus == "ERROR" {
1283+ return "未通过"
1284+ } else if qualityGateStatus == "NONE" || qualityGateStatus == "" {
1285+ return "未开启"
1286+ }
1287+ return ""
1288+ }
1289+
1290+ result := ""
1291+ mailResult := ""
1292+ for _ , jobStep := range jobSpec .Steps {
1293+ if jobStep .StepType == config .StepSonarGetMetrics {
1294+ stepSpec := & step.StepSonarGetMetricsSpec {}
1295+ models .IToi (jobStep .Spec , stepSpec )
1296+
1297+ if stepSpec .SonarMetrics == nil {
1298+ return "" , "" , nil
1299+ }
1300+
1301+ result = fmt .Sprintf ("**%s**(%s) **%s**(%s) **%s**(%s) **%s**(%s) **%s**(%s) **%s%%**(%s)" ,
1302+ getQualityGateStatusText (stepSpec .SonarMetrics .QualityGateStatus , language ), getText ("sonarQualityGateStatus" , language ),
1303+ stepSpec .SonarMetrics .Ncloc , getText ("sonarNcloc" , language ),
1304+ stepSpec .SonarMetrics .Bugs , getText ("sonarBugs" , language ),
1305+ stepSpec .SonarMetrics .Vulnerabilities , getText ("sonarVulnerabilities" , language ),
1306+ stepSpec .SonarMetrics .CodeSmells , getText ("sonarCodeSmells" , language ),
1307+ stepSpec .SonarMetrics .Coverage , getText ("sonarCoverage" , language ),
1308+ )
1309+ mailResult = fmt .Sprintf ("%s(%s) %s(%s) %s(%s) %s(%s) %s(%s) %s%%(%s)" ,
1310+ getQualityGateStatusText (stepSpec .SonarMetrics .QualityGateStatus , language ), getText ("sonarQualityGateStatus" , language ),
1311+ stepSpec .SonarMetrics .Ncloc , getText ("sonarNcloc" , language ),
1312+ stepSpec .SonarMetrics .Bugs , getText ("sonarBugs" , language ),
1313+ stepSpec .SonarMetrics .Vulnerabilities , getText ("sonarVulnerabilities" , language ),
1314+ stepSpec .SonarMetrics .CodeSmells , getText ("sonarCodeSmells" , language ),
1315+ stepSpec .SonarMetrics .Coverage , getText ("sonarCoverage" , language ),
1316+ )
1317+ }
1318+ }
1319+
1320+ return result , mailResult , nil
1321+ }
1322+
12381323func (w * Service ) sendNotification (title , content string , notify * models.NotifyCtl , card * LarkCard , webhookNotify * webhooknotify.WorkflowNotify , taskStatus config.Status ) error {
12391324 link := ""
12401325 if notify .WebHookType == setting .NotifyWebHookTypeDingDing || notify .WebHookType == setting .NotifyWebHookTypeWechatWork || notify .WebHookType == setting .NotifyWebHookTypeMSTeam {
0 commit comments