Skip to content

Commit 7fede6b

Browse files
committed
Refactor code
1 parent 824cabc commit 7fede6b

File tree

2 files changed

+56
-52
lines changed

2 files changed

+56
-52
lines changed

commands/utils/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var constants = {}
22

33
constants.stage = {
44
AUTH_URL: "https://stage-api.lambdatestinternal.com/storybook/auth",
5-
RENDER_API_URL: "https://stage-api.lambdatestinternal.com/storybook/render"
5+
RENDER_API_URL: "https://stage-api.lambdatestinternal.com/storybook/render",
6+
BUILD_STATUS_URL: "https://stage-api.lambdatestinternal.com/storybook/status"
67
};
78
constants.prod = {
89
AUTH_URL: "https://api.lambdatest.com/storybook/auth",

commands/utils/dom.js

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const { JSDOM } = require("jsdom");
66
var { constants } = require('./constants');
77
const { getLastCommit } = require('./git')
88

9+
var INTERVAL = 2000
10+
const MAX_INTERVAL = 512000
11+
912
async function sendDoM(storybookUrl, stories, storybookConfig, options) {
1013
const createBrowser = require('browserless')
1114
const browser = createBrowser()
@@ -63,7 +66,7 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
6366
})
6467
.then(async function (response) {
6568
console.log('[smartui] Build in progress...');
66-
await shortPolling(response.data.buildId, 0, 2000, 512000);
69+
await shortPolling(response.data.buildId, 0, options);
6770
})
6871
.catch(function (error) {
6972
console.log('[smartui] Build failed: Error: ', error.message);
@@ -76,59 +79,59 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
7679
});
7780
};
7881

79-
async function shortPolling(buildId, retries = 0, interval, maxInterval) {
80-
try {
81-
const response = await axios.get('https://stage-api.lambdatestinternal.com/storybook/status?buildId=' + buildId, {
82-
headers: {
83-
projectToken: process.env.PROJECT_TOKEN
82+
async function shortPolling(buildId, retries = 0, options) {
83+
await axios.get(new URL('?buildId=' + buildId, constants[options.env].BUILD_STATUS_URL).href, {
84+
headers: {
85+
projectToken: process.env.PROJECT_TOKEN
86+
}})
87+
.then(function (response) {
88+
if (response.data) {
89+
if (response.data.buildStatus === 'completed') {
90+
console.log('[smartui] Build successful\n');
91+
console.log('[smartui] Build details:\n',
92+
// 'Build URL: ', response.data.buildId, '\n',
93+
'Build Name: ', response.data.buildName, '\n',
94+
'Total Screenshots: ', response.data.screenshots.length, '\n',
95+
'Approved: ', response.data.buildResults.approved, '\n',
96+
'Changes found: ', response.data.buildResults.changesFound, '\n'
97+
);
98+
99+
response.data.screenshots.forEach(screenshot => {
100+
console.log(screenshot.storyName, ' | Mis-match: ', screenshot.mismatchPercentage);
101+
});
102+
103+
return;
104+
} else {
105+
if (response.data.screenshots && response.data.screenshots.length > 0) {
106+
// TODO: show Screenshots processed 8/10
107+
console.log('[smartui] Screenshots processed: ', response.data.screenshots.length)
108+
}
109+
}
84110
}
85-
});
86-
87-
if (response.data) {
88-
if (response.data.buildStatus === 'completed') {
89-
console.log('[smartui] Build successful\n');
90-
console.log('[smartui] Build details:\n',
91-
// 'Build URL: ', response.data.buildId, '\n',
92-
'Build Name: ', response.data.buildName, '\n',
93-
'Total Screenshots: ', response.data.screenshots.length, '\n',
94-
'Approved: ', response.data.buildResults.approved, '\n',
95-
'Changes found: ', response.data.buildResults.changesFound, '\n'
96-
);
97-
98-
response.data.screenshots.forEach(screenshot => {
99-
console.log(screenshot.storyName, ' | Mis-match: ', screenshot.mismatchPercentage);
100-
});
101-
111+
112+
// Double the INTERVAL, up to the maximum INTERVAL of 512 secs (so ~15 mins in total)
113+
INTERVAL = Math.min(INTERVAL * 2, MAX_INTERVAL);
114+
if (INTERVAL == MAX_INTERVAL) {
115+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
102116
return;
103-
} else {
104-
if (response.data.screenshots.length > 0) {
105-
// TODO: show Screenshots processed 8/10
106-
console.log('[smartui] Screenshots processed: ', response.data.screenshots.length)
107-
}
108117
}
109-
}
110-
111-
// Double the interval, up to the maximum interval of 512 secs (so ~15 mins in total)
112-
interval = Math.min(interval * 2, maxInterval);
113-
if (interval == maxInterval) {
114-
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
115-
return;
116-
}
117-
118-
setTimeout(function () {
119-
shortPolling(buildId, 0, interval, maxInterval)
120-
}, interval);
121-
} catch (error) {
122-
if (retries >= 3) {
123-
console.log('[smartui] Error: Failed getting build status.', error.message);
124-
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
125-
return;
126-
}
127-
128-
setTimeout(function () {
129-
shortPolling(buildId, retries+1, interval, maxInterval);
130-
}, 2000);
131-
}
118+
119+
setTimeout(function () {
120+
shortPolling(buildId, 0, options)
121+
}, INTERVAL);
122+
})
123+
.catch(function (error) {
124+
if (retries >= 3) {
125+
console.log('[smartui] Error: Failed getting build status.', error.message);
126+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
127+
return;
128+
}
129+
130+
console.log('here2');
131+
setTimeout(function () {
132+
shortPolling(buildId, retries+1, options);
133+
}, 2000);
134+
});
132135
};
133136

134137
function getBase64(url) {

0 commit comments

Comments
 (0)