Skip to content
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

Adding required instrumentation #14

Merged
merged 14 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 12 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
123 changes: 81 additions & 42 deletions bin/commands/info.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,94 @@
'use strict';
var config = require('../helpers/config');
var request = require('request')
var logger = require("../helpers/logger");
var Constant = require("../helpers/constants")
const request = require('request');

const config = require("../helpers/config"),
logger = require("../helpers/logger").winstonLogger,
Constants = require("../helpers/constants"),
util = require("../helpers/util");

module.exports = function info(args) {
return buildInfo(args)
}

function buildInfo(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.buildUrl + buildId,
method: 'GET',
auth: {
user: bsConfig.auth.username,
password: bsConfig.auth.access_key
}
}

request(options, function (err, resp, body) {
if (err) {
logger.log(Constant.userMessages.BUILD_INFO_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_INFO_FAILED} with error: \n${JSON.stringify(build, null, 2)}`);
} else {
logger.error(Constant.userMessages.BUILD_INFO_FAILED);

util.validateBstackJson(bsConfigPath).then(function (bsConfig) {
util.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

let buildId = args._[1];

let options = {
url: config.buildUrl + buildId,
method: "GET",
auth: {
user: bsConfig.auth.username,
password: bsConfig.auth.access_key,
},
headers: {
"User-Agent": util.getUserAgent(),
},
};

request(options, function (err, resp, body) {
let message = null;
let messageType = null;
let errorCode = null;

if (err) {
message = Constants.userMessages.BUILD_INFO_FAILED;
messageType = Constants.messageTypes.ERROR;
errorCode = 'api_failed_build_info';

logger.info(message);
} else {
let build = null;
try {
build = JSON.parse(body);
} catch (error) {
build = null;
}
} else if(resp.statusCode == 299) {
if(build) {
logger.log(build.message);

if (resp.statusCode == 299) {
messageType = Constants.messageTypes.INFO;
errorCode = "api_deprecated";

if (build) {
message = build.message;
logger.info(message);
} else {
message = Constants.userMessages.API_DEPRECATED;
logger.info(message);
}
} else if (resp.statusCode != 200) {
messageType = Constants.messageTypes.ERROR;
errorCode = "api_failed_build_info";

if (build) {
message = `${
Constants.userMessages.BUILD_INFO_FAILED
} with error: \n${JSON.stringify(build, null, 2)}`;
logger.error(message);
if (build.message === "Unauthorized") errorCode = "api_auth_failed";
} else {
message = Constants.userMessages.BUILD_INFO_FAILED;
logger.error(message);
}
} else {
logger.log(Constants.userMessages.API_DEPRECATED);
messageType = Constants.messageTypes.SUCCESS;
message = `Build info for build id: \n ${JSON.stringify(
build,
null,
2
)}`;
logger.info(message);
}
} else {
logger.log(`Build info for build id: \n ${JSON.stringify(build, null, 2)}`)
}
}
})
util.sendUsageReport(bsConfig, args, message, messageType, errorCode);
})
}).catch(function (err) {
logger.error(err);
util.setUsageReportingFlag(null, args.disableUsageReporting);
util.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, util.getErrorCodeFromErr(err));
})
}
15 changes: 10 additions & 5 deletions bin/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var fileHelpers = require('../helpers/fileHelpers');
const Constants = require('../helpers/constants');
var logger = require("../helpers/logger");
const fileHelpers = require("../helpers/fileHelpers"),
Constants = require("../helpers/constants"),
logger = require("../helpers/logger").winstonLogger,
util = require("../helpers/util");

module.exports = function init(args) {
return createBrowserStackConfig(args)
Expand All @@ -21,12 +22,16 @@ function createBrowserStackConfig(args) {
};

function allDone() {
logger.log(Constants.userMessages.CONFIG_FILE_CREATED);
let message = Constants.userMessages.CONFIG_FILE_CREATED
logger.info(message);
util.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
}

return fileHelpers.fileExists(config.path, function(exists){
if (exists) {
logger.error(Constants.userMessages.CONFIG_FILE_EXISTS);
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
logger.error(message);
util.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
} else {
fileHelpers.write(config, null, allDone);
}
Expand Down
101 changes: 62 additions & 39 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';
var archiver = require("../helpers/archiver");
var zipUploader = require("../helpers/zipUpload");
var build = require("../helpers/build");
var logger = require("../helpers/logger");
var config = require('../helpers/config');
var capabilityHelper = require("../helpers/capabilityHelper");
var fs = require('fs');
const Constants = require('../helpers/constants');
const fs = require('fs');

const archiver = require("../helpers/archiver"),
zipUploader = require("../helpers/zipUpload"),
build = require("../helpers/build"),
logger = require("../helpers/logger").winstonLogger,
config = require("../helpers/config"),
capabilityHelper = require("../helpers/capabilityHelper"),
Constants = require("../helpers/constants"),
util = require("../helpers/util");

module.exports = function run(args) {
return runCypress(args);
Expand All @@ -15,48 +17,69 @@ module.exports = function run(args) {
function deleteZip() {
fs.unlink(config.fileName, function (err) {
if(err) {
logger.log(Constants.userMessages.ZIP_DELETE_FAILED);
logger.info(Constants.userMessages.ZIP_DELETE_FAILED);
} else {
logger.log(Constants.userMessages.ZIP_DELETED);
}
logger.info(Constants.userMessages.ZIP_DELETED);
}
});
}

function runCypress(args) {
let bsConfigPath = process.cwd() + args.cf;
logger.log(`Reading config from ${args.cf}`);
var bsConfig = require(bsConfigPath);

// Validate browserstack.json
capabilityHelper.validate(bsConfig).then(function (validated) {
logger.log(validated);
// Archive the spec files
archiver.archive(bsConfig.run_settings, config.fileName).then(function (data) {
// Uploaded zip file
zipUploader.zipUpload(bsConfig, config.fileName).then(function (zip) {
// Create build
build.createBuild(bsConfig, zip).then(function (data) {
return;

util.validateBstackJson(bsConfigPath).then(function (bsConfig) {
util.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

// Validate browserstack.json values
capabilityHelper.validate(bsConfig).then(function (validated) {
logger.info(validated);

// Archive the spec files
archiver.archive(bsConfig.run_settings, config.fileName).then(function (data) {

// Uploaded zip file
zipUploader.zipUpload(bsConfig, config.fileName).then(function (zip) {

// Create build
build.createBuild(bsConfig, zip).then(function (message) {
logger.info(message);
util.sendUsageReport(bsConfig, args, message, Constants.messageTypes.SUCCESS, null);
return;
}).catch(function (err) {
// Build creation failed
logger.error(err);
util.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'build_failed');
});
}).catch(function (err) {
// Build creation failed
logger.error(Constants.userMessages.BUILD_FAILED)
// Zip Upload failed
logger.error(err)
logger.error(Constants.userMessages.ZIP_UPLOAD_FAILED)
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.ZIP_UPLOAD_FAILED}`, Constants.messageTypes.ERROR, 'zip_upload_failed');
}).finally(function () {
deleteZip();
});
}).catch(function (err) {
// Zip Upload failed
logger.error(err)
logger.error(Constants.userMessages.ZIP_UPLOAD_FAILED)
}).finally(function () {
deleteZip();
// Zipping failed
logger.error(err);
logger.error(Constants.userMessages.FAILED_TO_ZIP);
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.FAILED_TO_ZIP}`, Constants.messageTypes.ERROR, 'zip_creation_failed');
try {
deleteZip();
} catch (err) {
util.sendUsageReport(bsConfig, args, Constants.userMessages.ZIP_DELETE_FAILED, Constants.messageTypes.ERROR, 'zip_deletion_failed');
}
});
}).catch(function (err) {
// Zipping failed
logger.error(err)
logger.error(Constants.userMessages.FAILED_TO_ZIP)
deleteZip();
// browerstack.json is not valid
logger.error(err);
logger.error(Constants.validationMessages.NOT_VALID);

let error_code = util.getErrorCodeFromMsg(err);
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.validationMessages.NOT_VALID}`, Constants.messageTypes.ERROR, error_code);
});
}).catch(function (err) {
// browerstack.json is not valid
logger.error(err)
logger.error(Constants.validationMessages.NOT_VALID)
});
logger.error(err);
util.setUsageReportingFlag(null, args.disableUsageReporting);
util.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, util.getErrorCodeFromErr(err));
})
}
Loading