Skip to content

Commit 3a68882

Browse files
roshan04Karan Nagpal
authored andcommitted
handling custom report requests and buil stop api
1 parent 94fffe5 commit 3a68882

File tree

3 files changed

+227
-184
lines changed

3 files changed

+227
-184
lines changed

bin/helpers/buildArtifacts.js

Lines changed: 97 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -98,35 +98,31 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
9898
const writer = fs.createWriteStream(tmpFilePath);
9999

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

@@ -140,55 +136,50 @@ const unzipFile = async (filePath, fileName) => {
140136
}
141137

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

145-
let cypressJSON = utils.getCypressJSON(bsConfig);
141+
let cypressJSON = utils.getCypressJSON(bsConfig);
146142

147-
let reporter = null;
148-
if(!utils.isUndefined(args.reporter)) {
149-
reporter = args.reporter;
150-
} else if(cypressJSON !== undefined){
151-
reporter = cypressJSON.reporter;
152-
}
143+
let reporter = null;
144+
if(!utils.isUndefined(args.reporter)) {
145+
reporter = args.reporter;
146+
} else if(cypressJSON !== undefined){
147+
reporter = cypressJSON.reporter;
148+
}
153149

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-
}
150+
let data = {
151+
feature_usage: {
152+
downloads: {
153+
eligible_download_folders: BUILD_ARTIFACTS_TOTAL_COUNT,
154+
successfully_downloaded_folders: BUILD_ARTIFACTS_TOTAL_COUNT - BUILD_ARTIFACTS_FAIL_COUNT
155+
},
156+
reporter: reporter
162157
}
158+
}
163159

164-
options.formData = data.toString();
165-
let responseData = null;
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-
}
184-
}
185-
resolve()
186-
});
187-
} catch(err) {
160+
options.formData = data.toString();
161+
let responseData = null;
162+
return new Promise (async (resolve, reject) => {
163+
request.post(options, function (err, resp, data) {
164+
if(err) {
188165
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts_status_update', null, rawArgs);
189166
reject(err);
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+
reject(responseData["error"])
177+
}
178+
}
190179
}
180+
resolve()
191181
});
182+
});
192183
}
193184

194185
exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
@@ -210,56 +201,50 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
210201
let message = null;
211202
let messageType = null;
212203
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';
204+
let buildDetails = null;
205+
request.get(options, async function (err, resp, body) {
206+
if(err) {
207+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
208+
process.exitCode = Constants.ERROR_EXIT_CODE;
209+
} else {
210+
try {
211+
buildDetails = JSON.parse(body);
212+
if(resp.statusCode != 200) {
213+
logger.error('Downloading the build artifacts failed.');
214+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
215+
utils.sendUsageReport(bsConfig, args, buildDetails, Constants.messageTypes.ERROR, 'api_failed_build_artifacts', null, rawArgs);
216+
process.exitCode = Constants.ERROR_EXIT_CODE;
217+
} else {
218+
await createDirectories(buildId, buildDetails);
219+
await parseAndDownloadArtifacts(buildId, buildDetails);
247220
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
248221
messageType = Constants.messageTypes.ERROR;
249222
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
250223
logger.error(message);
224+
process.exitCode = Constants.ERROR_EXIT_CODE;
251225
} else {
252-
logger.error('Downloading the build artifacts failed.');
226+
messageType = Constants.messageTypes.SUCCESS;
227+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
228+
logger.info(message);
253229
}
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;
230+
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs)
231+
utils.sendUsageReport(bsConfig, args, message, messageType, null, null, rawArgs);
232+
}
233+
} catch (err) {
234+
messageType = Constants.messageTypes.ERROR;
235+
errorCode = 'api_failed_build_artifacts';
236+
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
237+
messageType = Constants.messageTypes.ERROR;
238+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
239+
logger.error(message);
240+
} else {
241+
logger.error('Downloading the build artifacts failed.');
257242
}
243+
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, null, rawArgs);
244+
logger.error(`Error: Request failed with status code ${resp.statusCode}`)
245+
process.exitCode = Constants.ERROR_EXIT_CODE;
258246
}
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-
}
247+
}
248+
});
264249
});
265250
};

