@@ -4,6 +4,7 @@ import {statSync} from 'fs';
4
4
import { isTravisBuild , isTravisMasterBuild } from '../util/travis-ci' ;
5
5
import { buildConfig } from '../packaging/build-config' ;
6
6
import { openFirebaseDashboardApp , openFirebaseDashboardAppAsGuest } from '../util/firebase' ;
7
+ import { spawnSync } from 'child_process' ;
7
8
import * as firebaseAdmin from 'firebase-admin' ;
8
9
9
10
@@ -78,14 +79,17 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c
78
79
return ;
79
80
}
80
81
81
- const previousPayload = await getLastPayloadResults ( database ) ;
82
+ const previousSha = getCommitFromPreviousPayloadUpload ( ) ;
83
+ const previousPayload = await getPayloadResults ( database , previousSha ) ;
82
84
83
85
if ( ! previousPayload ) {
84
86
console . warn ( 'There are no previous payload results uploaded. Cannot calculate payload ' +
85
87
'difference for this job' ) ;
86
88
return ;
87
89
}
88
90
91
+ console . log ( `Comparing payload against payload results from SHA ${ previousSha } ` ) ;
92
+
89
93
// Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles.
90
94
const cdkFullSize = currentPayload . cdk_fesm_2015 ;
91
95
const cdkDiff = cdkFullSize - previousPayload . cdk_fesm_2015 ;
@@ -138,16 +142,34 @@ async function uploadPayloadResults(database: firebaseAdmin.database.Database, c
138
142
}
139
143
}
140
144
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
+ }
147
152
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
+ }
151
155
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
+ }
153
175
}
0 commit comments