Skip to content

Commit 88e37e0

Browse files
auth fix new sonar versions
1 parent 518e7ed commit 88e37e0

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

plugin.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,22 @@ type (
107107
// TaskResponse Give Compute Engine task details such as type, status, duration and associated component.
108108
TaskResponse struct {
109109
Task struct {
110-
ID string `json:"id"`
111-
Type string `json:"type"`
112-
ComponentID string `json:"componentId"`
113-
ComponentKey string `json:"componentKey"`
114-
ComponentName string `json:"componentName"`
115-
AnalysisID string `json:"analysisId"`
116-
Status string `json:"status"`
110+
ID string `json:"id"`
111+
Type string `json:"type"`
112+
ComponentID string `json:"componentId"`
113+
ComponentKey string `json:"componentKey"`
114+
ComponentName string `json:"componentName"`
115+
ComponentQualifier string `json:"componentQualifier"`
116+
AnalysisID string `json:"analysisId"`
117+
Status string `json:"status"`
118+
SubmittedAt string `json:"submittedAt"`
119+
SubmitterLogin string `json:"submitterLogin"`
120+
StartedAt string `json:"startedAt"`
121+
ExecutedAt string `json:"executedAt"`
122+
ExecutionTimeMs int `json:"executionTimeMs"`
123+
HasScannerContext bool `json:"hasScannerContext"`
124+
WarningCount int `json:"warningCount"`
125+
Warnings []string `json:"warnings"`
117126
} `json:"task"`
118127
}
119128

@@ -753,34 +762,43 @@ func getStatus(task *TaskResponse, report *SonarReport) string {
753762
"analysisId": {task.Task.AnalysisID},
754763
}
755764
sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN")
765+
766+
// First try with Basic Auth
756767
projectRequest, err := http.NewRequest("GET", report.ServerURL+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
757-
projectRequest.Header.Add("Authorization", basicAuth+sonarToken)
758-
projectResponse, err := netClient.Do(projectRequest)
759768
if err != nil {
760769
logrus.WithFields(logrus.Fields{
761770
"error": err,
762-
}).Info("Failed to get status, retrying with encoded token...")
771+
}).Fatal("Failed get status")
772+
}
773+
774+
projectRequest.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(sonarToken+":")))
775+
projectResponse, err := netClient.Do(projectRequest)
776+
777+
if err != nil || projectResponse.StatusCode != http.StatusOK {
778+
logrus.WithFields(logrus.Fields{
779+
"error": err,
780+
}).Info("Failed to get status with Basic Auth, retrying with Bearer token...")
763781

764-
// Retry with the token encoded in base64
765-
encodedToken := base64.StdEncoding.EncodeToString([]byte(sonarToken))
766-
projectRequest.Header.Set("Authorization", "Basic "+encodedToken)
782+
// Retry with Bearer token
783+
projectRequest.Header.Set("Authorization", "Bearer "+sonarToken)
767784
projectResponse, err = netClient.Do(projectRequest)
768785

769-
if err != nil {
786+
if err != nil || projectResponse.StatusCode != http.StatusOK {
770787
logrus.WithFields(logrus.Fields{
771788
"error": err,
772-
}).Fatal("Failed to get status after retry")
789+
}).Fatal("Failed to get status after retry with Bearer token")
773790
}
774791
}
775-
buf, _ := ioutil.ReadAll(projectResponse.Body)
792+
793+
buf, _ := io.ReadAll(projectResponse.Body)
776794
project := ProjectStatusResponse{}
777795
if err := json.Unmarshal(buf, &project); err != nil {
778796
logrus.WithFields(logrus.Fields{
779797
"error": err,
780798
}).Fatal("Failed")
781799
}
782800
fmt.Printf("==> Report Result:\n")
783-
fmt.Printf(string(buf))
801+
fmt.Println(string(buf))
784802

785803
// JUNUT
786804
junitReport := ""

0 commit comments

Comments
 (0)