|
7 | 7 | "encoding/xml" |
8 | 8 | "errors" |
9 | 9 | "fmt" |
| 10 | + "io" |
10 | 11 | "io/ioutil" |
11 | 12 | "net" |
12 | 13 | "net/http" |
@@ -250,7 +251,7 @@ func displaySummary(total, passed, failed int, errors int, newErrors int, projec |
250 | 251 | } |
251 | 252 |
|
252 | 253 | // Write to the .env file |
253 | | - filePath := fmt.Sprintf("%s", droneOutputPath) |
| 254 | + filePath := fmt.Sprintf(droneOutputPath) |
254 | 255 | err := writeEnvFile(vars, filePath) |
255 | 256 | if err != nil { |
256 | 257 | fmt.Println("Error writing to .env file:", err) |
@@ -1034,34 +1035,59 @@ func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) { |
1034 | 1035 | } |
1035 | 1036 |
|
1036 | 1037 | func getSonarJobStatus(report *SonarReport) *TaskResponse { |
| 1038 | + fmt.Printf("\n") |
| 1039 | + fmt.Printf("==> Job Status Request:\n") |
| 1040 | + fmt.Printf(report.ServerURL + "/api/ce/task?id=" + report.CeTaskID) |
| 1041 | + fmt.Printf("\n") |
| 1042 | + fmt.Printf("\n") |
1037 | 1043 |
|
1038 | 1044 | taskRequest, err := http.NewRequest("GET", report.CeTaskURL, nil) |
| 1045 | + if err != nil { |
| 1046 | + logrus.WithFields(logrus.Fields{ |
| 1047 | + "error": err, |
| 1048 | + }).Fatal("Failed get sonar job status") |
| 1049 | + } |
1039 | 1050 | taskRequest.Header.Add("Authorization", basicAuth+os.Getenv("PLUGIN_SONAR_TOKEN")) |
1040 | 1051 | taskResponse, err := netClient.Do(taskRequest) |
1041 | 1052 | if err != nil { |
1042 | 1053 | logrus.WithFields(logrus.Fields{ |
1043 | 1054 | "error": err, |
1044 | 1055 | }).Fatal("Failed get sonar job status") |
1045 | 1056 | } |
1046 | | - buf, _ := ioutil.ReadAll(taskResponse.Body) |
| 1057 | + buf, err := io.ReadAll(taskResponse.Body) |
| 1058 | + if err != nil { |
| 1059 | + logrus.WithFields(logrus.Fields{ |
| 1060 | + "error": err, |
| 1061 | + }).Fatal("Failed to read sonar job status response body") |
| 1062 | + } |
1047 | 1063 | task := TaskResponse{} |
| 1064 | + fmt.Println("|----------------------------------------------------------------|") |
| 1065 | + fmt.Println("| Report Result: |") |
| 1066 | + fmt.Println("|----------------------------------------------------------------|") |
| 1067 | + fmt.Print(string(buf)) |
| 1068 | + fmt.Println("|----------------------------------------------------------------|") |
1048 | 1069 | json.Unmarshal(buf, &task) |
1049 | 1070 | return &task |
1050 | 1071 | } |
1051 | 1072 |
|
1052 | 1073 | func waitForSonarJob(report *SonarReport) (*TaskResponse, error) { |
1053 | 1074 | timeout := time.After(300 * time.Second) |
1054 | 1075 | tick := time.Tick(500 * time.Millisecond) |
| 1076 | + fmt.Println("Waiting for sonar job to finish...") |
1055 | 1077 | for { |
1056 | 1078 | select { |
1057 | 1079 | case <-timeout: |
| 1080 | + fmt.Println("Timed out waiting for sonar job to finish") |
1058 | 1081 | return nil, errors.New("timed out") |
1059 | 1082 | case <-tick: |
| 1083 | + fmt.Println("Checking sonar job status...") |
1060 | 1084 | job := getSonarJobStatus(report) |
1061 | 1085 | if job.Task.Status == "SUCCESS" { |
| 1086 | + fmt.Println("\033[32mSonar job finished successfully\033[0m") |
1062 | 1087 | return job, nil |
1063 | 1088 | } |
1064 | 1089 | if job.Task.Status == "ERROR" { |
| 1090 | + fmt.Println("Sonar job failed") |
1065 | 1091 | return nil, errors.New("ERROR") |
1066 | 1092 | } |
1067 | 1093 | } |
|
0 commit comments