Skip to content

Commit

Permalink
build: get previous payload by querying git (#5122)
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion authored and kara committed Jun 14, 2017
1 parent 154f54e commit 7b5b18d
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions tools/gulp/tasks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -78,14 +79,17 @@ 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 ' +
'difference for this job');
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;
Expand Down Expand Up @@ -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');

if (!snapshot.exists()) {
throw `There is no payload data uploaded for SHA ${commitSha}`;
}

// 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();
return snapshot.val();
}

return snapshot.hasChildren() ? results[Object.keys(results)[0]] : null;
/** 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();
}
}

0 comments on commit 7b5b18d

Please sign in to comment.