-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagicpod-api-client.go
430 lines (410 loc) · 12.3 KB
/
magicpod-api-client.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/Magic-Pod/magicpod-api-client/common"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Version = "1.2.0.1"
app.Name = "magicpod-api-client"
app.Usage = "Simple and useful wrapper for MagicPod Web API"
app.Flags = []cli.Flag{
// hidden option only for MagicPod developers
cli.StringFlag{
Name: "url-base",
Value: "https://app.magicpod.com",
Hidden: true,
},
}
app.Commands = []cli.Command{
{
Name: "batch-run",
Usage: "Run batch test",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "test_settings_number, S",
Usage: "Test settings number defined in the project batch run page",
},
cli.StringFlag{
Name: "setting, s",
Usage: "Test setting in JSON format. Please check https://app.magicpod.com/api/v1.0/doc/ for more detail",
},
cli.BoolFlag{
Name: "no_wait, n",
Usage: "Return immediately without waiting the batch run to be finished",
},
cli.IntFlag{
Name: "wait_limit, w",
Usage: "Wait limit in seconds. If 0 is specified, the value is test count x 10 minutes",
},
}...),
Action: batchRunAction,
},
{
Name: "get-batch-run",
Usage: "Get batch run result",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "batch_run_number, b",
Usage: "Batch run number",
},
}...),
Action: getBatchRunAction,
},
{
Name: "get-batch-runs",
Usage: "Get the batch runs information in the **most recent first** order.",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "count, c",
Usage: "The maximum number of records to retrieve.",
Value: 20,
},
cli.IntFlag{
Name: "max_batch_run_number, max",
Usage: "The most recent batch run number to start retrieving records from.",
Required: false,
},
cli.IntFlag{
Name: "min_batch_run_number, min",
Usage: "The least recent batch run number to stop retrieving records at.",
Required: false,
},
}...),
Action: getBatchRunsAction,
},
{
Name: "latest-batch-run-no",
Usage: "Get the latest batch run number",
Flags: commonFlags(),
Action: latestBatchRunNoAction,
},
{
Name: "upload-app",
Usage: "Upload app/ipa/apk file",
Flags: append(commonFlags(), []cli.Flag{
cli.StringFlag{
Name: "app_path, a",
Usage: "Path to the app/ipa/apk file to upload",
},
}...),
Action: uploadAppAction,
},
{
Name: "delete-app",
Usage: "Delete uploaded app/ipa/apk file",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "app_file_number, a",
Usage: "File number of the uploaded file",
},
}...),
Action: deleteAppAction,
},
{
Name: "get-screenshots",
Usage: "Download screenshots for a batch run",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "batch_run_number, b",
Usage: "Batch run number",
},
cli.StringFlag{
Name: "download_path, d",
Usage: "Download destination file path. If empty string is speficied, the path will be ./screenshots.zip",
},
cli.StringFlag{
Name: "file_index_type, i",
Usage: "'line_number' or 'auto_increment'. If empty string is specified, the type will be 'line_number'",
},
cli.StringFlag{
Name: "file_name_body_type, B",
Usage: "'none' or 'screenshot_name'. If empty string is specified, the type will be 'none'",
},
cli.StringFlag{
Name: "download_type, D",
Usage: "'all' or 'command_only' (i.e. screenshots only for 'Take screenshot' command). If empty string is specified, the type will be 'all'",
},
cli.BoolFlag{
Name: "mask_dynamically_changed_area, m",
Usage: "Mask dynamically changed areas which can cause unexpected image difference between each test",
},
cli.IntFlag{
Name: "wait_limit, w",
Usage: "Wait limit in seconds. The default value is 300",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Not output any logs during download. Disabled by default",
},
}...),
Action: getScrenshotsAction,
},
{
Name: "wait-for-batch-run",
Usage: "Wait until a batch run ends",
Flags: append(commonFlags(), []cli.Flag{
cli.IntFlag{
Name: "batch_run_number, b",
Usage: "Batch run number",
},
cli.IntFlag{
Name: "wait_limit, w",
Usage: "Wait limit in seconds. If 0 is specified, the value is test count x 10 minutes",
},
}...),
Action: waitForBatchRunAction,
},
}
app.Run(os.Args)
}
func getBatchRunAction(c *cli.Context) error {
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
batchRunNumber := c.Int("batch_run_number")
if batchRunNumber == 0 {
return cli.NewExitError("--batch_run_number option is not specified or 0", 1)
}
batchRun, exitErr := common.GetBatchRun(urlBase, apiToken, organization, project, httpHeadersMap, batchRunNumber)
if exitErr != nil {
return exitErr
}
b, err := json.Marshal(batchRun)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
func getBatchRunsAction(c *cli.Context) error {
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
count := c.Int("count")
maxBatchRunNumber := c.Int("max_batch_run_number")
minBatchRunNumber := c.Int("min_batch_run_number")
if maxBatchRunNumber != 0 && minBatchRunNumber != 0 && maxBatchRunNumber < minBatchRunNumber {
return cli.NewExitError("--max_batch_run_number value is smaller than --min_batch_run_number value", 1)
}
batchRuns, exitErr := common.GetBatchRuns(urlBase, apiToken, organization, project, httpHeadersMap, count, maxBatchRunNumber, minBatchRunNumber)
if exitErr != nil {
return exitErr
}
b, err := json.Marshal(batchRuns)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
func latestBatchRunNoAction(c *cli.Context) error {
// handle command line arguments
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
batchRunNo, exitErr := common.LatestBatchRunNo(urlBase, apiToken, organization, project, httpHeadersMap)
if exitErr != nil {
return exitErr
}
fmt.Printf("%d\n", batchRunNo)
return nil
}
func uploadAppAction(c *cli.Context) error {
// handle command line arguments
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
appPath := c.String("app_path")
if appPath == "" {
return cli.NewExitError("--app_path option is required", 1)
}
fileNo, exitErr := common.UploadApp(urlBase, apiToken, organization, project, httpHeadersMap, appPath)
if exitErr != nil {
return exitErr
}
fmt.Printf("%d\n", fileNo)
return nil
}
func deleteAppAction(c *cli.Context) error {
// handle command line arguments
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
appFileNumber := c.Int("app_file_number")
if appFileNumber == 0 {
return cli.NewExitError("--app_file_number option is not specified or 0", 1)
}
exitErr := common.DeleteApp(urlBase, apiToken, organization, project, httpHeadersMap, appFileNumber)
if exitErr != nil {
return exitErr
}
return nil
}
func getScrenshotsAction(c *cli.Context) error {
// handle command line arguments
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
batchRunNumber := c.Int("batch_run_number")
if batchRunNumber == 0 {
return cli.NewExitError("--batch_run_number option is not specified or 0", 1)
}
downloadPath := c.String("download_path")
if downloadPath == "" {
curDir, err := os.Getwd()
if err != nil {
panic(err)
}
downloadPath = filepath.Join(curDir, "screenshots.zip")
} else {
stat, err := os.Stat(downloadPath)
if err == nil {
// downloadPath already exists
mode := stat.Mode()
if mode.IsDir() {
return cli.NewExitError(fmt.Sprintf("'%s' should be not a directory but a file", downloadPath), 1)
}
}
}
downloadPath, err = filepath.Abs(downloadPath)
if err != nil {
panic(err)
}
fileIndexType := c.String("file_index_type")
if fileIndexType == "" {
fileIndexType = "line_number"
}
fileNameBodyType := c.String("file_name_body_type")
if fileNameBodyType == "" {
fileNameBodyType = "none"
}
downloadType := c.String("download_type")
if downloadType == "" {
downloadType = "all"
}
maskDynamicallyChangedArea := c.Bool("mask_dynamically_changed_area")
waitLimit := c.Int("wait_limit")
if waitLimit == 0 {
waitLimit = -1
}
quiet := c.Bool("quiet")
exitErr := common.GetScreenshots(urlBase, apiToken, organization, project, httpHeadersMap, batchRunNumber, downloadPath, fileIndexType, fileNameBodyType, downloadType, maskDynamicallyChangedArea, waitLimit, !quiet)
if exitErr != nil {
return exitErr
}
return nil
}
func batchRunAction(c *cli.Context) error {
// handle command line arguments
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
testSettingsNumber := c.Int("test_settings_number")
setting := c.String("setting")
if testSettingsNumber == 0 && setting == "" {
return cli.NewExitError("Either of --test_settings_number or --setting option is required", 1)
}
noWait := c.Bool("no_wait")
waitLimit := c.Int("wait_limit")
_, existsErr, existsUnresolved, batchRunError := common.ExecuteBatchRun(urlBase, apiToken, organization,
project, httpHeadersMap, testSettingsNumber, setting, !noWait, waitLimit, true)
if batchRunError != nil {
return batchRunError
}
if existsErr {
return cli.NewExitError("", 1)
}
if existsUnresolved {
return cli.NewExitError("", 2)
}
return nil
}
func waitForBatchRunAction(c *cli.Context) error {
urlBase, apiToken, organization, project, httpHeadersMap, err := parseCommonFlags(c)
if err != nil {
return err
}
batchRunNumber := c.Int("batch_run_number")
if batchRunNumber == 0 {
return cli.NewExitError("--batch_run_number option is not specified or 0", 1)
}
waitLimit := c.Int("wait_limit")
batchRunUnderProgress, batchRunError := common.GetBatchRun(urlBase, apiToken, organization, project, httpHeadersMap, batchRunNumber)
if batchRunError != nil {
return batchRunError
}
_, existsErr, existsUnresolved, batchRunError := common.WaitForBatchRunResult(urlBase, apiToken, organization,
project, httpHeadersMap, batchRunUnderProgress, waitLimit, true)
if batchRunError != nil {
return batchRunError
}
if existsErr {
return cli.NewExitError("", 1)
}
if existsUnresolved {
return cli.NewExitError("", 2)
}
return nil
}
func commonFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "token, t",
Usage: "API token. You can get the value from https://app.magicpod.com/accounts/api-token/",
EnvVar: "MAGICPOD_API_TOKEN",
},
cli.StringFlag{
Name: "organization, o",
Usage: "Organization name. (Not \"organization display name\", be careful!)",
EnvVar: "MAGICPOD_ORGANIZATION",
},
cli.StringFlag{
Name: "project, p",
Usage: "Project name. (Not \"project display name\", be careful!)",
EnvVar: "MAGICPOD_PROJECT",
},
cli.StringFlag{
Name: "http_headers, H",
Usage: "Additional HTTP headers in JSON string format (e.g.: '{\"accept-language\":\"ja\"}')",
},
}
}
func parseCommonFlags(c *cli.Context) (string, string, string, string, map[string]string, error) {
urlBase := c.GlobalString("url-base")
apiToken := c.String("token")
organization := c.String("organization")
project := c.String("project")
httpHeadersMap := make(map[string]string)
var err error
if urlBase == "" {
err = cli.NewExitError("url-base argument cannot be empty", 1)
} else if apiToken == "" {
err = cli.NewExitError("--token option is required", 1)
} else if organization == "" {
err = cli.NewExitError("--organization option is required", 1)
} else if project == "" {
err = cli.NewExitError("--project option is required", 1)
} else {
err = nil
httpHeadersStr := c.String("http_headers")
if httpHeadersStr != "" {
err = json.Unmarshal([]byte(httpHeadersStr), &httpHeadersMap)
if err != nil {
err = cli.NewExitError("http headers must be in JSON string format whose keys and values are string", 1)
}
}
}
return urlBase, apiToken, organization, project, httpHeadersMap, err
}