bin/helpers/reporterHTML.js

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,74 @@ async function cypressReportData(report_data) {
235235
return report_data;
236236
}
237237

238+
function getConfigJsonResponse(combination) {
239+
return new Promise(async (resolve, reject) => {
240+
configJsonResponse = null;
241+
configJsonError = false
242+
request.get(combination.tests.config_json , function(err, resp, body) {
243+
if(err) {
244+
configJsonError = true;
245+
reject([configJsonResponse, configJsonError]);
246+
} else {
247+
if(resp.statusCode != 200) {
248+
configJsonError = true;
249+
reject([configJsonResponse, configJsonError]);
250+
} else {
251+
try {
252+
configJsonResponse = JSON.parse(body);
253+
} catch (err) {
254+
configJsonError = true
255+
reject([configJsonResponse, configJsonError]);
256+
}
257+
}
258+
}
259+
resolve([configJsonResponse, configJsonError]);
260+
});
261+
});
262+
}
263+
264+
function getResultsJsonResponse(combination) {
265+
return new Promise(async (resolve, reject) => {
266+
resultsJsonResponse = null
267+
resultsJsonError = false;
268+
request.get(combination.tests.result_json , function(err, resp, body) {
269+
if(err) {
270+
resultsJsonError = true;
271+
reject([resultsJsonResponse, resultsJsonError]);
272+
} else {
273+
if(resp.statusCode != 200) {
274+
resultsJsonError = true;
275+
reject([resultsJsonResponse, resultsJsonError]);
276+
} else {
277+
try {
278+
resultsJsonResponse = JSON.parse(body);
279+
} catch (err) {
280+
resultsJsonError = true
281+
reject([resultsJsonResponse, resultsJsonError]);
282+
}
283+
}
284+
}
285+
resolve([resultsJsonResponse, resultsJsonError]);
286+
});
287+
});
288+
}
289+
238290
function generateCypressCombinationSpecReportDataWithConfigJson(combination){
239291
return new Promise(async (resolve, reject) => {
240292
try {
241293
let configJsonError, resultsJsonError;
242-
let [configJsonResponse, resultsJsonResponse] = await axios.all([
243-
axios.get(combination.tests.config_json).catch(function (error) {
244-
configJsonError = true;
245-
}),
246-
axios.get(combination.tests.result_json).catch(function(error){
247-
resultsJsonError = true;
248-
})
249-
]);
294+
let configJson, resultsJson;
295+
296+
await Promise.all([getConfigJsonResponse(combination), getResultsJsonResponse(combination)]).then(function (successResult) {
297+
[configJson, configJsonError, resultsJson, resultsJsonError] = successResult;
298+
}).catch(function (failureResult) {
299+
[configJson, configJsonError, resultsJson, resultsJsonError] = failureResult;
300+
});
301+
250302
if(resultsJsonError || configJsonError){
251303
resolve();
252304
}
253305
let tests = {};
254-
let configJson = configJsonResponse.data;
255-
let resultsJson = resultsJsonResponse.data;
256306
if(utils.isUndefined(configJson.tests) || utils.isUndefined(resultsJson.tests)){
257307
resolve();
258308
}
@@ -287,14 +337,15 @@ function generateCypressCombinationSpecReportDataWithConfigJson(combination){
287337
function generateCypressCombinationSpecReportDataWithoutConfigJson(combination){
288338
return new Promise(async (resolve, reject) => {
289339
try {
290-
let resultsJsonError;
291-
let resultsJsonResponse = await axios.get(combination.tests.result_json).catch(function(error){
292-
resultsJsonError = true;
293-
});
340+
let resultsJson ,resultsJsonError;
341+
await getResultsJsonResponse(combination).then(function (successResult) {
342+
[resultsJson, resultsJsonError] = successResult
343+
}).catch( function (failureResult) {
344+
[resultsJson, resultsJsonError] = failureResult
345+
})
294346
if(resultsJsonError || utils.isUndefined(resultsJsonResponse)){
295347
resolve();
296348
}
297-
let resultsJson = resultsJsonResponse.data;
298349
let sessionTests = [];
299350
if(utils.isUndefined(resultsJson.tests)){
300351
resolve();

0 commit comments

Comments
 (0)