-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathservices.go
More file actions
197 lines (139 loc) · 4.5 KB
/
services.go
File metadata and controls
197 lines (139 loc) · 4.5 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func build(app_url string, test_suite_url string, username string, access_key string) (string, error) {
if app_url == "" || test_suite_url == "" {
return "", errors.New(FILE_NOT_AVAILABLE_ERROR)
}
payload_values := createBuildPayload()
payload_values.App = app_url
payload_values.TestSuite = test_suite_url
payload, _ := json.Marshal(payload_values)
final_payload := appendExtraCapabilities(string(payload))
// log.Print("Final payload -> ", string(final_payload))
client := &http.Client{}
req, _ := http.NewRequest("POST", BROWSERSTACK_DOMAIN+APP_AUTOMATE_BUILD_ENDPOINT, bytes.NewBuffer(final_payload))
req.SetBasicAuth(username+"-bitrise", access_key)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return "", errors.New(fmt.Sprintf(HTTP_ERROR, err))
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.New(fmt.Sprintf(HTTP_ERROR, err))
}
return string(body), nil
}
// this function uploads both app and test suite
func upload(app_path string, endpoint string, username string, access_key string) (string, error) {
if app_path == "" {
return "", errors.New(FILE_NOT_AVAILABLE_ERROR)
}
payload := &bytes.Buffer{}
multipart_writer := multipart.NewWriter(payload)
file, fileErr := os.Open(app_path)
if fileErr != nil {
return "", errors.New(FILE_NOT_AVAILABLE_ERROR)
}
defer file.Close()
// creates a new form data header
// reading and copying the file's content to form data
attached_file,
fileErr := multipart_writer.CreateFormFile("file", filepath.Base(app_path))
if fileErr != nil {
return "", errors.New(FILE_NOT_AVAILABLE_ERROR)
}
_, fileErr = io.Copy(attached_file, file)
if fileErr != nil {
return "", errors.New(FILE_NOT_AVAILABLE_ERROR)
}
err := multipart_writer.Close()
if err != nil {
return "", errors.New(INVALID_FILE_TYPE_ERROR)
}
client := &http.Client{}
req, _ := http.NewRequest("POST", BROWSERSTACK_DOMAIN+endpoint, payload)
req.SetBasicAuth(username, access_key)
req.Header.Set("Content-Type", multipart_writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
return "", errors.New(fmt.Sprintf(HTTP_ERROR, err))
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.New(fmt.Sprintf(HTTP_ERROR, err))
}
return string(body), nil
}
func checkBuildStatus(build_id string, username string, access_key string, waitForBuild bool) (string, error) {
if build_id == "" {
return "", errors.New(fmt.Sprintf(FETCH_BUILD_STATUS_ERROR, "invalid build_id"))
}
if waitForBuild {
log.Println("Waiting for results")
}
// ticker can't have negative value
var POLLING_INTERVAL int = 1000
if waitForBuild {
POLLING_INTERVAL = POLLING_INTERVAL_IN_MS
}
build_parsed_response := make(map[string]interface{})
build_status := ""
var body []byte
var build_status_error error
clear := setInterval(func() {
client := &http.Client{}
req, _ := http.NewRequest("GET", BROWSERSTACK_DOMAIN+APP_AUTOMATE_BUILD_STATUS_ENDPOINT+build_id, nil)
req.SetBasicAuth(username, access_key)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
build_status_error = errors.New(fmt.Sprintf(HTTP_ERROR, err))
return
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
build_status_error = errors.New(fmt.Sprintf(HTTP_ERROR, err))
return
}
unmarshal_err := json.Unmarshal([]byte(body), &build_parsed_response)
if unmarshal_err != nil {
build_status_error = errors.New(fmt.Sprintf(HTTP_ERROR, err))
return
}
if build_parsed_response["error"] != nil && build_parsed_response["error"] != "" {
build_status_error = errors.New(fmt.Sprintf(FETCH_BUILD_STATUS_ERROR, build_parsed_response["error"]))
return
}
log.Printf("Build is running (BrowserStack build id %s)", build_id)
build_status = build_parsed_response["status"].(string)
}, POLLING_INTERVAL, false)
// infinite loop -> consider this as a ticker
for {
if build_status != "running" && build_status != "" {
// Stop the ticket, ending the interval go routine
clear <- true
printBuildStatus(build_parsed_response)
return build_status, build_status_error
}
if build_status_error != nil || (!waitForBuild && build_status != "") {
clear <- true
return build_status, build_status_error
}
}
}