Skip to content

Commit a21daaf

Browse files
committed
refactoring code
1 parent 0b6cc9c commit a21daaf

File tree

7 files changed

+74
-40
lines changed

7 files changed

+74
-40
lines changed

browserstack-report-action/dist/index.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38154,7 +38154,7 @@ const constants = __nccwpck_require__(9350);
3815438154
const ActionInput = __nccwpck_require__(460);
3815538155
const ReportService = __nccwpck_require__(9427);
3815638156
const ReportProcessor = __nccwpck_require__(4140);
38157-
const TimeoutManager = __nccwpck_require__(933);
38157+
const TimeManager = __nccwpck_require__(9991);
3815838158

3815938159
async function run() {
3816038160
try {
@@ -38172,7 +38172,7 @@ async function run() {
3817238172
core.info('Running in test mode with mock API responses');
3817338173
}
3817438174

38175-
const timeoutManager = new TimeoutManager(userTimeout
38175+
const timeManager = new TimeManager(userTimeout
3817638176
|| constants.DEFAULT_USER_TIMEOUT_SECONDS);
3817738177
const reportService = new ReportService(authHeader, isTestMode);
3817838178

@@ -38185,7 +38185,7 @@ async function run() {
3818538185
userTimeout,
3818638186
};
3818738187

38188-
timeoutManager.check();
38188+
timeManager.checkTimeout();
3818938189
let reportData = await reportService.fetchReport(initialParams);
3819038190
let { retryCount: maxRetries, pollingInterval } = reportData;
3819138191

@@ -38200,7 +38200,7 @@ async function run() {
3820038200
if (reportData.reportStatus === constants.REPORT_STATUS.IN_PROGRESS) {
3820138201
reportData = await reportService.pollReport(
3820238202
initialParams,
38203-
timeoutManager,
38203+
timeManager,
3820438204
maxRetries,
3820538205
pollingInterval,
3820638206
);
@@ -38683,7 +38683,8 @@ class ReportProcessor {
3868338683
summary.write();
3868438684
if (reportData?.report?.richHtml) {
3868538685
const report = `<!DOCTYPE html> <html><head><style>${reportData?.report?.richCss}</style></head> ${reportData?.report?.richHtml}</html>`;
38686-
await UploadFileForArtifact.saveReportInFile(report);
38686+
const artifactObj = new UploadFileForArtifact(report);
38687+
await artifactObj.saveReportInFile();
3868738688
}
3868838689
} catch (error) {
3868938690
core.info(`Error processing report: ${JSON.stringify(error)}`);
@@ -38743,9 +38744,11 @@ class ReportService {
3874338744
};
3874438745
}
3874538746

38746-
async pollReport(params, timeoutManager, maxRetries, pollingInterval) {
38747+
async pollReport(params, timeManager, maxRetries, pollingInterval) {
3874738748
const poll = async (retries) => {
38748-
timeoutManager.check();
38749+
if (timeManager.checkTimeout()) {
38750+
return this.handleErrorStatus(constants.REPORT_STATUS.IN_PROGRESS);
38751+
}
3874938752

3875038753
const reportData = await this.fetchReport({
3875138754
...params,
@@ -38761,7 +38764,7 @@ class ReportService {
3876138764
}
3876238765

3876338766
if (status === constants.REPORT_STATUS.IN_PROGRESS && retries < maxRetries) {
38764-
await new Promise((resolve) => setTimeout(resolve, pollingInterval * 1000));
38767+
await timeManager.sleep(pollingInterval);
3876538768
return poll(retries + 1);
3876638769
}
3876738770

@@ -38772,15 +38775,16 @@ class ReportService {
3877238775
return poll(0);
3877338776
}
3877438777

38775-
static handleErrorStatus(status, reportData) {
38778+
static handleErrorStatus(status, reportData = {}) {
3877638779
const errorMessages = {
38777-
ERROR: 'Error occurred while fetching report',
38780+
ERROR: 'Unable to Fetch Report',
38781+
IN_PROGRESS: 'Report is still in progress',
3877838782
};
3877938783

3878038784
return {
3878138785
errorMessage: errorMessages[status] || `Unexpected status: ${status}`,
3878238786
reportStatus: status,
38783-
report: reportData.report,
38787+
report: status === constants.REPORT_STATUS.IN_PROGRESS ? { basicHtml: '<pre>Report generation not completed, please try again after increasing report timeout</pre>' } : reportData.report,
3878438788
};
3878538789
}
3878638790
}
@@ -38790,23 +38794,32 @@ module.exports = ReportService;
3879038794

3879138795
/***/ }),
3879238796

38793-
/***/ 933:
38797+
/***/ 9991:
3879438798
/***/ ((module) => {
3879538799

38796-
class TimeoutManager {
38800+
class TimeManager {
3879738801
constructor(timeoutSeconds) {
3879838802
this.timeoutMs = timeoutSeconds * 1000;
3879938803
this.startTime = Date.now();
3880038804
}
3880138805

38802-
check() {
38806+
checkTimeout() {
3880338807
if (Date.now() - this.startTime >= this.timeoutMs) {
3880438808
throw new Error(`Operation timed out after ${this.timeoutMs / 1000} seconds`);
3880538809
}
3880638810
}
38811+
38812+
/**
38813+
* Sleep for specified seconds
38814+
* @param {number} seconds - Number of seconds to sleep
38815+
* @returns {Promise} - Promise that resolves after the specified time
38816+
*/
38817+
static async sleep(seconds) {
38818+
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
38819+
}
3880738820
}
3880838821

38809-
module.exports = TimeoutManager;
38822+
module.exports = TimeManager;
3881038823

3881138824

3881238825
/***/ }),
@@ -38819,6 +38832,10 @@ const path = __nccwpck_require__(6928);
3881938832
const core = __nccwpck_require__(7484);
3882038833

3882138834
class UploadFileForArtifact {
38835+
constructor(report) {
38836+
this.report = report;
38837+
}
38838+
3882238839
static async saveReportInFile(report) {
3882338840
if (!report) {
3882438841
core.debug('No HTML content available to save as artifact');

browserstack-report-action/src/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const constants = require('../config/constants');
33
const ActionInput = require('./actionInput');
44
const ReportService = require('./services/ReportService');
55
const ReportProcessor = require('./services/ReportProcessor');
6-
const TimeoutManager = require('./utils/TimeoutManager');
6+
const TimeManager = require('./utils/TimeManager');
77

88
async function run() {
99
try {
@@ -21,7 +21,7 @@ async function run() {
2121
core.info('Running in test mode with mock API responses');
2222
}
2323

24-
const timeoutManager = new TimeoutManager(userTimeout
24+
const timeManager = new TimeManager(userTimeout
2525
|| constants.DEFAULT_USER_TIMEOUT_SECONDS);
2626
const reportService = new ReportService(authHeader, isTestMode);
2727

@@ -34,7 +34,7 @@ async function run() {
3434
userTimeout,
3535
};
3636

37-
timeoutManager.check();
37+
timeManager.checkTimeout();
3838
let reportData = await reportService.fetchReport(initialParams);
3939
let { retryCount: maxRetries, pollingInterval } = reportData;
4040

@@ -49,7 +49,7 @@ async function run() {
4949
if (reportData.reportStatus === constants.REPORT_STATUS.IN_PROGRESS) {
5050
reportData = await reportService.pollReport(
5151
initialParams,
52-
timeoutManager,
52+
timeManager,
5353
maxRetries,
5454
pollingInterval,
5555
);

browserstack-report-action/src/services/ReportProcessor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class ReportProcessor {
1515
summary.write();
1616
if (reportData?.report?.richHtml) {
1717
const report = `<!DOCTYPE html> <html><head><style>${reportData?.report?.richCss}</style></head> ${reportData?.report?.richHtml}</html>`;
18-
await UploadFileForArtifact.saveReportInFile(report);
18+
const artifactObj = new UploadFileForArtifact(report);
19+
await artifactObj.saveReportInFile();
1920
}
2021
} catch (error) {
2122
core.info(`Error processing report: ${JSON.stringify(error)}`);

browserstack-report-action/src/services/ReportService.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class ReportService {
3838
};
3939
}
4040

41-
async pollReport(params, timeoutManager, maxRetries, pollingInterval) {
41+
async pollReport(params, timeManager, maxRetries, pollingInterval) {
4242
const poll = async (retries) => {
43-
timeoutManager.check();
43+
if (timeManager.checkTimeout()) {
44+
return this.handleErrorStatus(constants.REPORT_STATUS.IN_PROGRESS);
45+
}
4446

4547
const reportData = await this.fetchReport({
4648
...params,
@@ -56,7 +58,7 @@ class ReportService {
5658
}
5759

5860
if (status === constants.REPORT_STATUS.IN_PROGRESS && retries < maxRetries) {
59-
await new Promise((resolve) => setTimeout(resolve, pollingInterval * 1000));
61+
await timeManager.sleep(pollingInterval);
6062
return poll(retries + 1);
6163
}
6264

@@ -67,15 +69,16 @@ class ReportService {
6769
return poll(0);
6870
}
6971

70-
static handleErrorStatus(status, reportData) {
72+
static handleErrorStatus(status, reportData = {}) {
7173
const errorMessages = {
72-
ERROR: 'Error occurred while fetching report',
74+
ERROR: 'Unable to Fetch Report',
75+
IN_PROGRESS: 'Report is still in progress',
7376
};
7477

7578
return {
7679
errorMessage: errorMessages[status] || `Unexpected status: ${status}`,
7780
reportStatus: status,
78-
report: reportData.report,
81+
report: status === constants.REPORT_STATUS.IN_PROGRESS ? { basicHtml: '<pre>Report generation not completed, please try again after increasing report timeout</pre>' } : reportData.report,
7982
};
8083
}
8184
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class TimeManager {
2+
constructor(timeoutSeconds) {
3+
this.timeoutMs = timeoutSeconds * 1000;
4+
this.startTime = Date.now();
5+
}
6+
7+
checkTimeout() {
8+
if (Date.now() - this.startTime >= this.timeoutMs) {
9+
throw new Error(`Operation timed out after ${this.timeoutMs / 1000} seconds`);
10+
}
11+
}
12+
13+
/**
14+
* Sleep for specified seconds
15+
* @param {number} seconds - Number of seconds to sleep
16+
* @returns {Promise} - Promise that resolves after the specified time
17+
*/
18+
static async sleep(seconds) {
19+
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
20+
}
21+
}
22+
23+
module.exports = TimeManager;

browserstack-report-action/src/utils/TimeoutManager.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

browserstack-report-action/src/utils/UploadFileForArtifact.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const path = require('path');
33
const core = require('@actions/core');
44

55
class UploadFileForArtifact {
6+
constructor(report) {
7+
this.report = report;
8+
}
9+
610
static async saveReportInFile(report) {
711
if (!report) {
812
core.debug('No HTML content available to save as artifact');

0 commit comments

Comments
 (0)