Skip to content

Give build stop functionality using CLI #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <buildId>
browserstack-cypress build-info <buildId>
```

Example

```bash
browserstack-cypress build 06f28ce423d10314b32e98bb6f68e10b0d02a49a
browserstack-cypress build-info 06f28ce423d10314b32e98bb6f68e10b0d02a49a
```

Output:
Expand Down Expand Up @@ -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 <buildId>
```

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.
Expand Down
55 changes: 55 additions & 0 deletions bin/commands/stop.js
Original file line number Diff line number Diff line change
@@ -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)}`)
}
}
})
}
1 change: 1 addition & 0 deletions bin/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 4 additions & 1 deletion bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down
27 changes: 24 additions & 3 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we changing this command name? We will have to update the documentation as well for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

argv = yargs
.usage('usage: $0 info <buildId>')
.usage('usage: $0 <buildId>')
.demand(1, Constants.cliMessages.BUILD.DEMAND)
.options('cf', {
alias: 'config-file',
Expand All @@ -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 <buildId>')
.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')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down