Skip to content

Commit 950142c

Browse files
roshan04Karan Nagpal
authored andcommitted
added changes for request module
1 parent 8860953 commit 950142c

File tree

1 file changed

+77
-40
lines changed

1 file changed

+77
-40
lines changed

bin/helpers/buildArtifacts.js

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const logger = require('./logger').winstonLogger,
1111
Constants = require("./constants"),
1212
config = require("./config");
1313

14+
const request = require('request');
15+
const { inspect } = require('util');
16+
const { reject } = require('async');
17+
1418

1519
let BUILD_ARTIFACTS_TOTAL_COUNT = 0;
1620
let BUILD_ARTIFACTS_FAIL_COUNT = 0;
@@ -95,17 +99,14 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
9599
let tmpFilePath = path.join(filePath, fileName);
96100
const writer = fs.createWriteStream(tmpFilePath);
97101

98-
return axios({
99-
method: 'get',
100-
url: url,
101-
responseType: 'stream',
102-
}).then(response => {
103-
102+
console.log(`roshan1: url ${url}`)
103+
console.log(`roshan inside downloadAndUnzip`)
104+
return request.get(url).on('response', function(response) {
104105
//ensure that the user can call `then()` only when the file has
105106
//been downloaded entirely.
106-
107+
console.log(`roshan1: response ${inspect(response)}`)
107108
return new Promise(async (resolve, reject) => {
108-
response.data.pipe(writer);
109+
response.pipe(writer);
109110
let error = null;
110111
writer.on('error', err => {
111112
error = err;
@@ -135,7 +136,7 @@ const unzipFile = async (filePath, fileName) => {
135136
}
136137

137138
const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs) => {
138-
let url = `${config.buildUrl}${buildId}/build_artifacts/status`;
139+
options.url = `${config.buildUrl}${buildId}/build_artifacts/status`;
139140

140141
let cypressJSON = utils.getCypressJSON(bsConfig);
141142

@@ -156,19 +157,38 @@ const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs) =>
156157
}
157158
}
158159

160+
options.formData = data;
161+
159162
try {
160-
await axios.post(url, data, options);
163+
let responseData = null;
164+
request.post(options, function (err, resp, data) {
165+
if(err) {
166+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
167+
} else {
168+
try {
169+
responseData = JSON.parse(data);
170+
} catch(e) {
171+
responseData = {};
172+
}
173+
if (resp.statusCode != 200) {
174+
if (responseData && responseData["error"]) {
175+
utils.sendUsageReport(bsConfig, args, responseData["error"], Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
176+
}
177+
}
178+
}
179+
});
161180
} catch (err) {
162181
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
163182
}
164183
}
165184

166185
exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
186+
console.log('hello brother')
167187
BUILD_ARTIFACTS_FAIL_COUNT = 0;
168188
BUILD_ARTIFACTS_TOTAL_COUNT = 0;
169189

170-
let url = `${config.buildUrl}${buildId}/build_artifacts`;
171190
let options = {
191+
url: `${config.buildUrl}${buildId}/build_artifacts`,
172192
auth: {
173193
username: bsConfig.auth.username,
174194
password: bsConfig.auth.access_key,
@@ -183,39 +203,56 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
183203
let errorCode = null;
184204

185205
try {
186-
const res = await axios.get(url, options);
187-
let buildDetails = res.data;
188-
189-
await createDirectories(buildId, buildDetails);
190-
await parseAndDownloadArtifacts(buildId, buildDetails);
191-
192-
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
193-
messageType = Constants.messageTypes.ERROR;
194-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
195-
logger.error(message);
206+
let buildDetails = null;
207+
request.get(options, async function (err, resp, body) {
208+
if(err) {
209+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
196210
process.exitCode = Constants.ERROR_EXIT_CODE;
197211
} else {
198-
messageType = Constants.messageTypes.SUCCESS;
199-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
200-
logger.info(message);
212+
try {
213+
buildDetails = JSON.parse(body);
214+
if(resp.statusCode != 200) {
215+
logger.error('Downloading the build artifacts failed.');
216+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
217+
utils.sendUsageReport(bsConfig, args, buildDetails, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
218+
process.exitCode = Constants.ERROR_EXIT_CODE;
219+
} else {
220+
await createDirectories(buildId, buildDetails);
221+
await parseAndDownloadArtifacts(buildId, buildDetails);
222+
console.log(`roshan1 making request passed1 ${inspect(buildDetails)}`);
223+
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
224+
messageType = Constants.messageTypes.ERROR;
225+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
226+
logger.error(message);
227+
process.exitCode = Constants.ERROR_EXIT_CODE;
228+
} else {
229+
messageType = Constants.messageTypes.SUCCESS;
230+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
231+
logger.info(message);
232+
}
233+
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs);
234+
utils.sendUsageReport(bsConfig, args, message, messageType, null, null, rawArgs);
235+
}
236+
} catch (err) {
237+
messageType = Constants.messageTypes.ERROR;
238+
errorCode = 'api_failed_build_artifacts';
239+
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
240+
messageType = Constants.messageTypes.ERROR;
241+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
242+
logger.error(message);
243+
} else {
244+
logger.error('Downloading the build artifacts failed.');
245+
}
246+
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, null, rawArgs);
247+
logger.error(err.message);
248+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
249+
process.exitCode = Constants.ERROR_EXIT_CODE;
250+
}
201251
}
202-
203-
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs);
204-
utils.sendUsageReport(bsConfig, args, message, messageType, null, null, rawArgs);
252+
});
205253
} catch (err) {
206-
messageType = Constants.messageTypes.ERROR;
207-
errorCode = 'api_failed_build_artifacts';
208-
209-
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
210-
messageType = Constants.messageTypes.ERROR;
211-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
212-
logger.error(message);
213-
} else {
214-
logger.error('Downloading the build artifacts failed.');
215-
}
216-
217-
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, null, rawArgs);
218-
logger.error(err.message);
254+
console.log(`roshan1: error here ${err}`)
255+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
219256
process.exitCode = Constants.ERROR_EXIT_CODE;
220257
}
221258
};

0 commit comments

Comments
 (0)