3333
3434 // sonarDashStatic is a static string used in the dashboard URL.
3535 sonarDashStatic = "/dashboard?id="
36+
37+ // basicAuth is the basic authentication string.
38+ basicAuth = "Basic "
3639)
3740
3841type (
@@ -743,7 +746,7 @@ func getStatus(task *TaskResponse, report *SonarReport) string {
743746 }
744747 sonarToken := os .Getenv ("PLUGIN_SONAR_TOKEN" )
745748 projectRequest , err := http .NewRequest ("GET" , report .ServerURL + "/api/qualitygates/project_status?" + reportRequest .Encode (), nil )
746- projectRequest .Header .Add ("Authorization" , "Basic " + sonarToken )
749+ projectRequest .Header .Add ("Authorization" , basicAuth + sonarToken )
747750 projectResponse , err := netClient .Do (projectRequest )
748751 if err != nil {
749752 logrus .WithFields (logrus.Fields {
@@ -817,7 +820,7 @@ func getStatusID(taskIDOld string, sonarHost string, projectSlug string) (string
817820 fmt .Printf ("\n " )
818821
819822 // projectRequest, err := http.NewRequest("GET", sonarHost+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
820- // projectRequest.Header.Add("Authorization", "Basic " +token)
823+ // projectRequest.Header.Add("Authorization", basicAuth +token)
821824 // projectResponse, err := netClient.Do(projectRequest)
822825 // if err != nil {
823826 // logrus.WithFields(logrus.Fields{
@@ -886,13 +889,16 @@ func GetProjectStatus(sonarHost string, analysisId string) ([]byte, error) {
886889
887890 fmt .Printf ("\n " )
888891 // fmt.Printf("Setting Authorization header:" + token)
892+ // Retry with the token encoded in base64
893+ encodedToken := base64 .StdEncoding .EncodeToString ([]byte (token ))
894+ projectRequest .Header .Set ("Authorization" , basicAuth + encodedToken )
889895
890- projectRequest .Header .Add ("Authorization" , "Basic " + token )
896+ // projectRequest.Header.Add("Authorization", basicAuth +token)
891897 projectResponse , err := netClient .Do (projectRequest )
892898
893899 if err != nil {
894900 fmt .Printf ("\n " )
895- fmt .Printf ("NIL - Error getting project status, trying again with bearer token... " )
901+ fmt .Printf ("NIL - Error getting project status, failed! " )
896902
897903 return nil , err
898904
@@ -912,13 +918,14 @@ func GetProjectStatus(sonarHost string, analysisId string) ([]byte, error) {
912918 projectBearerResponse , err := netClient .Do (projectBearerRequest )
913919 if err != nil {
914920 fmt .Printf ("\n " )
915- fmt .Printf ("Error getting project status, trying again with bearer token..." )
921+ fmt .Printf ("NIL - Error getting project status, trying again with bearer token..." )
916922 return nil , err
917923 }
918924 fmt .Printf ("Response Code with Bearer:" + projectBearerResponse .Status )
919925 if projectBearerResponse .StatusCode == 401 {
920926 fmt .Printf ("\n " )
921927 fmt .Printf ("Error getting project status, trying again with bearer token..." )
928+
922929 return nil , fmt .Errorf ("unauthorized to get project status" )
923930 }
924931 bufResponse , err := ioutil .ReadAll (projectBearerResponse .Body )
@@ -980,6 +987,8 @@ func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) {
980987 fmt .Printf ("Retrying with encoded token...\n " )
981988
982989 encodedToken := base64 .StdEncoding .EncodeToString ([]byte (sonarToken ))
990+ req .Header .Add ("Authorization" , basicAuth + encodedToken )
991+ fmt .Printf ("Token encoded: %s\n " , encodedToken )
983992 req .SetBasicAuth (encodedToken , "" )
984993 resp , err = netClient .Do (req )
985994 if err != nil {
@@ -1026,7 +1035,7 @@ func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) {
10261035func getSonarJobStatus (report * SonarReport ) * TaskResponse {
10271036
10281037 taskRequest , err := http .NewRequest ("GET" , report .CeTaskURL , nil )
1029- taskRequest .Header .Add ("Authorization" , "Basic " + os .Getenv ("PLUGIN_SONAR_TOKEN" ))
1038+ taskRequest .Header .Add ("Authorization" , basicAuth + os .Getenv ("PLUGIN_SONAR_TOKEN" ))
10301039 taskResponse , err := netClient .Do (taskRequest )
10311040 if err != nil {
10321041 logrus .WithFields (logrus.Fields {
0 commit comments