@@ -173,19 +173,58 @@ class Pipeline {
173173 ev . sender . send ( 'data-pipeline-log-callback' , utils . serialize ( err ) , result ) ;
174174 }
175175 try {
176- let index = 1 ;
177-
176+ let pipeline = this . storage . get ( arg . id ) ;
177+ if ( pipeline . deployResult && pipeline . deployResult . done != true ) {
178+ // Uncompleted deploy, clean cache and reload
179+ this . storage . getAll ( { cache : false } ) ;
180+ pipeline = this . storage . get ( arg . id ) ;
181+ }
178182 const metadata = new Metadata ( ) ;
179183 const pPath = metadata . getPipelineFolder ( ) ;
180- const logPath = path . join ( pPath , arg . id , 'pipeline.log' ) ;
184+ const logPath = path . join ( pPath , pipeline . id , 'pipeline.log' ) ;
181185 if ( ! fs . existsSync ( logPath ) ) {
182186 return callback ( null ) ;
183187 }
184188 fs . readFile ( logPath , 'utf-8' , function ( err , data ) {
185189 //console.log('>>> getPipelineLog' + index, (new Date()));
186- index ++ ;
187190 if ( err ) return callback ( err ) ;
188- const result = data ;
191+ // read deployResult
192+ const drPath = path . join ( pPath , pipeline . id , 'deployResult.json' ) ;
193+ let deployResult = { } ;
194+ if ( fs . existsSync ( drPath ) ) {
195+ deployResult = JSON . parse ( fs . readFileSync ( drPath ) ) ;
196+ }
197+ const result = { body : data , deployResult : deployResult } ;
198+ return callback ( null , result ) ;
199+ } ) ;
200+ } catch ( err ) {
201+ console . error ( '[ERROR]' , err ) ;
202+ Raven . captureException ( err ) ;
203+ return callback ( err ) ;
204+ }
205+ }
206+
207+ /**
208+ * Export pipeline metadata zip file
209+ * @param {Object } ev
210+ * @param {Object } arg
211+ */
212+ exportMetadata ( ev , arg ) {
213+ const callback = function ( err , result ) {
214+ ev . sender . send ( 'data-pipeline-export-metadata-callback' , utils . serialize ( err ) , result ) ;
215+ }
216+ try {
217+ const metadata = new Metadata ( ) ;
218+ const pPath = metadata . getPipelineFolder ( ) ;
219+ const packagePath = path . join ( pPath , arg . id , 'package.zip' ) ;
220+ if ( ! fs . existsSync ( packagePath ) ) {
221+ return callback ( null ) ;
222+ }
223+ fs . readFile ( packagePath , 'base64' , function ( err , data ) {
224+ //new Buffer(data).toString('base64')
225+ //console.log('>>> getPipelineLog' + index, (new Date()));
226+ if ( err ) return callback ( err ) ;
227+ const result = { data : data } ;
189228 return callback ( null , result ) ;
190229 } ) ;
191230 } catch ( err ) {
@@ -229,6 +268,7 @@ class Pipeline {
229268 const pPath = metadata . mkdirPipelineFolder ( pipeline . id ) ;
230269
231270 const logPath = path . join ( pPath , 'pipeline.log' ) ;
271+ const deployResultPath = path . join ( pPath , 'deployResult.json' ) ;
232272 const pipelineLog = function ( line ) {
233273 line = moment ( ) . format ( 'HH:mm:ss' ) + ' ' + line + '\n' ;
234274 fs . appendFileSync ( logPath , line ) ;
@@ -288,20 +328,22 @@ class Pipeline {
288328 // Do Destruct
289329 opts [ 'purgeOnDelete' ] = true ;
290330 return metadata . deploy ( fromConn , zipPath , opts , function ( deployResult ) {
331+ deployResult = self . saveDeployResult ( deployResultPath , fromConn , deployResult ) ;
291332 self . outputDeployProcessLog ( pipelineLog , deployResult ) ;
292333 } ) ;
293334 } else {
294335 // Do Deploy
295336 opts [ 'runAllTests' ] = ( pipeline . runTests === true ) ;
296337 return metadata . deploy ( toConn , zipPath , opts , function ( deployResult ) {
338+ deployResult = self . saveDeployResult ( deployResultPath , toConn , deployResult ) ;
297339 self . outputDeployProcessLog ( pipelineLog , deployResult ) ;
298340 } ) ;
299341 }
300342 } )
301343 . then ( function ( deployResult ) {
302344 // Save deploy result
303345 const targetConn = ( pipeline . action == 'destruct' ) ? fromConn : toConn ;
304- deployResult [ 'url' ] = targetConn . instanceUrl + '/changemgmt/monitorDeploymentsDetails.apexp?asyncId=' + deployResult . id
346+ deployResult = self . saveDeployResult ( deployResultPath , targetConn , deployResult ) ;
305347 self . outputDeployLog ( pipelineLog , deployResult ) ;
306348 const now = new Date ( ) ;
307349 const endTime = now . toISOString ( ) ;
@@ -366,18 +408,53 @@ class Pipeline {
366408 * @param {Object } deployResult
367409 */
368410 outputDeployLog ( pipelineLog , deployResult ) {
369- pipelineLog ( '[Metadata] Deploy Done : ' ) ;
370- //pipelineLog(' Id: ' + deployResult.id);
371- pipelineLog ( ' Success: ' + deployResult . success ) ;
372- pipelineLog ( ' Components Total: ' + deployResult . numberComponentsTotal ) ;
373- pipelineLog ( ' Components Error: ' + deployResult . numberComponentErrors ) ;
374- pipelineLog ( ' Components Deployed: ' + deployResult . numberComponentsDeployed ) ;
375- pipelineLog ( ' Tests Total: ' + deployResult . numberTestsTotal ) ;
376- pipelineLog ( ' Tests Error: ' + deployResult . numberTestErrors ) ;
377- pipelineLog ( ' Tests Completed: ' + deployResult . numberTestsCompleted ) ;
411+ pipelineLog ( '[Metadata] Deploy done' ) ;
378412 pipelineLog ( '[Metadata] Deploy result: @see ' + deployResult . url ) ;
379413 }
380414
415+ /**
416+ * Set component type label to deploy result and export to json file
417+ */
418+ saveDeployResult ( filePath , targetConn , deployResult ) {
419+ // Set Entity Label to deployResult
420+ const sfdcApi = new SfdcApi ( targetConn ) ;
421+ const componentLables = sfdcApi . getComponentLabels ( targetConn . language ) ;
422+ deployResult [ 'instanceUrl' ] = targetConn . instanceUrl ;
423+ deployResult [ 'url' ] = targetConn . instanceUrl + '/changemgmt/monitorDeploymentsDetails.apexp?asyncId=' + deployResult . id
424+
425+ const getStatus = function ( cmp ) {
426+ let status = 'No Change' ;
427+ if ( cmp . changed == 'true' ) status = 'Updated' ;
428+ if ( cmp . created == 'true' ) status = 'Created' ;
429+ if ( cmp . deleted == 'true' ) status = 'Deleted' ;
430+ if ( cmp . problem && cmp . problemType ) {
431+ status = 'Error: ' + cmp . problem + ' (line ' + ( cmp . lineNumber || 0 ) + ', column ' + ( cmp . columnNumber || 0 ) + ')' ;
432+ }
433+ return status ;
434+ }
435+
436+ if ( deployResult . details && deployResult . details . componentSuccesses ) {
437+ for ( let i = 0 ; i < deployResult . details . componentSuccesses . length ; i ++ ) {
438+ let componentType = deployResult . details . componentSuccesses [ i ] . componentType ;
439+ if ( utils . isBlank ( componentType ) ) continue ;
440+ if ( ! componentLables . hasOwnProperty ( componentType ) ) continue ;
441+ deployResult . details . componentSuccesses [ i ] [ 'componentTypeLabel' ] = componentLables [ componentType ] ;
442+ deployResult . details . componentSuccesses [ i ] [ 'status' ] = getStatus ( deployResult . details . componentSuccesses [ i ] ) ;
443+ }
444+ }
445+ if ( deployResult . details && deployResult . details . componentFailures ) {
446+ for ( let i = 0 ; i < deployResult . details . componentFailures . length ; i ++ ) {
447+ let componentType = deployResult . details . componentFailures [ i ] . componentType ;
448+ if ( utils . isBlank ( componentType ) ) continue ;
449+ if ( ! componentLables . hasOwnProperty ( componentType ) ) continue ;
450+ deployResult . details . componentFailures [ i ] [ 'componentTypeLabel' ] = componentLables [ componentType ] ;
451+ deployResult . details . componentSuccesses [ i ] [ 'status' ] = getStatus ( deployResult . details . componentSuccesses [ i ] ) ;
452+ }
453+ }
454+ fs . writeFileSync ( filePath , JSON . stringify ( deployResult ) ) ;
455+ return deployResult ;
456+ }
457+
381458}
382459
383460module . exports = Pipeline ;
0 commit comments