Skip to content

Commit f250243

Browse files
committed
Move polling functionality
1 parent aaf9790 commit f250243

File tree

2 files changed

+83
-78
lines changed

2 files changed

+83
-78
lines changed

commands/utils/dom.js

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ const fs = require('fs');
33
const path = require('path')
44
const formData = require('form-data');
55
const { JSDOM } = require("jsdom");
6-
const Table = require('cli-table3');
76
var { constants } = require('./constants');
87
const { getLastCommit } = require('./git');
9-
10-
var INTERVAL = 2000
11-
const MAX_INTERVAL = 512000
8+
const { shortPolling } = require('./polling');
129

1310
async function sendDoM(storybookUrl, stories, storybookConfig, options) {
1411
const createBrowser = require('browserless')
@@ -97,80 +94,6 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
9794
});
9895
};
9996

100-
async function shortPolling(buildId, retries = 0, options) {
101-
await axios.get(new URL('?buildId=' + buildId, constants[options.env].BUILD_STATUS_URL).href, {
102-
headers: {
103-
projectToken: process.env.PROJECT_TOKEN
104-
}})
105-
.then(function (response) {
106-
if (response.data) {
107-
if (response.data.buildStatus === 'completed') {
108-
console.log('[smartui] Build successful\n');
109-
console.log('[smartui] Build details:\n',
110-
'Build URL: ', response.data.buildURL, '\n',
111-
'Build Name: ', response.data.buildName, '\n',
112-
'Total Screenshots: ', response.data.totalScreenshots, '\n',
113-
'Approved: ', response.data.buildResults.approved, '\n',
114-
'Changes found: ', response.data.buildResults.changesFound, '\n'
115-
);
116-
117-
if (response.data.screenshots && response.data.screenshots.length > 0) {
118-
import('chalk').then((chalk) => {
119-
const table = new Table({
120-
head: [
121-
{content: chalk.default.white('Story'), hAlign: 'center'},
122-
{content: chalk.default.white('Mis-match %'), hAlign: 'center'},
123-
]
124-
});
125-
response.data.screenshots.forEach(screenshot => {
126-
let mismatch = screenshot.mismatchPercentage
127-
table.push([
128-
chalk.default.yellow(screenshot.storyName),
129-
mismatch > 0 ? chalk.default.red(mismatch) : chalk.default.green(mismatch)
130-
])
131-
});
132-
console.log(table.toString());
133-
})
134-
} else {
135-
if (response.data.baseline) {
136-
console.log('No comparisons run. This is a baseline build.');
137-
} else {
138-
console.log('No comparisons run. No screenshot in the current build has the corresponding screenshot in baseline build.');
139-
}
140-
}
141-
return;
142-
} else {
143-
if (response.data.screenshots && response.data.screenshots.length > 0) {
144-
// TODO: show Screenshots processed current/total
145-
console.log('[smartui] Screenshots compared: ', response.data.screenshots.length)
146-
}
147-
}
148-
}
149-
150-
// Double the INTERVAL, up to the maximum INTERVAL of 512 secs (so ~15 mins in total)
151-
INTERVAL = Math.min(INTERVAL * 2, MAX_INTERVAL);
152-
if (INTERVAL == MAX_INTERVAL) {
153-
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
154-
return;
155-
}
156-
157-
setTimeout(function () {
158-
shortPolling(buildId, 0, options)
159-
}, INTERVAL);
160-
})
161-
.catch(function (error) {
162-
if (retries >= 3) {
163-
console.log('[smartui] Error: Failed getting build status.', error.message);
164-
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
165-
return;
166-
}
167-
168-
setTimeout(function () {
169-
shortPolling(buildId, retries+1, options);
170-
}, 2000);
171-
});
172-
};
173-
17497
function getBase64(url) {
17598
return axios.get(url, {
17699
responseType: "text",

commands/utils/polling.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const axios = require('axios');
2+
const Table = require('cli-table3');
3+
var { constants } = require('./constants');
4+
5+
var INTERVAL = 2000
6+
const MAX_INTERVAL = 512000
7+
8+
async function shortPolling(buildId, retries = 0, options) {
9+
await axios.get(new URL('?buildId=' + buildId, constants[options.env].BUILD_STATUS_URL).href, {
10+
headers: {
11+
projectToken: process.env.PROJECT_TOKEN
12+
}})
13+
.then(function (response) {
14+
if (response.data) {
15+
if (response.data.buildStatus === 'completed') {
16+
console.log('[smartui] Build successful\n');
17+
console.log('[smartui] Build details:\n',
18+
'Build URL: ', response.data.buildURL, '\n',
19+
'Build Name: ', response.data.buildName, '\n',
20+
'Total Screenshots: ', response.data.totalScreenshots, '\n',
21+
'Approved: ', response.data.buildResults.approved, '\n',
22+
'Changes found: ', response.data.buildResults.changesFound, '\n'
23+
);
24+
25+
if (response.data.screenshots && response.data.screenshots.length > 0) {
26+
import('chalk').then((chalk) => {
27+
const table = new Table({
28+
head: [
29+
{content: chalk.default.white('Story'), hAlign: 'center'},
30+
{content: chalk.default.white('Mis-match %'), hAlign: 'center'},
31+
]
32+
});
33+
response.data.screenshots.forEach(screenshot => {
34+
let mismatch = screenshot.mismatchPercentage
35+
table.push([
36+
chalk.default.yellow(screenshot.storyName),
37+
mismatch > 0 ? chalk.default.red(mismatch) : chalk.default.green(mismatch)
38+
])
39+
});
40+
console.log(table.toString());
41+
})
42+
} else {
43+
if (response.data.baseline) {
44+
console.log('No comparisons run. This is a baseline build.');
45+
} else {
46+
console.log('No comparisons run. No screenshot in the current build has the corresponding screenshot in baseline build.');
47+
}
48+
}
49+
return;
50+
} else {
51+
if (response.data.screenshots && response.data.screenshots.length > 0) {
52+
// TODO: show Screenshots processed current/total
53+
console.log('[smartui] Screenshots compared: ', response.data.screenshots.length)
54+
}
55+
}
56+
}
57+
58+
// Double the INTERVAL, up to the maximum INTERVAL of 512 secs (so ~15 mins in total)
59+
INTERVAL = Math.min(INTERVAL * 2, MAX_INTERVAL);
60+
if (INTERVAL == MAX_INTERVAL) {
61+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
62+
return;
63+
}
64+
65+
setTimeout(function () {
66+
shortPolling(buildId, 0, options)
67+
}, INTERVAL);
68+
})
69+
.catch(function (error) {
70+
if (retries >= 3) {
71+
console.log('[smartui] Error: Failed getting build status.', error.message);
72+
console.log('[smartui] Please check the build status on LambdaTest SmartUI.');
73+
return;
74+
}
75+
76+
setTimeout(function () {
77+
shortPolling(buildId, retries+1, options);
78+
}, 2000);
79+
});
80+
};
81+
82+
module.exports = { shortPolling }

0 commit comments

Comments
 (0)