Skip to content

Commit 7c46f4b

Browse files
authored
Merge pull request #721 from browserstack/build-artifacts-404-fix
Avoid throwing exit error code if we get 404 for build artifacts
2 parents f0625f8 + fdbc23f commit 7c46f4b

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

bin/helpers/buildArtifacts.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const decompress = require('decompress');
1515
let BUILD_ARTIFACTS_TOTAL_COUNT = 0;
1616
let BUILD_ARTIFACTS_FAIL_COUNT = 0;
1717

18-
const parseAndDownloadArtifacts = async (buildId, data) => {
18+
const parseAndDownloadArtifacts = async (buildId, data, bsConfig, args, rawArgs, buildReportData) => {
1919
return new Promise(async (resolve, reject) => {
2020
let all_promises = [];
2121
let combs = Object.keys(data);
@@ -28,7 +28,14 @@ const parseAndDownloadArtifacts = async (buildId, data) => {
2828
let fileName = 'build_artifacts.zip';
2929
BUILD_ARTIFACTS_TOTAL_COUNT += 1;
3030
all_promises.push(downloadAndUnzip(filePath, fileName, data[comb][sessionId]).catch((error) => {
31-
BUILD_ARTIFACTS_FAIL_COUNT += 1;
31+
if (error === Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_NOT_FOUND) {
32+
// Don't consider build artifact 404 error as a failure
33+
let warningMessage = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_NOT_FOUND.replace('<session-id>', sessionId);
34+
logger.warn(warningMessage);
35+
utils.sendUsageReport(bsConfig, args, warningMessage, Constants.messageTypes.ERROR, 'build_artifacts_not_found', buildReportData, rawArgs);
36+
} else {
37+
BUILD_ARTIFACTS_FAIL_COUNT += 1;
38+
}
3239
// delete malformed zip if present
3340
let tmpFilePath = path.join(filePath, fileName);
3441
if(fs.existsSync(tmpFilePath)){
@@ -99,6 +106,9 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
99106
request.get(url).on('response', function(response) {
100107

101108
if(response.statusCode != 200) {
109+
if (response.statusCode === 404) {
110+
reject(Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_NOT_FOUND);
111+
}
102112
reject();
103113
} else {
104114
//ensure that the user can call `then()` only when the file has
@@ -220,7 +230,7 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs, buildR
220230
process.exitCode = Constants.ERROR_EXIT_CODE;
221231
} else {
222232
await createDirectories(buildId, buildDetails);
223-
await parseAndDownloadArtifacts(buildId, buildDetails);
233+
await parseAndDownloadArtifacts(buildId, buildDetails, bsConfig, args, rawArgs, buildReportData);
224234
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
225235
messageType = Constants.messageTypes.ERROR;
226236
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);

bin/helpers/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ const userMessages = {
9494
SPEC_LIMIT_WARNING:
9595
"You might not see all your results on the dashboard because of high spec count, please consider reducing the number of spec files in this folder.",
9696
DOWNLOAD_BUILD_ARTIFACTS_FAILED:
97-
"Downloading build artifacts for the build <build-id> failed for <machine-count> machines.",
97+
"Downloading build artifact(s) for the build <build-id> failed for <machine-count> machines.",
98+
DOWNLOAD_BUILD_ARTIFACTS_NOT_FOUND:
99+
"Build artifact(s) for the session <session-id> was either not generated or not uploaded.",
98100
ASYNC_DOWNLOADS:
99101
"Test artifacts as specified under 'downloads' can be downloaded after the build has completed its run, using 'browserstack-cypress generate-downloads <build-id>'",
100102
DOWNLOAD_BUILD_ARTIFACTS_SUCCESS:

bin/helpers/utils.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,25 +1314,29 @@ exports.readBsConfigJSON = (bsConfigPath) => {
13141314
}
13151315

13161316
exports.getCypressConfigFile = (bsConfig) => {
1317-
let cypressConfigFile = undefined;
1318-
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
1319-
if (bsConfig.run_settings.cypress_config_filename.endsWith("cypress.config.js")) {
1317+
try {
1318+
let cypressConfigFile = undefined;
1319+
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
1320+
if (bsConfig.run_settings.cypress_config_filename.endsWith("cypress.config.js")) {
1321+
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1322+
cypressConfigFile = require(path.resolve(bsConfig.run_settings.cypressConfigFilePath));
1323+
} else if (bsConfig.run_settings.cypressProjectDir) {
1324+
cypressConfigFile = require(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename));
1325+
}
1326+
} else {
1327+
cypressConfigFile = {};
1328+
}
1329+
} else {
13201330
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1321-
cypressConfigFile = require(path.resolve(bsConfig.run_settings.cypressConfigFilePath));
1331+
cypressConfigFile = JSON.parse(fs.readFileSync(bsConfig.run_settings.cypressConfigFilePath))
13221332
} else if (bsConfig.run_settings.cypressProjectDir) {
1323-
cypressConfigFile = require(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename));
1333+
cypressConfigFile = JSON.parse(fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename)));
13241334
}
1325-
} else {
1326-
cypressConfigFile = {};
1327-
}
1328-
} else {
1329-
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1330-
cypressConfigFile = JSON.parse(fs.readFileSync(bsConfig.run_settings.cypressConfigFilePath))
1331-
} else if (bsConfig.run_settings.cypressProjectDir) {
1332-
cypressConfigFile = JSON.parse(fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename)));
13331335
}
1336+
return cypressConfigFile;
1337+
} catch (err) {
1338+
return {}
13341339
}
1335-
return cypressConfigFile;
13361340
}
13371341

13381342
exports.setCLIMode = (bsConfig, args) => {

0 commit comments

Comments
 (0)