Skip to content

Commit c25888f

Browse files
committed
build: get previous payload by querying git
Currently the previous payload is just the latest uploaded payload result on Firebase. This is problematic because it can happen that the current payload is uploaded "too fast" and the payload task is comparing against itself. Just calculating before uploading could work most of the time, but there it would still fail if another Travis Job uploades payload results at the same time. The safest solution would be resolving the parent commit of the current Travis job. This way the payload diff would be always correct (even if multiple Jobs are running).
1 parent 82d752b commit c25888f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

tools/gulp/tasks/payload.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {statSync} from 'fs';
44
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
55
import {buildConfig} from '../packaging/build-config';
66
import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase';
7+
import {spawnSync} from 'child_process';
78
import * as firebaseAdmin from 'firebase-admin';
89

910

@@ -140,14 +141,24 @@ async function uploadPayloadResults(database: firebaseAdmin.database.Database, c
140141

141142
/** Gets the last payload uploaded from previous Travis builds. */
142143
async function getLastPayloadResults(database: firebaseAdmin.database.Database) {
143-
const snapshot = await database.ref('payloads')
144-
.orderByChild('timestamp')
145-
.limitToLast(1)
146-
.once('value');
144+
const previousSha = getCommitFromPreviousPayloadUpload();
145+
const snapshot = await database.ref('payloads').child(previousSha).once('value');
147146

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();
147+
if (!snapshot.exists()) {
148+
throw `There is no payload data uploaded for SHA ${previousSha}`;
149+
}
150+
151+
return snapshot.val();
152+
}
151153

152-
return snapshot.hasChildren() ? results[Object.keys(results)[0]] : null;
154+
/** Gets the SHA of the commit where the payload was uploaded before this Travis Job started. */
155+
function getCommitFromPreviousPayloadUpload(): string {
156+
if (isTravisMasterBuild()) {
157+
const commitRange = process.env['TRAVIS_COMMIT_RANGE'];
158+
const commitCount = spawnSync('git', ['rev-list', '--count', commitRange]).stdout
159+
.toString().trim();
160+
return spawnSync('git', ['rev-parse', `HEAD~${commitCount}`]).stdout.toString().trim();
161+
} else {
162+
return spawnSync('git', ['rev-parse', process.env['TRAVIS_BRANCH']]).stdout.toString().trim();
163+
}
153164
}

0 commit comments

Comments
 (0)