@@ -4,6 +4,7 @@ import {statSync} from 'fs';
44import { isTravisBuild , isTravisMasterBuild } from '../util/travis-ci' ;
55import { buildConfig } from '../packaging/build-config' ;
66import { openFirebaseDashboardApp , openFirebaseDashboardAppAsGuest } from '../util/firebase' ;
7+ import { spawnSync } from 'child_process' ;
78import * as firebaseAdmin from 'firebase-admin' ;
89
910
@@ -78,14 +79,17 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c
7879 return ;
7980 }
8081
81- const previousPayload = await getLastPayloadResults ( database ) ;
82+ const previousSha = getCommitFromPreviousPayloadUpload ( ) ;
83+ const previousPayload = await getPayloadResults ( database , previousSha ) ;
8284
8385 if ( ! previousPayload ) {
8486 console . warn ( 'There are no previous payload results uploaded. Cannot calculate payload ' +
8587 'difference for this job' ) ;
8688 return ;
8789 }
8890
91+ console . log ( `Comparing payload against payload results from SHA ${ previousSha } ` ) ;
92+
8993 // Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles.
9094 const cdkFullSize = currentPayload . cdk_fesm_2015 ;
9195 const cdkDiff = cdkFullSize - previousPayload . cdk_fesm_2015 ;
@@ -138,16 +142,34 @@ async function uploadPayloadResults(database: firebaseAdmin.database.Database, c
138142 }
139143}
140144
141- /** Gets the last payload uploaded from previous Travis builds. */
142- async function getLastPayloadResults ( database : firebaseAdmin . database . Database ) {
143- const snapshot = await database . ref ( 'payloads' )
144- . orderByChild ( 'timestamp' )
145- . limitToLast ( 1 )
146- . once ( 'value' ) ;
145+ /** Gets payload results of the specified commit sha. */
146+ async function getPayloadResults ( database : firebaseAdmin . database . Database , commitSha : string ) {
147+ const snapshot = await database . ref ( 'payloads' ) . child ( commitSha ) . once ( 'value' ) ;
148+
149+ if ( ! snapshot . exists ( ) ) {
150+ throw `There is no payload data uploaded for SHA ${ commitSha } ` ;
151+ }
147152
148- // The value of the DataSnapshot is an object with the SHA as a key. Later only the
149- // first value of the object will be returned because the SHA is unnecessary.
150- const results = snapshot . val ( ) ;
153+ return snapshot . val ( ) ;
154+ }
151155
152- return snapshot . hasChildren ( ) ? results [ Object . keys ( results ) [ 0 ] ] : null ;
156+ /** Gets the SHA of the commit where the payload was uploaded before this Travis Job started. */
157+ function getCommitFromPreviousPayloadUpload ( ) : string {
158+ if ( isTravisMasterBuild ( ) ) {
159+ const commitRange = process . env [ 'TRAVIS_COMMIT_RANGE' ] ;
160+ // In some situations, Travis will include multiple commits in a single Travis Job. This means
161+ // that we can't just resolve the previous commit by using the parent commit of HEAD.
162+ // By resolving the amount of commits inside of the current Travis Job, we can figure out
163+ // how many commits before HEAD the last Travis Job ran.
164+ const commitCount = spawnSync ( 'git' , [ 'rev-list' , '--count' , commitRange ] ) . stdout
165+ . toString ( ) . trim ( ) ;
166+ // With the amount of commits inside of the current Travis Job, we can query Git to print
167+ // the SHA of the commit that ran before this Travis Job was created.
168+ return spawnSync ( 'git' , [ 'rev-parse' , `HEAD~${ commitCount } ` ] ) . stdout . toString ( ) . trim ( ) ;
169+ } else {
170+ // Travis applies the changes of Pull Requests in new branches. This means that resolving
171+ // the commit that previously ran on the target branch (mostly "master") can be done
172+ // by just loading the SHA of the most recent commit in the target branch.
173+ return spawnSync ( 'git' , [ 'rev-parse' , process . env [ 'TRAVIS_BRANCH' ] ] ) . stdout . toString ( ) . trim ( ) ;
174+ }
153175}
0 commit comments