Skip to content

Commit 53c2954

Browse files
authored
Merge pull request #8 from browserstack/CYP_59_build_stop
Give build stop functionality using CLI
2 parents 4b8fc5c + 40f4fcc commit 53c2954

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ Sample output :
109109
In case you want to get information on the build you can use the following command
110110

111111
```bash
112-
browserstack-cypress build <buildId>
112+
browserstack-cypress build-info <buildId>
113113
```
114114

115115
Example
116116

117117
```bash
118-
browserstack-cypress build 06f28ce423d10314b32e98bb6f68e10b0d02a49a
118+
browserstack-cypress build-info 06f28ce423d10314b32e98bb6f68e10b0d02a49a
119119
```
120120

121121
Output:
@@ -182,6 +182,30 @@ Output:
182182

183183
**Note:** Each browser version represents a session. It is advised to validate your account's parallel limit before running multiple versions.
184184

185+
### Stopping a running build
186+
In case you want to stop a running build, you can use the following command
187+
188+
```bash
189+
browserstack-cypress build-stop <buildId>
190+
```
191+
192+
Example
193+
194+
```bash
195+
browserstack-cypress build-stop 06f28ce423d10314b32e98bb6f68e10b0d02a49a
196+
```
197+
198+
Output:
199+
200+
```bash
201+
[3/24/2020, 2:31:11 PM] Stopping build with given buildId 06f28ce423d10314b32e98bb6f68e10b0d02a49a
202+
[3/24/2020, 2:31:12 PM] Reading config from /browserstack.json
203+
[3/24/2020, 2:31:14 PM] {
204+
"message": "stopped 1 sessions",
205+
"stopped_session_count": 1
206+
}
207+
```
208+
185209
### Limitations
186210

187211
- `exec` and `task` are not allowed.

bin/commands/stop.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
var config = require('../helpers/config');
3+
var request = require('request')
4+
var logger = require("../helpers/logger");
5+
var Constant = require("../helpers/constants")
6+
7+
module.exports = function stop(args) {
8+
return buildStop(args)
9+
}
10+
11+
function buildStop(args) {
12+
let bsConfigPath = process.cwd() + args.cf;
13+
logger.log(`Reading config from ${args.cf}`);
14+
var bsConfig = require(bsConfigPath);
15+
16+
let buildId = args._[1]
17+
18+
let options = {
19+
url: config.buildStopUrl + buildId,
20+
method: 'POST',
21+
auth: {
22+
user: bsConfig.auth.username,
23+
password: bsConfig.auth.access_key
24+
}
25+
}
26+
27+
request(options, function (err, resp, body) {
28+
if (err) {
29+
logger.log(Constant.userMessages.BUILD_STOP_FAILED);
30+
} else {
31+
let build = null
32+
try {
33+
build = JSON.parse(body)
34+
} catch (error) {
35+
build = null
36+
}
37+
38+
if (resp.statusCode != 200) {
39+
if (build) {
40+
logger.error(`${Constant.userMessages.BUILD_STOP_FAILED} with error: \n${JSON.stringify(build, null, 2)}`);
41+
} else {
42+
logger.error(Constant.userMessages.BUILD_STOP_FAILED);
43+
}
44+
} else if (resp.statusCode == 299) {
45+
if (build) {
46+
logger.log(build.message);
47+
} else {
48+
logger.log(Constants.userMessages.API_DEPRECATED);
49+
}
50+
} else {
51+
logger.log(`${JSON.stringify(build, null, 2)}`)
52+
}
53+
}
54+
})
55+
}

bin/helpers/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ config.uploadUrl = hosts[config.env].uploadUrl;
1010
config.rails_host = hosts[config.env].rails_host;
1111
config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`;
1212
config.buildUrl = `${config.cypress_v1}/builds/`;
13+
config.buildStopUrl = `${config.cypress_v1}/builds/stop/`;
1314
config.fileName = "tests.zip";
1415
module.exports = config;

bin/helpers/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const userMessages = {
22
BUILD_FAILED: "Build creation failed.",
33
BUILD_CREATED: "Build created",
44
BUILD_INFO_FAILED: "Failed to get build info.",
5+
BUILD_STOP_FAILED: "Failed to stop build.",
56
ZIP_UPLOADER_NOT_REACHABLE: "Could not reach to zip uploader.",
67
ZIP_UPLOAD_FAILED: "Zip Upload failed.",
78
CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run",
@@ -35,10 +36,12 @@ const cliMessages = {
3536
},
3637
BUILD: {
3738
INFO: "Check status of your build.",
39+
STOP: "Stop your build.",
3840
DEMAND: "Requires a build id.",
3941
DESC: "Path to BrowserStack config",
4042
CONFIG_DEMAND: "config file is required",
41-
DISPLAY: "Getting information for buildId "
43+
INFO_MESSAGE: "Getting information for buildId ",
44+
STOP_MESSAGE: "Stopping build with given buildId "
4245
},
4346
RUN: {
4447
INFO: "Run your tests on BrowserStack.",

bin/runner.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ var argv = yargs
3737
return require('./commands/init')(argv);
3838
}
3939
})
40-
.command('build', Constants.cliMessages.BUILD.INFO, function(yargs) {
40+
.command('build-info', Constants.cliMessages.BUILD.INFO, function(yargs) {
4141
argv = yargs
42-
.usage('usage: $0 info <buildId>')
42+
.usage('usage: $0 <buildId>')
4343
.demand(1, Constants.cliMessages.BUILD.DEMAND)
4444
.options('cf', {
4545
alias: 'config-file',
@@ -54,10 +54,31 @@ var argv = yargs
5454
.wrap(null)
5555
.argv
5656
if (checkCommands(yargs, argv, 1)) {
57-
logger.log(Constants.cliMessages.BUILD.DISPLAY + argv._[1]);
57+
logger.log(Constants.cliMessages.BUILD.INFO_MESSAGE + argv._[1]);
5858
return require('./commands/info')(argv);
5959
}
6060
})
61+
.command('build-stop', Constants.cliMessages.BUILD.STOP, function (yargs) {
62+
argv = yargs
63+
.usage('usage: $0 <buildId>')
64+
.demand(1, Constants.cliMessages.BUILD.DEMAND)
65+
.options('cf', {
66+
alias: 'config-file',
67+
describe: Constants.cliMessages.BUILD.DESC,
68+
default: '/browserstack.json',
69+
type: 'string',
70+
nargs: 1,
71+
demand: true,
72+
demand: Constants.cliMessages.BUILD.CONFIG_DEMAND
73+
})
74+
.help('help')
75+
.wrap(null)
76+
.argv
77+
if (checkCommands(yargs, argv, 1)) {
78+
logger.log(Constants.cliMessages.BUILD.STOP_MESSAGE + argv._[1]);
79+
return require('./commands/stop')(argv);
80+
}
81+
})
6182
.command('run', Constants.cliMessages.RUN.INFO, function(yargs) {
6283
argv = yargs
6384
.usage('usage: $0 build')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-cypress-cli",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)