Skip to content

Commit 12083d1

Browse files
Merge pull request #66 from diegopereiraeng/main
support sonar cloud and sonar open source fix
2 parents 38bff7e + e7a76a7 commit 12083d1

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

plugin.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
// Standard library imports
44
import (
5+
"encoding/base64"
56
"encoding/json"
67
"encoding/xml"
78
"errors"
@@ -740,13 +741,25 @@ func getStatus(task *TaskResponse, report *SonarReport) string {
740741
reportRequest := url.Values{
741742
"analysisId": {task.Task.AnalysisID},
742743
}
744+
sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN")
743745
projectRequest, err := http.NewRequest("GET", report.ServerURL+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
744-
projectRequest.Header.Add("Authorization", "Basic "+os.Getenv("PLUGIN_SONAR_TOKEN"))
746+
projectRequest.Header.Add("Authorization", "Basic "+sonarToken)
745747
projectResponse, err := netClient.Do(projectRequest)
746748
if err != nil {
747749
logrus.WithFields(logrus.Fields{
748750
"error": err,
749-
}).Fatal("Failed get status")
751+
}).Info("Failed to get status, retrying with encoded token...")
752+
753+
// Retry with the token encoded in base64
754+
encodedToken := base64.StdEncoding.EncodeToString([]byte(sonarToken))
755+
projectRequest.Header.Set("Authorization", "Basic "+encodedToken)
756+
projectResponse, err = netClient.Do(projectRequest)
757+
758+
if err != nil {
759+
logrus.WithFields(logrus.Fields{
760+
"error": err,
761+
}).Fatal("Failed to get status after retry")
762+
}
750763
}
751764
buf, _ := ioutil.ReadAll(projectResponse.Body)
752765
project := ProjectStatusResponse{}
@@ -953,18 +966,31 @@ func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) {
953966
return "", err
954967
}
955968

956-
req.SetBasicAuth(os.Getenv("PLUGIN_SONAR_TOKEN"), "")
969+
sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN")
970+
req.SetBasicAuth(sonarToken, "")
957971
resp, err := netClient.Do(req)
958972
if err != nil {
959973
fmt.Printf("\nRequest Error in Task discovery: %s\n", err.Error())
960974
return "", err
961975
}
962976
defer resp.Body.Close()
963977

978+
if resp.StatusCode == http.StatusForbidden {
979+
fmt.Printf("\nError in Task discovery: %s\n", "Check your token permission - probably it does not have 'Browse' permission on the project")
980+
fmt.Printf("Retrying with encoded token...\n")
981+
982+
encodedToken := base64.StdEncoding.EncodeToString([]byte(sonarToken))
983+
req.SetBasicAuth(encodedToken, "")
984+
resp, err = netClient.Do(req)
985+
if err != nil {
986+
fmt.Printf("\nRequest Error in Task discovery after retry: %s\n", err.Error())
987+
return "", err
988+
}
989+
defer resp.Body.Close()
990+
}
991+
964992
if resp.StatusCode != http.StatusOK {
965-
if resp.StatusCode == http.StatusForbidden {
966-
fmt.Printf("\nError in Task discovery: %s\n", "Check your token permission - probably it does not have 'Browse' permission on the project")
967-
} else if resp.StatusCode == http.StatusUnauthorized {
993+
if resp.StatusCode == http.StatusUnauthorized {
968994
fmt.Printf("\nError in Task discovery: %s\n", "Invalid Credentials - your token is not valid")
969995
}
970996
return "", fmt.Errorf("HTTP request error. Status code: %d", resp.StatusCode)

0 commit comments

Comments
 (0)