forked from kubesphere/kubesphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
88 lines (76 loc) · 3.09 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright 2020 KubeSphere Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sonarqube
import (
"github.com/fearlesschenc/kubesphere/pkg/simple/client/devops"
sonargo "github.com/kubesphere/sonargo/sonar"
"k8s.io/klog"
)
type SonarInterface interface {
GetSonarResultsByTaskIds(taskId ...string) ([]*SonarStatus, error)
}
type sonarQube struct {
client *sonargo.Client
}
func NewSonar(client *sonargo.Client) *sonarQube {
return &sonarQube{client: client}
}
const (
SonarAnalysisActionClass = "hudson.plugins.sonar.action.SonarAnalysisAction"
SonarMetricKeys = "alert_status,quality_gate_details,bugs,new_bugs,reliability_rating,new_reliability_rating,vulnerabilities,new_vulnerabilities,security_rating,new_security_rating,code_smells,new_code_smells,sqale_rating,new_maintainability_rating,sqale_index,new_technical_debt,coverage,new_coverage,new_lines_to_cover,tests,duplicated_lines_density,new_duplicated_lines_density,duplicated_blocks,ncloc,ncloc_language_distribution,projects,new_lines"
SonarAdditionalFields = "metrics,periods"
)
type SonarStatus struct {
Measures *sonargo.MeasuresComponentObject `json:"measures,omitempty"`
Issues *sonargo.IssuesSearchObject `json:"issues,omitempty"`
GeneralAction *devops.GeneralAction `json:"generalAction,omitempty"`
Task *sonargo.CeTaskObject `json:"task,omitempty"`
}
func (s *sonarQube) GetSonarResultsByTaskIds(taskIds ...string) ([]*SonarStatus, error) {
sonarStatuses := make([]*SonarStatus, 0)
for _, taskId := range taskIds {
sonarStatus := &SonarStatus{}
taskOptions := &sonargo.CeTaskOption{
Id: taskId,
}
ceTask, _, err := s.client.Ce.Task(taskOptions)
if err != nil {
klog.Errorf("get sonar task error [%+v]", err)
continue
}
sonarStatus.Task = ceTask
measuresComponentOption := &sonargo.MeasuresComponentOption{
Component: ceTask.Task.ComponentKey,
AdditionalFields: SonarAdditionalFields,
MetricKeys: SonarMetricKeys,
}
measures, _, err := s.client.Measures.Component(measuresComponentOption)
if err != nil {
klog.Errorf("get sonar task error [%+v]", err)
continue
}
sonarStatus.Measures = measures
issuesSearchOption := &sonargo.IssuesSearchOption{
AdditionalFields: "_all",
ComponentKeys: ceTask.Task.ComponentKey,
Resolved: "false",
Ps: "10",
S: "FILE_LINE",
Facets: "severities,types",
}
issuesSearch, _, err := s.client.Issues.Search(issuesSearchOption)
sonarStatus.Issues = issuesSearch
sonarStatuses = append(sonarStatuses, sonarStatus)
}
return sonarStatuses, nil
}