Skip to content

Commit ff3727d

Browse files
roshan04Karan Nagpal
authored andcommitted
following the approach of promises of request
1 parent d267eb5 commit ff3727d

File tree

2 files changed

+134
-117
lines changed

2 files changed

+134
-117
lines changed

bin/commands/runs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ module.exports = function run(args, rawArgs) {
177177

178178
// download build artifacts
179179
if (utils.nonEmptyArray(bsConfig.run_settings.downloads)) {
180-
await downloadBuildArtifacts(bsConfig, data.build_id, args, rawArgs);
180+
try {
181+
await downloadBuildArtifacts(bsConfig, data.build_id, args, rawArgs);
182+
} catch (err) {
183+
logger.error(err);
184+
}
181185
}
182186

183187
// Generate custom report!

bin/helpers/buildArtifacts.js

Lines changed: 129 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const logger = require('./logger').winstonLogger,
1212
config = require("./config");
1313

1414
const request = require('request');
15-
const { inspect } = require('util');
16-
const { reject } = require('async');
1715

1816

1917
let BUILD_ARTIFACTS_TOTAL_COUNT = 0;
@@ -100,26 +98,35 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
10098
const writer = fs.createWriteStream(tmpFilePath);
10199

102100
return new Promise(async (resolve, reject) => {
103-
request.get(url).on('response', function(response) {
104-
//ensure that the user can call `then()` only when the file has
105-
//been downloaded entirely.
106-
response.pipe(writer);
107-
let error = null;
108-
writer.on('error', err => {
109-
error = err;
110-
writer.close();
111-
reject(err);
112-
});
113-
writer.on('close', async () => {
114-
if (!error) {
115-
await unzipFile(filePath, fileName);
116-
fs.unlinkSync(tmpFilePath);
117-
resolve(true);
101+
try {
102+
request.get(url).on('response', function(response) {
103+
104+
if(response.statusCode != 200) {
105+
reject();
106+
} else {
107+
//ensure that the user can call `then()` only when the file has
108+
//been downloaded entirely.
109+
response.pipe(writer);
110+
let error = null;
111+
writer.on('error', err => {
112+
error = err;
113+
writer.close();
114+
reject(err);
115+
});
116+
writer.on('close', async () => {
117+
if (!error) {
118+
await unzipFile(filePath, fileName);
119+
fs.unlinkSync(tmpFilePath);
120+
resolve(true);
121+
}
122+
//no need to call the reject here, as it will have been called in the
123+
//'error' stream;
124+
});
118125
}
119-
//no need to call the reject here, as it will have been called in the
120-
//'error' stream;
121126
});
122-
});
127+
} catch (err) {
128+
reject(err);
129+
}
123130
});
124131
}
125132

@@ -133,120 +140,126 @@ const unzipFile = async (filePath, fileName) => {
133140
}
134141

135142
const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs) => {
136-
options.url = `${config.buildUrl}${buildId}/build_artifacts/status`;
143+
options.url = `${config.buildUrl}${buildId}/build_artifacts/status`;
137144

138-
let cypressJSON = utils.getCypressJSON(bsConfig);
145+
let cypressJSON = utils.getCypressJSON(bsConfig);
139146

140-
let reporter = null;
141-
if(!utils.isUndefined(args.reporter)) {
142-
reporter = args.reporter;
143-
} else if(cypressJSON !== undefined){
144-
reporter = cypressJSON.reporter;
145-
}
146-
147-
let data = {
148-
feature_usage: {
149-
downloads: {
150-
eligible_download_folders: BUILD_ARTIFACTS_TOTAL_COUNT,
151-
successfully_downloaded_folders: BUILD_ARTIFACTS_TOTAL_COUNT - BUILD_ARTIFACTS_FAIL_COUNT
152-
},
153-
reporter: reporter
147+
let reporter = null;
148+
if(!utils.isUndefined(args.reporter)) {
149+
reporter = args.reporter;
150+
} else if(cypressJSON !== undefined){
151+
reporter = cypressJSON.reporter;
154152
}
155-
}
156153

157-
options.formData = data;
154+
let data = {
155+
feature_usage: {
156+
downloads: {
157+
eligible_download_folders: BUILD_ARTIFACTS_TOTAL_COUNT,
158+
successfully_downloaded_folders: BUILD_ARTIFACTS_TOTAL_COUNT - BUILD_ARTIFACTS_FAIL_COUNT
159+
},
160+
reporter: reporter
161+
}
162+
}
158163

159-
try {
164+
options.formData = data.toString();
160165
let responseData = null;
161-
request.post(options, function (err, resp, data) {
162-
if(err) {
163-
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
164-
} else {
165-
try {
166-
responseData = JSON.parse(data);
167-
} catch(e) {
168-
responseData = {};
169-
}
170-
if (resp.statusCode != 200) {
171-
if (responseData && responseData["error"]) {
172-
utils.sendUsageReport(bsConfig, args, responseData["error"], Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
166+
return new Promise (async (resolve, reject) => {
167+
try {
168+
request.post(options, function (err, resp, data) {
169+
if(err) {
170+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
171+
reject(err);
172+
} else {
173+
try {
174+
responseData = JSON.parse(data);
175+
} catch(e) {
176+
responseData = {};
177+
}
178+
if (resp.statusCode != 200) {
179+
if (responseData && responseData["error"]) {
180+
utils.sendUsageReport(bsConfig, args, responseData["error"], Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
181+
reject(responseData["error"])
182+
}
183+
}
173184
}
174-
}
185+
resolve()
186+
});
187+
} catch(err) {
188+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
189+
reject(err);
175190
}
176191
});
177-
} catch (err) {
178-
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
179-
}
180192
}
181193

182194
exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
183-
BUILD_ARTIFACTS_FAIL_COUNT = 0;
184-
BUILD_ARTIFACTS_TOTAL_COUNT = 0;
185-
186-
let options = {
187-
url: `${config.buildUrl}${buildId}/build_artifacts`,
188-
auth: {
189-
username: bsConfig.auth.username,
190-
password: bsConfig.auth.access_key,
191-
},
192-
headers: {
193-
'User-Agent': utils.getUserAgent(),
194-
},
195-
};
196-
197-
let message = null;
198-
let messageType = null;
199-
let errorCode = null;
200-
201-
try {
202-
let buildDetails = null;
203-
request.get(options, async function (err, resp, body) {
204-
if(err) {
205-
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
206-
process.exitCode = Constants.ERROR_EXIT_CODE;
207-
} else {
208-
try {
209-
buildDetails = JSON.parse(body);
210-
if(resp.statusCode != 200) {
211-
logger.error('Downloading the build artifacts failed.');
212-
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
213-
utils.sendUsageReport(bsConfig, args, buildDetails, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
214-
process.exitCode = Constants.ERROR_EXIT_CODE;
215-
} else {
216-
await createDirectories(buildId, buildDetails);
217-
await parseAndDownloadArtifacts(buildId, buildDetails);
195+
return new Promise ( async (resolve, reject) => {
196+
BUILD_ARTIFACTS_FAIL_COUNT = 0;
197+
BUILD_ARTIFACTS_TOTAL_COUNT = 0;
198+
199+
let options = {
200+
url: `${config.buildUrl}${buildId}/build_artifacts`,
201+
auth: {
202+
username: bsConfig.auth.username,
203+
password: bsConfig.auth.access_key,
204+
},
205+
headers: {
206+
'User-Agent': utils.getUserAgent(),
207+
},
208+
};
209+
210+
let message = null;
211+
let messageType = null;
212+
let errorCode = null;
213+
214+
try {
215+
let buildDetails = null;
216+
request.get(options, async function (err, resp, body) {
217+
if(err) {
218+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
219+
process.exitCode = Constants.ERROR_EXIT_CODE;
220+
} else {
221+
try {
222+
buildDetails = JSON.parse(body);
223+
if(resp.statusCode != 200) {
224+
logger.error('Downloading the build artifacts failed.');
225+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
226+
utils.sendUsageReport(bsConfig, args, buildDetails, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
227+
process.exitCode = Constants.ERROR_EXIT_CODE;
228+
} else {
229+
await createDirectories(buildId, buildDetails);
230+
await parseAndDownloadArtifacts(buildId, buildDetails);
231+
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
232+
messageType = Constants.messageTypes.ERROR;
233+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
234+
logger.error(message);
235+
process.exitCode = Constants.ERROR_EXIT_CODE;
236+
} else {
237+
messageType = Constants.messageTypes.SUCCESS;
238+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
239+
logger.info(message);
240+
}
241+
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs)
242+
utils.sendUsageReport(bsConfig, args, message, messageType, null, null, rawArgs);
243+
}
244+
} catch (err) {
245+
messageType = Constants.messageTypes.ERROR;
246+
errorCode = 'api_failed_build_artifacts';
218247
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
219248
messageType = Constants.messageTypes.ERROR;
220249
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
221250
logger.error(message);
222-
process.exitCode = Constants.ERROR_EXIT_CODE;
223251
} else {
224-
messageType = Constants.messageTypes.SUCCESS;
225-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
226-
logger.info(message);
252+
logger.error('Downloading the build artifacts failed.');
227253
}
228-
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs);
229-
utils.sendUsageReport(bsConfig, args, message, messageType, null, null, rawArgs);
230-
}
231-
} catch (err) {
232-
messageType = Constants.messageTypes.ERROR;
233-
errorCode = 'api_failed_build_artifacts';
234-
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
235-
messageType = Constants.messageTypes.ERROR;
236-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
237-
logger.error(message);
238-
} else {
239-
logger.error('Downloading the build artifacts failed.');
254+
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, null, rawArgs);
255+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
256+
process.exitCode = Constants.ERROR_EXIT_CODE;
240257
}
241-
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, null, rawArgs);
242-
logger.error(err.message);
243-
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
244-
process.exitCode = Constants.ERROR_EXIT_CODE;
245258
}
246-
}
247-
});
248-
} catch (err) {
249-
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
250-
process.exitCode = Constants.ERROR_EXIT_CODE;
251-
}
259+
});
260+
} catch (err) {
261+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
262+
process.exitCode = Constants.ERROR_EXIT_CODE;
263+
}
264+
});
252265
};

0 commit comments

Comments
 (0)