From 7b5b18d5e7d27ec152e27d2073e5ab4b20155056 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 14 Jun 2017 23:50:37 +0200 Subject: [PATCH] build: get previous payload by querying git (#5122) --- tools/gulp/tasks/payload.ts | 44 +++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/tools/gulp/tasks/payload.ts b/tools/gulp/tasks/payload.ts index ccd04b4aeb8..ba64119152c 100644 --- a/tools/gulp/tasks/payload.ts +++ b/tools/gulp/tasks/payload.ts @@ -4,6 +4,7 @@ import {statSync} from 'fs'; import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci'; import {buildConfig} from '../packaging/build-config'; import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase'; +import {spawnSync} from 'child_process'; import * as firebaseAdmin from 'firebase-admin'; @@ -78,7 +79,8 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c return; } - const previousPayload = await getLastPayloadResults(database); + const previousSha = getCommitFromPreviousPayloadUpload(); + const previousPayload = await getPayloadResults(database, previousSha); if (!previousPayload) { console.warn('There are no previous payload results uploaded. Cannot calculate payload ' + @@ -86,6 +88,8 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c return; } + console.log(`Comparing payload against payload results from SHA ${previousSha}`); + // Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles. const cdkFullSize = currentPayload.cdk_fesm_2015; const cdkDiff = cdkFullSize - previousPayload.cdk_fesm_2015; @@ -138,16 +142,34 @@ async function uploadPayloadResults(database: firebaseAdmin.database.Database, c } } -/** Gets the last payload uploaded from previous Travis builds. */ -async function getLastPayloadResults(database: firebaseAdmin.database.Database) { - const snapshot = await database.ref('payloads') - .orderByChild('timestamp') - .limitToLast(1) - .once('value'); +/** Gets payload results of the specified commit sha. */ +async function getPayloadResults(database: firebaseAdmin.database.Database, commitSha: string) { + const snapshot = await database.ref('payloads').child(commitSha).once('value'); - // The value of the DataSnapshot is an object with the SHA as a key. Later only the - // first value of the object will be returned because the SHA is unnecessary. - const results = snapshot.val(); + if (!snapshot.exists()) { + throw `There is no payload data uploaded for SHA ${commitSha}`; + } - return snapshot.hasChildren() ? results[Object.keys(results)[0]] : null; + return snapshot.val(); +} + +/** Gets the SHA of the commit where the payload was uploaded before this Travis Job started. */ +function getCommitFromPreviousPayloadUpload(): string { + if (isTravisMasterBuild()) { + const commitRange = process.env['TRAVIS_COMMIT_RANGE']; + // In some situations, Travis will include multiple commits in a single Travis Job. This means + // that we can't just resolve the previous commit by using the parent commit of HEAD. + // By resolving the amount of commits inside of the current Travis Job, we can figure out + // how many commits before HEAD the last Travis Job ran. + const commitCount = spawnSync('git', ['rev-list', '--count', commitRange]).stdout + .toString().trim(); + // With the amount of commits inside of the current Travis Job, we can query Git to print + // the SHA of the commit that ran before this Travis Job was created. + return spawnSync('git', ['rev-parse', `HEAD~${commitCount}`]).stdout.toString().trim(); + } else { + // Travis applies the changes of Pull Requests in new branches. This means that resolving + // the commit that previously ran on the target branch (mostly "master") can be done + // by just loading the SHA of the most recent commit in the target branch. + return spawnSync('git', ['rev-parse', process.env['TRAVIS_BRANCH']]).stdout.toString().trim(); + } }