@@ -6,6 +6,9 @@ const { JSDOM } = require("jsdom");
66var { constants } = require ( './constants' ) ;
77const { getLastCommit } = require ( './git' )
88
9+ var INTERVAL = 2000
10+ const MAX_INTERVAL = 512000
11+
912async function sendDoM ( storybookUrl , stories , storybookConfig , options ) {
1013 const createBrowser = require ( 'browserless' )
1114 const browser = createBrowser ( )
@@ -40,7 +43,7 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
4043 await browser . close ( )
4144
4245 // Create form
43- let commit = await getLastCommit ( ) ;
46+ // let commit = await getLastCommit();
4447 const form = new formData ( ) ;
4548 for ( const [ storyId , storyInfo ] of Object . entries ( stories ) ) {
4649 const file = fs . readFileSync ( 'doms/' + storyId + '.html' ) ;
@@ -49,38 +52,88 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
4952 form . append ( 'resolution' , storybookConfig . resolutions ) ;
5053 form . append ( 'browser' , storybookConfig . browsers ) ;
5154 form . append ( 'projectToken' , process . env . PROJECT_TOKEN ) ;
52- form . append ( 'buildName' , options . buildname ) ;
53- form . append ( 'branch' , commit . branch ) ;
54- form . append ( 'commitId' , commit . shortHash ) ;
55- form . append ( 'commitAuthor' , commit . author . name ) ;
56- form . append ( 'commitMessage' , commit . subject ) ;
55+ // form.append('buildName', options.buildname);
56+ // form.append('branch', commit.branch);
57+ // form.append('commitId', commit.shortHash);
58+ // form.append('commitAuthor', commit.author.name);
59+ // form.append('commitMessage', commit.subject);
5760
5861 // Send DOM to render API
59- axios . post ( constants [ options . env ] . RENDER_API_URL , form , {
62+ await axios . post ( constants [ options . env ] . RENDER_API_URL , form , {
6063 headers : {
6164 ...form . getHeaders ( )
6265 }
6366 } )
64- . then ( function ( response ) {
65- console . log ( '[smartui] Build successful' )
67+ . then ( async function ( response ) {
68+ console . log ( '[smartui] Build in progress...' ) ;
69+ await shortPolling ( response . data . buildId , 0 , options ) ;
6670 } )
6771 . catch ( function ( error ) {
68- fs . rm ( 'doms' , { recursive : true } , ( err ) => {
69- if ( err ) {
70- return console . error ( err ) ;
71- }
72- } ) ;
7372 console . log ( '[smartui] Build failed: Error: ' , error . message ) ;
74- process . exit ( 0 ) ;
7573 } ) ;
76-
74+
7775 fs . rm ( 'doms' , { recursive : true } , ( err ) => {
7876 if ( err ) {
7977 return console . error ( err ) ;
8078 }
8179 } ) ;
8280} ;
8381
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+ }
110+ }
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.' ) ;
116+ return ;
117+ }
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+ } ) ;
135+ } ;
136+
84137function getBase64 ( url ) {
85138 return axios . get ( url , {
86139 responseType : "text" ,
0 commit comments