-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (82 loc) · 3.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"log"
"os"
"strings"
"time"
"github.com/Nhattd97/kobiton-execute-test-buildkite-plugin/app/model"
"github.com/Nhattd97/kobiton-execute-test-buildkite-plugin/app/utils"
)
const MAX_MS_WAIT_FOR_EXECUTION = 1 * 3600 * 1000 // 1 hour in miliseconds
var jobId = ""
var reportUrl = ""
func main() {
stepConfig := new(model.StepConfig)
stepConfig.Init()
var executorBasicAuth = strings.Join([]string{stepConfig.GetExecutorUsername(), stepConfig.GetExecutorPassword()}, ":")
var executorBasicAuthEncoded = utils.Base64Encode(executorBasicAuth)
var headers = map[string]string{}
headers["x-kobiton-credential-username"] = stepConfig.GetKobiUsername()
headers["x-kobiton-credential-api-key"] = stepConfig.GetKobiPassword()
headers["authorization"] = "Basic " + executorBasicAuthEncoded
headers["content-type"] = "application/json"
headers["accept"] = "application/json"
executorPayload := new(model.ExecutorRequestPayload)
model.BuildExecutorRequestPayload(executorPayload, stepConfig)
executorJsonPayload, _ := json.MarshalIndent(executorPayload, "", " ")
client := utils.HttpClient()
var executorUrl = stepConfig.GetExecutorUrl() + "/submit"
var response = utils.SendRequest(client, "POST", executorUrl, headers, executorJsonPayload)
jobId = string(response)
if stepConfig.IsWaitForExecution() {
log.Printf("Requesting to get logs for job %s", jobId)
var getJobInfoUrl = stepConfig.GetExecutorUrl() + "/jobs/" + jobId
var getJobLogUrl = getJobInfoUrl + "/logs?type=" + stepConfig.GetLogType()
var getReportUrl = getJobInfoUrl + "/report"
var isTimeout = false
ticker := time.NewTicker(30 * time.Second)
var authHeader = map[string]string{"authorization": "Basic " + executorBasicAuthEncoded}
var jobResponse model.JobResponse
var waitingBeginAt = time.Now().UnixMilli()
for range ticker.C {
var response = utils.SendRequest(client, "GET", getJobInfoUrl, authHeader, nil)
json.Unmarshal(response, &jobResponse)
log.Println("Job Status: ", jobResponse.Status)
if jobResponse.Status == "COMPLETED" || jobResponse.Status == "FAILED" {
log.Printf("Job ID %s is finish with status: %s", jobId, jobResponse.Status)
break
} else {
var currentTime = time.Now().UnixMilli()
if currentTime-waitingBeginAt >= MAX_MS_WAIT_FOR_EXECUTION {
isTimeout = true
break
}
}
}
defer ticker.Stop()
if isTimeout {
log.Println("==============================================================================")
log.Println("Execution has reached maximum waiting time")
} else {
var logResponse = utils.SendRequest(client, "GET", getJobLogUrl, authHeader, nil)
log.Println("==============================================================================")
log.Println(string(logResponse))
var reportResponse = utils.SendRequest(client, "GET", getReportUrl, authHeader, nil)
reportUrl = string(reportResponse)
}
}
log.Println("==============================================================================")
if jobId != "" {
log.Println("Job ID: ", jobId)
}
if reportUrl != "" {
log.Println("Report URL: ", reportUrl)
}
//
// --- Exit codes:
// The exit code of your Step is very important. If you return
// with a 0 exit code `bitrise` will register your Step as "successful".
// Any non zero exit code will be registered as "failed" by `bitrise`.
os.Exit(0)
}