|
40 | 40 | basicAuth = "Basic " |
41 | 41 | ) |
42 | 42 |
|
| 43 | +const ( |
| 44 | + lineBreak = "----------------------------------------------" |
| 45 | + lineBreak2 = "|----------------------------------------------------------------|" |
| 46 | +) |
| 47 | + |
43 | 48 | type ( |
44 | 49 | Config struct { |
45 | 50 | Key string |
@@ -102,13 +107,22 @@ type ( |
102 | 107 | // TaskResponse Give Compute Engine task details such as type, status, duration and associated component. |
103 | 108 | TaskResponse struct { |
104 | 109 | Task struct { |
105 | | - ID string `json:"id"` |
106 | | - Type string `json:"type"` |
107 | | - ComponentID string `json:"componentId"` |
108 | | - ComponentKey string `json:"componentKey"` |
109 | | - ComponentName string `json:"componentName"` |
110 | | - AnalysisID string `json:"analysisId"` |
111 | | - 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"` |
112 | 126 | } `json:"task"` |
113 | 127 | } |
114 | 128 |
|
@@ -181,8 +195,6 @@ type AnalysisResponse struct { |
181 | 195 | } `json:"analyses"` |
182 | 196 | } |
183 | 197 |
|
184 | | -const lineBreak = "----------------------------------------------" |
185 | | - |
186 | 198 | func init() { |
187 | 199 | netClient = &http.Client{ |
188 | 200 | Timeout: time.Second * 10, |
@@ -750,34 +762,49 @@ func getStatus(task *TaskResponse, report *SonarReport) string { |
750 | 762 | "analysisId": {task.Task.AnalysisID}, |
751 | 763 | } |
752 | 764 | sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN") |
| 765 | + |
| 766 | + // First try with Basic Auth |
753 | 767 | projectRequest, err := http.NewRequest("GET", report.ServerURL+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil) |
754 | | - projectRequest.Header.Add("Authorization", basicAuth+sonarToken) |
755 | | - projectResponse, err := netClient.Do(projectRequest) |
756 | 768 | if err != nil { |
757 | 769 | logrus.WithFields(logrus.Fields{ |
758 | 770 | "error": err, |
759 | | - }).Info("Failed to get status, retrying with encoded token...") |
| 771 | + }).Fatal("Failed get status") |
| 772 | + } |
| 773 | + fmt.Printf("==> Job Quality Gate Request:\n") |
| 774 | + fmt.Printf(report.ServerURL + "/api/qualitygates/project_status?" + reportRequest.Encode()) |
| 775 | + fmt.Printf("\n") |
| 776 | + fmt.Printf("\n") |
| 777 | + projectRequest.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(sonarToken+":"))) |
| 778 | + projectResponse, err := netClient.Do(projectRequest) |
| 779 | + |
| 780 | + if err != nil || projectResponse.StatusCode != http.StatusOK { |
| 781 | + logrus.WithFields(logrus.Fields{ |
| 782 | + "error": err, |
| 783 | + }).Info("Failed to get status with Basic Auth, retrying with Bearer token...") |
760 | 784 |
|
761 | | - // Retry with the token encoded in base64 |
762 | | - encodedToken := base64.StdEncoding.EncodeToString([]byte(sonarToken)) |
763 | | - projectRequest.Header.Set("Authorization", "Basic "+encodedToken) |
| 785 | + // Retry with Bearer token |
| 786 | + projectRequest.Header.Set("Authorization", "Bearer "+sonarToken) |
764 | 787 | projectResponse, err = netClient.Do(projectRequest) |
765 | 788 |
|
766 | | - if err != nil { |
| 789 | + if err != nil || projectResponse.StatusCode != http.StatusOK { |
767 | 790 | logrus.WithFields(logrus.Fields{ |
768 | 791 | "error": err, |
769 | | - }).Fatal("Failed to get status after retry") |
| 792 | + }).Fatal("Failed to get status after retry with Bearer token") |
770 | 793 | } |
771 | 794 | } |
772 | | - buf, _ := ioutil.ReadAll(projectResponse.Body) |
| 795 | + |
| 796 | + buf, _ := io.ReadAll(projectResponse.Body) |
| 797 | + fmt.Printf("==> Report Result:\n") |
| 798 | + fmt.Println(string(buf)) |
| 799 | + fmt.Printf("\n") |
773 | 800 | project := ProjectStatusResponse{} |
774 | 801 | if err := json.Unmarshal(buf, &project); err != nil { |
775 | 802 | logrus.WithFields(logrus.Fields{ |
776 | 803 | "error": err, |
777 | 804 | }).Fatal("Failed") |
778 | 805 | } |
779 | 806 | fmt.Printf("==> Report Result:\n") |
780 | | - fmt.Printf(string(buf)) |
| 807 | + fmt.Println(string(buf)) |
781 | 808 |
|
782 | 809 | // JUNUT |
783 | 810 | junitReport := "" |
@@ -968,50 +995,55 @@ func GetProjectStatus(sonarHost string, analysisId string, projectSlug string) ( |
968 | 995 | return buf, nil |
969 | 996 | } |
970 | 997 |
|
| 998 | +func addBearerToken(req *http.Request, token string) { |
| 999 | + req.Header.Add("Authorization", "Bearer "+token) |
| 1000 | +} |
| 1001 | + |
| 1002 | +func addBasicAuth(req *http.Request, token string) { |
| 1003 | + req.SetBasicAuth(token, "") |
| 1004 | +} |
| 1005 | + |
971 | 1006 | func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) { |
972 | 1007 | fmt.Printf("\nStarting Task ID Discovery\n") |
973 | 1008 | url := fmt.Sprintf("%s/api/project_analyses/search?project=%s&ps=1", sonarHost, projectSlug) |
974 | 1009 | fmt.Printf("URL: %s\n", url) |
975 | 1010 |
|
976 | | - req, err := http.NewRequest("GET", url, nil) |
| 1011 | + taskRequest, err := http.NewRequest("GET", url, nil) |
977 | 1012 | if err != nil { |
978 | 1013 | fmt.Printf("\nError to create request in Task discovery: %s\n", err.Error()) |
979 | 1014 | return "", err |
980 | 1015 | } |
981 | 1016 |
|
982 | 1017 | sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN") |
983 | | - req.SetBasicAuth(sonarToken, "") |
984 | | - resp, err := netClient.Do(req) |
| 1018 | + // First, try with Bearer token |
| 1019 | + addBearerToken(taskRequest, sonarToken) |
| 1020 | + taskResponse, err := netClient.Do(taskRequest) |
985 | 1021 | if err != nil { |
986 | | - fmt.Printf("\nRequest Error in Task discovery: %s\n", err.Error()) |
987 | | - return "", err |
| 1022 | + logrus.WithFields(logrus.Fields{ |
| 1023 | + "error": err, |
| 1024 | + }).Fatal("Failed get sonar job status") |
988 | 1025 | } |
989 | | - defer resp.Body.Close() |
990 | 1026 |
|
991 | | - if resp.StatusCode == http.StatusForbidden { |
992 | | - fmt.Printf("\nError in Task discovery: %s\n", "Check your token permission - probably it does not have 'Browse' permission on the project") |
993 | | - fmt.Printf("Retrying with encoded token...\n") |
994 | | - |
995 | | - encodedToken := base64.StdEncoding.EncodeToString([]byte(sonarToken)) |
996 | | - req.Header.Add("Authorization", basicAuth+encodedToken) |
997 | | - fmt.Printf("Token encoded: %s\n", encodedToken) |
998 | | - req.SetBasicAuth(encodedToken, "") |
999 | | - resp, err = netClient.Do(req) |
| 1027 | + // If Forbidden, try with Basic Auth |
| 1028 | + if taskResponse.StatusCode == http.StatusForbidden { |
| 1029 | + fmt.Printf("\nRetrying with Basic Auth...\n") |
| 1030 | + addBasicAuth(taskRequest, sonarToken) |
| 1031 | + taskResponse, err = netClient.Do(taskRequest) |
1000 | 1032 | if err != nil { |
1001 | | - fmt.Printf("\nRequest Error in Task discovery after retry: %s\n", err.Error()) |
1002 | | - return "", err |
| 1033 | + logrus.WithFields(logrus.Fields{ |
| 1034 | + "error": err, |
| 1035 | + }).Fatal("Failed get sonar job status") |
1003 | 1036 | } |
1004 | | - defer resp.Body.Close() |
1005 | 1037 | } |
1006 | 1038 |
|
1007 | | - if resp.StatusCode != http.StatusOK { |
1008 | | - if resp.StatusCode == http.StatusUnauthorized { |
| 1039 | + if taskResponse.StatusCode != http.StatusOK { |
| 1040 | + if taskResponse.StatusCode == http.StatusUnauthorized { |
1009 | 1041 | fmt.Printf("\nError in Task discovery: %s\n", "Invalid Credentials - your token is not valid") |
1010 | 1042 | } |
1011 | | - return "", fmt.Errorf("HTTP request error. Status code: %d", resp.StatusCode) |
| 1043 | + return "", fmt.Errorf("HTTP request error. Status code: %d", taskResponse.StatusCode) |
1012 | 1044 | } |
1013 | 1045 |
|
1014 | | - body, err := ioutil.ReadAll(resp.Body) |
| 1046 | + body, err := io.ReadAll(taskResponse.Body) |
1015 | 1047 | if err != nil { |
1016 | 1048 | fmt.Printf("\nError reading response body in Task discovery: %s\n", err.Error()) |
1017 | 1049 | return "", err |
@@ -1049,27 +1081,48 @@ func getSonarJobStatus(report *SonarReport) *TaskResponse { |
1049 | 1081 | if err != nil { |
1050 | 1082 | logrus.WithFields(logrus.Fields{ |
1051 | 1083 | "error": err, |
1052 | | - }).Fatal("Failed get sonar job status") |
| 1084 | + }).Fatal("Failed to create request for Sonar job status") |
1053 | 1085 | } |
1054 | | - taskRequest.Header.Add("Authorization", basicAuth+os.Getenv("PLUGIN_SONAR_TOKEN")) |
| 1086 | + |
| 1087 | + sonarToken := os.Getenv("PLUGIN_SONAR_TOKEN") |
| 1088 | + taskRequest.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(sonarToken+":"))) |
| 1089 | + |
1055 | 1090 | taskResponse, err := netClient.Do(taskRequest) |
1056 | 1091 | if err != nil { |
1057 | 1092 | logrus.WithFields(logrus.Fields{ |
1058 | 1093 | "error": err, |
1059 | | - }).Fatal("Failed get sonar job status") |
| 1094 | + }).Fatal("Failed to get Sonar job status") |
1060 | 1095 | } |
| 1096 | + |
| 1097 | + if taskResponse.StatusCode == http.StatusForbidden { |
| 1098 | + fmt.Println("Basic Auth failed. Retrying with Bearer token...") |
| 1099 | + taskRequest.Header.Set("Authorization", "Bearer "+sonarToken) |
| 1100 | + taskResponse, err = netClient.Do(taskRequest) |
| 1101 | + if err != nil { |
| 1102 | + logrus.WithFields(logrus.Fields{ |
| 1103 | + "error": err, |
| 1104 | + }).Fatal("Failed to get Sonar job status with Bearer token") |
| 1105 | + } |
| 1106 | + } |
| 1107 | + |
1061 | 1108 | buf, err := io.ReadAll(taskResponse.Body) |
1062 | 1109 | if err != nil { |
1063 | 1110 | logrus.WithFields(logrus.Fields{ |
1064 | 1111 | "error": err, |
1065 | | - }).Fatal("Failed to read sonar job status response body") |
| 1112 | + }).Fatal("Failed to read Sonar job status response body") |
1066 | 1113 | } |
| 1114 | + |
| 1115 | + fmt.Printf("\n==> Job Status Response:\n") |
| 1116 | + fmt.Println(string(buf)) |
| 1117 | + fmt.Printf("\n") |
| 1118 | + |
1067 | 1119 | task := TaskResponse{} |
1068 | | - fmt.Println("|----------------------------------------------------------------|") |
| 1120 | + |
| 1121 | + fmt.Println(lineBreak2) |
1069 | 1122 | fmt.Println("| Report Result: |") |
1070 | | - fmt.Println("|----------------------------------------------------------------|") |
| 1123 | + fmt.Println(lineBreak2) |
1071 | 1124 | fmt.Print(string(buf)) |
1072 | | - fmt.Println("|----------------------------------------------------------------|") |
| 1125 | + fmt.Println(lineBreak2) |
1073 | 1126 | json.Unmarshal(buf, &task) |
1074 | 1127 | return &task |
1075 | 1128 | } |
|
0 commit comments