From e62a681bea2f1ac49c7dd20543e963275ddc08e9 Mon Sep 17 00:00:00 2001 From: nagpalkaran95 Date: Tue, 24 Mar 2020 14:38:06 +0530 Subject: [PATCH 1/5] Give build stop functionality using CLI --- bin/commands/stop.js | 55 ++++++++++++++++++++++++++++++++++++++++ bin/helpers/config.js | 1 + bin/helpers/constants.js | 5 +++- bin/runner.js | 27 +++++++++++++++++--- 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 bin/commands/stop.js 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..757e5c6a 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') From 7d937e417391c7bc53899d139aede979c41eaf3c Mon Sep 17 00:00:00 2001 From: nagpalkaran95 Date: Tue, 24 Mar 2020 17:11:17 +0530 Subject: [PATCH 2/5] Add trailing space in display message --- bin/helpers/constants.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 757e5c6a..279e4085 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -39,8 +39,8 @@ const cliMessages = { DEMAND: "Requires a build id.", DESC: "Path to BrowserStack config", CONFIG_DEMAND: "config file is required", - INFO_MESSAGE: "Getting information for buildId", - STOP_MESSAGE: "Stopping build with given buildId" + INFO_MESSAGE: "Getting information for buildId ", + STOP_MESSAGE: "Stopping build with given buildId " }, RUN: { INFO: "Run your tests on BrowserStack.", From 34983b09b269ab959fc91f9c6e1bf29c356d3c41 Mon Sep 17 00:00:00 2001 From: nagpalkaran95 Date: Tue, 24 Mar 2020 17:17:24 +0530 Subject: [PATCH 3/5] Update readme with the build-stop command example --- README.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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. From d1af5f0bd18b603c8ecea2f2400e8bf8ed38e98b Mon Sep 17 00:00:00 2001 From: Karan Nagpal Date: Thu, 26 Mar 2020 11:48:43 +0530 Subject: [PATCH 4/5] version bump with build-stop command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ed917d05..c72051a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-cypress-cli", - "version": "1.0.1", + "version": "1.0.2", "description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.", "main": "index.js", "scripts": { From 40f4fcc097d0fb903d6a660a9f570db35987d91f Mon Sep 17 00:00:00 2001 From: Karan Nagpal Date: Thu, 26 Mar 2020 12:10:53 +0530 Subject: [PATCH 5/5] update minor version instead of patch version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c72051a5..29363418 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-cypress-cli", - "version": "1.0.2", + "version": "1.1.0", "description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.", "main": "index.js", "scripts": {