diff --git a/README.md b/README.md index 35f6ba5d..7f173511 100644 --- a/README.md +++ b/README.md @@ -102,13 +102,13 @@ Output : In case you want to get information on the build you can use the following command ```bash -browserstack-cypress build +browserstack-cypress build-info ``` Example ```bash -browserstack-cypress build 06f28ce423d10314b32e98bb6f68e10b0d02a49a +browserstack-cypress build-info 06f28ce423d10314b32e98bb6f68e10b0d02a49a ``` Output: @@ -175,6 +175,30 @@ Output: **Note** that individual version represents a session. It is advised to validate your account's parallel before running multiple versions. +### Stopping a running build +In case you want to stop a running build, you can use the following command + +```bash +browserstack-cypress build-stop +``` + +Example + +```bash +browserstack-cypress build-stop 06f28ce423d10314b32e98bb6f68e10b0d02a49a +``` + +Output: + +```bash +[3/24/2020, 2:31:11 PM] Stopping build with given buildId 06f28ce423d10314b32e98bb6f68e10b0d02a49a +[3/24/2020, 2:31:12 PM] Reading config from /browserstack.json +[3/24/2020, 2:31:14 PM] { + "message": "stopped 1 sessions", + "stopped_session_count": 1 +} +``` + ### Limitations - `exec` and `task` are not allowed. diff --git a/bin/commands/stop.js b/bin/commands/stop.js new file mode 100644 index 00000000..cb82b74b --- /dev/null +++ b/bin/commands/stop.js @@ -0,0 +1,55 @@ +'use strict'; +var config = require('../helpers/config'); +var request = require('request') +var logger = require("../helpers/logger"); +var Constant = require("../helpers/constants") + +module.exports = function stop(args) { + return buildStop(args) +} + +function buildStop(args) { + let bsConfigPath = process.cwd() + args.cf; + logger.log(`Reading config from ${args.cf}`); + var bsConfig = require(bsConfigPath); + + let buildId = args._[1] + + let options = { + url: config.buildStopUrl + buildId, + method: 'POST', + auth: { + user: bsConfig.auth.username, + password: bsConfig.auth.access_key + } + } + + request(options, function (err, resp, body) { + if (err) { + logger.log(Constant.userMessages.BUILD_STOP_FAILED); + } else { + let build = null + try { + build = JSON.parse(body) + } catch (error) { + build = null + } + + if (resp.statusCode != 200) { + if (build) { + logger.error(`${Constant.userMessages.BUILD_STOP_FAILED} with error: \n${JSON.stringify(build, null, 2)}`); + } else { + logger.error(Constant.userMessages.BUILD_STOP_FAILED); + } + } else if (resp.statusCode == 299) { + if (build) { + logger.log(build.message); + } else { + logger.log(Constants.userMessages.API_DEPRECATED); + } + } else { + logger.log(`${JSON.stringify(build, null, 2)}`) + } + } + }) +} diff --git a/bin/helpers/config.js b/bin/helpers/config.js index a5251f08..75eb084b 100644 --- a/bin/helpers/config.js +++ b/bin/helpers/config.js @@ -10,5 +10,6 @@ config.uploadUrl = hosts[config.env].uploadUrl; config.rails_host = hosts[config.env].rails_host; config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`; config.buildUrl = `${config.cypress_v1}/builds/`; +config.buildStopUrl = `${config.cypress_v1}/builds/stop/`; config.fileName = "tests.zip"; module.exports = config; diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index fbffbbaa..279e4085 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -2,6 +2,7 @@ const userMessages = { BUILD_FAILED: "Build creation failed.", BUILD_CREATED: "Build created", BUILD_INFO_FAILED: "Failed to get build info.", + BUILD_STOP_FAILED: "Failed to stop build.", ZIP_UPLOADER_NOT_REACHABLE: "Could not reach to zip uploader.", ZIP_UPLOAD_FAILED: "Zip Upload failed.", CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run", @@ -34,10 +35,12 @@ const cliMessages = { }, BUILD: { INFO: "Check status of your build.", + STOP: "Stop your build.", DEMAND: "Requires a build id.", DESC: "Path to BrowserStack config", CONFIG_DEMAND: "config file is required", - DISPLAY: "Getting information for buildId " + INFO_MESSAGE: "Getting information for buildId ", + STOP_MESSAGE: "Stopping build with given buildId " }, RUN: { INFO: "Run your tests on BrowserStack.", diff --git a/bin/runner.js b/bin/runner.js index 41785c0c..2985dbfc 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -37,9 +37,9 @@ var argv = yargs return require('./commands/init')(argv); } }) - .command('build', Constants.cliMessages.BUILD.INFO, function(yargs) { + .command('build-info', Constants.cliMessages.BUILD.INFO, function(yargs) { argv = yargs - .usage('usage: $0 info ') + .usage('usage: $0 ') .demand(1, Constants.cliMessages.BUILD.DEMAND) .options('cf', { alias: 'config-file', @@ -54,10 +54,31 @@ var argv = yargs .wrap(null) .argv if (checkCommands(yargs, argv, 1)) { - logger.log(Constants.cliMessages.BUILD.DISPLAY + argv._[1]); + logger.log(Constants.cliMessages.BUILD.INFO_MESSAGE + argv._[1]); return require('./commands/info')(argv); } }) + .command('build-stop', Constants.cliMessages.BUILD.STOP, function (yargs) { + argv = yargs + .usage('usage: $0 ') + .demand(1, Constants.cliMessages.BUILD.DEMAND) + .options('cf', { + alias: 'config-file', + describe: Constants.cliMessages.BUILD.DESC, + default: '/browserstack.json', + type: 'string', + nargs: 1, + demand: true, + demand: Constants.cliMessages.BUILD.CONFIG_DEMAND + }) + .help('help') + .wrap(null) + .argv + if (checkCommands(yargs, argv, 1)) { + logger.log(Constants.cliMessages.BUILD.STOP_MESSAGE + argv._[1]); + return require('./commands/stop')(argv); + } + }) .command('run', Constants.cliMessages.RUN.INFO, function(yargs) { argv = yargs .usage('usage: $0 build') diff --git a/package.json b/package.json index ed917d05..29363418 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-cypress-cli", - "version": "1.0.1", + "version": "1.1.0", "description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.", "main": "index.js", "scripts": {