Skip to content

build: get previous payload by querying git #5122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}