Skip to content

Commit a79f5b2

Browse files
new matching algorithm
1 parent 9f23195 commit a79f5b2

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This plugin is designed to run SonarQube scans and handle the results and conver
99
- Quality Gate status reporting + Metrics
1010
- Skip Scan and only check for quality Gate Status of a specific analysisId or last analysis
1111

12+
Obs: USe branch and pr_key params for accuracy results matches when skiping the scan
13+
1214
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/sonar-result-v2.png" alt="Results" width="800"/>
1315

1416
### Simple Pipeline example
@@ -109,7 +111,7 @@ spec:
109111
- Example: `"token": "your-sonarqube-token"`
110112
- `ver`: The version of the project.
111113
- Example: `"ver": "1.0.0"`
112-
- `branch`: The branch of the project.
114+
- `branch`: The branch of the project. This parameter is used to specify the branch of your codebase that the results should be matched with. If you're working on multiple branches, it's important to specify the correct branch to ensure that you're looking at the correct set of results.
113115
- Example: `"branch": "master"`
114116
- `timeout`: The timeout for the Sonar scanner.
115117
- Example: `"timeout": "300"`
@@ -157,8 +159,8 @@ spec:
157159
- Example: `"tests": "/path/to/tests"`
158160
- `java_test`: Java Test.
159161
- Example: `"java_test": "/path/to/java/test"`
160-
- `pr_key`: PR Key.
161-
- Example: `"pr_key": "your-pr-key"`
162+
- `pr_key`: Pull Request Key.
163+
- Example: `"pr_key": "123"`
162164
- `pr_branch`: PR Branch.
163165
- Example: `"pr_branch": "your-pr-branch"`
164166
- `pr_base`: PR Base.

plugin.go

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,21 @@ func (p Plugin) Exec() error {
599599
fmt.Printf("#######################################\n")
600600
fmt.Printf("Waiting for quality gate validation...\n")
601601
fmt.Printf("#######################################\n")
602-
statusID, err := getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
602+
603+
// statusID, err := getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
604+
var statusID string
605+
var err error
606+
if p.Config.PRKey != "" {
607+
fmt.Printf("==> PR Key: " + p.Config.PRKey + "\n")
608+
statusID, err = getStatusV2("pr", p.Config.PRKey, p.Config.Host, p.Config.Key)
609+
} else if p.Config.Branch != "" {
610+
fmt.Printf("==> Branch: " + p.Config.Branch + "\n")
611+
statusID, err = getStatusV2("branch", p.Config.Branch, p.Config.Host, p.Config.Key)
612+
} else {
613+
fmt.Printf("==> Project Key: " + p.Config.Key + "\n")
614+
statusID, err = getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
615+
}
616+
603617
if err != nil {
604618
fmt.Printf("\n\n==> Error getting the latest scanID\n\n")
605619
fmt.Printf("Error: %s", err.Error())
@@ -834,6 +848,7 @@ func getStatus(task *TaskResponse, report *SonarReport) string {
834848

835849
func getStatusID(taskIDOld string, sonarHost string, projectSlug string) (string, error) {
836850
// token := os.Getenv("PLUGIN_SONAR_TOKEN")
851+
837852
taskID, err := GetLatestTaskID(sonarHost, projectSlug)
838853
if err != nil {
839854
fmt.Println("Failed to get the latest task ID:", err)
@@ -906,6 +921,90 @@ func getStatusID(taskIDOld string, sonarHost string, projectSlug string) (string
906921
return project.ProjectStatus.Status, nil
907922
}
908923

924+
func getStatusV2(scanType string, scanValue string, sonarHost string, projectSlug string) (string, error) {
925+
// token := os.Getenv("PLUGIN_SONAR_TOKEN")
926+
927+
fmt.Println("Searchng last analysis")
928+
929+
var reportRequest url.Values
930+
931+
if scanType == "branch" {
932+
fmt.Println("Searchng last analysis by branch")
933+
reportRequest = url.Values{
934+
"branch": {scanValue},
935+
"projectKey": {projectSlug},
936+
}
937+
} else {
938+
fmt.Println("Searchng last analysis by pull request")
939+
reportRequest = url.Values{
940+
"pullRequest": {scanValue},
941+
"projectKey": {projectSlug},
942+
}
943+
}
944+
945+
fmt.Printf("==> Job Status Request:\n")
946+
fmt.Printf(sonarHost + "/api/qualitygates/project_status?" + reportRequest.Encode())
947+
fmt.Printf("\n")
948+
fmt.Printf("\n")
949+
fmt.Printf("scanType:" + scanType)
950+
fmt.Printf("scanValue:" + scanValue)
951+
fmt.Printf("\n")
952+
953+
// projectRequest, err := http.NewRequest("GET", sonarHost+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
954+
// projectRequest.Header.Add("Authorization", basicAuth+token)
955+
// projectResponse, err := netClient.Do(projectRequest)
956+
// if err != nil {
957+
// logrus.WithFields(logrus.Fields{
958+
// "error": err,
959+
// }).Fatal("Failed get status")
960+
// return "", err
961+
// }
962+
// buf, _ := ioutil.ReadAll(projectResponse.Body)
963+
buf, err := GetProjectStatus(sonarHost, reportRequest.Encode(), projectSlug)
964+
965+
if err != nil {
966+
logrus.WithFields(logrus.Fields{
967+
"error": err,
968+
}).Error("Failed to get project status")
969+
}
970+
971+
project := ProjectStatusResponse{}
972+
if err := json.Unmarshal(buf, &project); err != nil {
973+
logrus.WithFields(logrus.Fields{
974+
"error": err,
975+
}).Fatal("Failed")
976+
return "", nil
977+
}
978+
979+
fmt.Printf("==> Report Result:\n")
980+
fmt.Printf(string(buf))
981+
982+
// JUNUT
983+
junitReport := ""
984+
junitReport = string(buf) // returns a string of what was written to it
985+
fmt.Printf("\n---------------------> JUNIT Exporter <---------------------\n")
986+
bytesReport := []byte(junitReport)
987+
var projectReport Project
988+
err = json.Unmarshal(bytesReport, &projectReport)
989+
if err != nil {
990+
panic(err)
991+
}
992+
993+
fmt.Printf("%+v", projectReport)
994+
fmt.Printf("\n")
995+
result := ParseJunit(projectReport, "BankingApp")
996+
file, _ := xml.MarshalIndent(result, "", " ")
997+
_ = ioutil.WriteFile("sonarResults.xml", file, 0644)
998+
999+
fmt.Printf("\n")
1000+
fmt.Printf("\n======> JUNIT Exporter <======\n")
1001+
1002+
//JUNIT
1003+
fmt.Printf("\n======> Harness Drone/CIE SonarQube Plugin <======\n\n====> Results:")
1004+
1005+
return project.ProjectStatus.Status, nil
1006+
}
1007+
9091008
func GetProjectStatus(sonarHost string, analysisId string, projectSlug string) ([]byte, error) {
9101009
token := os.Getenv("PLUGIN_SONAR_TOKEN")
9111010
fmt.Printf("\n")

0 commit comments

Comments
 (0)