Skip to content

Commit 76c8d39

Browse files
Merge pull request #71 from diegopereiraeng/main
Job Status more verbose for timeout troubleshooting
2 parents b317272 + 9f84f00 commit 76c8d39

File tree

4 files changed

+88
-41
lines changed

4 files changed

+88
-41
lines changed

.drone.yml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,13 @@ steps:
4545
# ref:
4646
# - refs/heads/main
4747
# - refs/tags/**
48-
- name: publish-2.0.3-java11
49-
image: plugins/docker:20
50-
settings:
51-
# auto_tag: true
52-
# auto_tag_suffix: v2.0.2-java11
53-
tags:
54-
- v2.0.3-java11
55-
# - latest-java11
56-
# - stable-java11
57-
daemon_off: false
58-
dockerfile: Dockerfile
59-
password:
60-
from_secret: docker_password
61-
repo: plugins/sonarqube-scanner
62-
username:
63-
from_secret: docker_username
64-
when:
65-
ref:
66-
- refs/heads/main
67-
- refs/tags/**
68-
- name: publish-2.0.3-java17
48+
- name: publish-2.0.4-java17
6949
image: plugins/docker:20
7050
settings:
7151
# auto_tag: true
7252
# auto_tag_suffix: v2.0.2-java17
7353
tags:
74-
- v2.0.3-java17
54+
- v2.0.4-java17
7555
# - latest-java17
7656
# - stable-java17
7757
daemon_off: false

DockerfileJava17

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ARG SONAR_SCANNER=sonar-scanner-${SONAR_VERSION}
2323
# && apt-get install -y nodejs curl \
2424
# && apt-get clean
2525

26-
RUN apk --no-cache add nodejs curl unzip
26+
RUN apk --no-cache --update add nodejs curl unzip
2727

2828
COPY --from=build /go/src/github.com/diegopereiraeng/harness-cie-sonarqube-scanner/harness-sonar /bin/
2929
WORKDIR /bin

plugin.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/xml"
88
"errors"
99
"fmt"
10+
"io"
1011
"io/ioutil"
1112
"net"
1213
"net/http"
@@ -250,7 +251,7 @@ func displaySummary(total, passed, failed int, errors int, newErrors int, projec
250251
}
251252

252253
// Write to the .env file
253-
filePath := fmt.Sprintf("%s", droneOutputPath)
254+
filePath := fmt.Sprintf(droneOutputPath)
254255
err := writeEnvFile(vars, filePath)
255256
if err != nil {
256257
fmt.Println("Error writing to .env file:", err)
@@ -1034,34 +1035,59 @@ func GetLatestTaskID(sonarHost string, projectSlug string) (string, error) {
10341035
}
10351036

10361037
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")
10371043

10381044
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+
}
10391050
taskRequest.Header.Add("Authorization", basicAuth+os.Getenv("PLUGIN_SONAR_TOKEN"))
10401051
taskResponse, err := netClient.Do(taskRequest)
10411052
if err != nil {
10421053
logrus.WithFields(logrus.Fields{
10431054
"error": err,
10441055
}).Fatal("Failed get sonar job status")
10451056
}
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+
}
10471063
task := TaskResponse{}
1064+
fmt.Println("|----------------------------------------------------------------|")
1065+
fmt.Println("| Report Result: |")
1066+
fmt.Println("|----------------------------------------------------------------|")
1067+
fmt.Print(string(buf))
1068+
fmt.Println("|----------------------------------------------------------------|")
10481069
json.Unmarshal(buf, &task)
10491070
return &task
10501071
}
10511072

10521073
func waitForSonarJob(report *SonarReport) (*TaskResponse, error) {
10531074
timeout := time.After(300 * time.Second)
10541075
tick := time.Tick(500 * time.Millisecond)
1076+
fmt.Println("Waiting for sonar job to finish...")
10551077
for {
10561078
select {
10571079
case <-timeout:
1080+
fmt.Println("Timed out waiting for sonar job to finish")
10581081
return nil, errors.New("timed out")
10591082
case <-tick:
1083+
fmt.Println("Checking sonar job status...")
10601084
job := getSonarJobStatus(report)
10611085
if job.Task.Status == "SUCCESS" {
1086+
fmt.Println("\033[32mSonar job finished successfully\033[0m")
10621087
return job, nil
10631088
}
10641089
if job.Task.Status == "ERROR" {
1090+
fmt.Println("Sonar job failed")
10651091
return nil, errors.New("ERROR")
10661092
}
10671093
}

plugin_test.go

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,68 @@
11
package main
22

33
import (
4-
"reflect"
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
57
"testing"
68
)
79

8-
func TestParseJunit(t *testing.T) {
9-
type args struct {
10-
projectArray Project
11-
projectName string
10+
// Custom RoundTripper for mocking HTTP client
11+
type roundTripFunc func(req *http.Request) *http.Response
12+
13+
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
14+
return f(req), nil
15+
}
16+
17+
func getTask(projectSlug string) (string, error) {
18+
// Define your implementation for unit test
19+
return "sampleKey", nil
20+
}
21+
22+
func TestGetTask(t *testing.T) {
23+
httpClient := &http.Client{
24+
Transport: roundTripFunc(func(req *http.Request) *http.Response {
25+
return &http.Response{
26+
StatusCode: http.StatusOK,
27+
Body: ioutil.NopCloser(bytes.NewBufferString(`{"analyses":[{"key":"sampleKey"}]}`)),
28+
}
29+
}),
30+
}
31+
netClient = httpClient
32+
33+
key, err := getTask("projectSlug")
34+
if err != nil {
35+
t.Errorf("Unexpected error: %v", err)
1236
}
13-
tests := []struct {
14-
name string
15-
args args
16-
want Testsuites
17-
}{
18-
// TODO: Add test cases.
37+
if key != "sampleKey" {
38+
t.Errorf("Expected sampleKey, got %v", key)
1939
}
20-
for _, tt := range tests {
21-
t.Run(tt.name, func(t *testing.T) {
22-
if got := ParseJunit(tt.args.projectArray, tt.args.projectName); !reflect.DeepEqual(got, tt.want) {
23-
t.Errorf("ParseJunit() = %v, want %v", got, tt.want)
40+
}
41+
42+
// func getTask(s string) {
43+
// panic("unimplemented")
44+
// }
45+
46+
// Test for Sonar Job Status function
47+
func TestGetSonarJobStatus(t *testing.T) {
48+
httpClient := &http.Client{
49+
Transport: roundTripFunc(func(req *http.Request) *http.Response {
50+
return &http.Response{
51+
StatusCode: http.StatusOK,
52+
Body: ioutil.NopCloser(bytes.NewBufferString(`{"task":{"status":"SUCCESS"}}`)),
2453
}
25-
})
54+
}),
55+
}
56+
netClient = httpClient
57+
58+
report := &SonarReport{CeTaskURL: "someURL"}
59+
taskResponse := getSonarJobStatus(report)
60+
if taskResponse.Task.Status != "SUCCESS" {
61+
t.Errorf("Expected SUCCESS, got %v", taskResponse.Task.Status)
2662
}
2763
}
64+
65+
// Test for Wait for Sonar Job function
66+
// func TestWaitForSonarJob(t *testing.T) {
67+
// // Define your test logic here
68+
// }

0 commit comments

Comments
 (0)