From bc6d4ff3e6e3521fe84c8c46925953b07034aa65 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sun, 4 Jun 2017 13:03:44 +0200 Subject: [PATCH] build: payload github status * Show payload deta on PRs using the Github Statuses API. --- tools/gulp/tasks/payload.ts | 105 +++++++++++++++++++++++++++++------ tools/gulp/util/firebase.ts | 12 +++- tools/gulp/util/travis-ci.ts | 4 ++ 3 files changed, 103 insertions(+), 18 deletions(-) diff --git a/tools/gulp/tasks/payload.ts b/tools/gulp/tasks/payload.ts index ea8b2ffc6da1..b07fcb8b7926 100644 --- a/tools/gulp/tasks/payload.ts +++ b/tools/gulp/tasks/payload.ts @@ -2,16 +2,18 @@ import {task} from 'gulp'; import {join} from 'path'; import {statSync} from 'fs'; import {DIST_ROOT} from '../build-config'; -import {spawnSync} from 'child_process'; -import {isTravisMasterBuild} from '../util/travis-ci'; +import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci'; import {openFirebaseDashboardApp} from '../util/firebase'; +// These imports lack of type definitions. +const request = require('request'); + const bundlesDir = join(DIST_ROOT, 'bundles'); /** Task which runs test against the size of material. */ -task('payload', ['material:clean-build'], () => { +task('payload', ['material:clean-build'], async () => { - let results = { + const results = { timestamp: Date.now(), // Material bundles material_umd: getBundleSize('material.umd.js'), @@ -28,9 +30,21 @@ task('payload', ['material:clean-build'], () => { // Print the results to the console, so we can read it from the CI. console.log('Payload Results:', JSON.stringify(results, null, 2)); - // Publish the results to firebase when it runs on Travis and not as a PR. - if (isTravisMasterBuild()) { - return publishResults(results); + if (isTravisBuild()) { + // Open a connection to Firebase. For PRs the connection will be established as a guest. + const firebaseApp = openFirebaseDashboardApp(!isTravisMasterBuild()); + const database = firebaseApp.database(); + const currentSha = process.env['TRAVIS_PULL_REQUEST_SHA'] || process.env['TRAVIS_COMMIT']; + + // Upload the payload results and calculate the payload diff in parallel. Otherwise the + // payload task will take much more time inside of Travis builds. + await Promise.all([ + uploadPayloadResults(database, currentSha, results), + calculatePayloadDiff(database, currentSha, results) + ]); + + // Disconnect database connection because Firebase otherwise prevents Gulp from exiting. + firebaseApp.delete(); } }); @@ -45,14 +59,73 @@ function getFilesize(filePath: string) { return statSync(filePath).size / 1000; } -/** Publishes the given results to the firebase database. */ -function publishResults(results: any) { - const latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim(); - const dashboardApp = openFirebaseDashboardApp(); - const database = dashboardApp.database(); +/** + * Calculates the difference between the last and current library payload. + * The results will be published as a commit status on Github. + */ +async function calculatePayloadDiff(database: any, currentSha: string, currentPayload: any) { + const authToken = process.env['FIREBASE_ACCESS_TOKEN']; + + if (!authToken) { + console.error('Cannot calculate Payload diff because there is no "FIREBASE_ACCESS_TOKEN" ' + + 'environment variable set.'); + return; + } + + const previousPayload = await getLastPayloadResults(database); + + // Calculate library sizes by combining the CDK and Material FESM 2015 bundles. + const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015; + const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015; + const deltaSize = currentSize - previousSize; + + // Update the Github status of the current commit by sending a request to the dashboard + // firebase http trigger function. + await updateGithubStatus(currentSha, deltaSize, authToken); +} + +/** + * Updates the Github status of a given commit by sending a request to a Firebase function of + * the dashboard. The function has access to the Github repository and can set status for PRs too. + */ +async function updateGithubStatus(commitSha: string, payloadDiff: number, authToken: string) { + const options = { + url: 'https://us-central1-material2-dashboard.cloudfunctions.net/payloadGithubStatus', + headers: { + 'User-Agent': 'Material2/PayloadTask', + 'auth-token': authToken, + 'commit-sha': commitSha, + 'commit-payload-diff': payloadDiff + } + }; + + return new Promise((resolve, reject) => { + request(options, (err: any, response: any, body: string) => { + if (err) { + reject(`Dashboard Error ${err.toString()}`); + } else { + console.info('Dashboard Response: ', JSON.parse(body).message); + resolve(response.statusCode); + } + }); + }); +} + +/** Uploads the current payload results to the Dashboard database. */ +async function uploadPayloadResults(database: any, currentSha: string, currentPayload: any) { + if (isTravisMasterBuild()) { + await database.ref('payloads').child(currentSha).set(currentPayload); + } +} + +/** Gets the last payload uploaded from previous Travis builds. */ +async function getLastPayloadResults(database: admin.database.Database) { + const snapshot = await database.ref('payloads') + .orderByChild('timestamp') + .limitToLast(1) + .once('value'); - // Write the results to the payloads object with the latest Git SHA as key. - return database.ref('payloads').child(latestSha).set(results) - .catch((err: any) => console.error(err)) - .then(() => dashboardApp.delete()); + // The value of the DataSnapshot is an object with the SHA as a key. Only return the + // value of the object because the SHA is not necessary. + return snapshot.val()[Object.keys(snapshot.val())[0]]; } diff --git a/tools/gulp/util/firebase.ts b/tools/gulp/util/firebase.ts index 7e852907e753..872e17bf3ef2 100644 --- a/tools/gulp/util/firebase.ts +++ b/tools/gulp/util/firebase.ts @@ -6,10 +6,19 @@ const cloudStorage = require('@google-cloud/storage'); const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json'); /** Opens a connection to the Firebase dashboard app. */ -export function openFirebaseDashboardApp() { +export function openFirebaseDashboardApp(asGuest = false) { + const databaseURL = 'https://material2-board.firebaseio.com'; + + // In some situations the service account credentials are not available and authorizing as + // a guest works fine. For example in Pull Requests the payload task just wants to read data. + if (asGuest) { + return firebase.initializeApp({ databaseURL }); + } + // Initialize the Firebase application with firebaseAdmin credentials. // Credentials need to be for a Service Account, which can be created in the Firebase console. return firebaseAdmin.initializeApp({ + databaseURL, credential: firebaseAdmin.credential.cert({ project_id: 'material2-board', client_email: 'material2-board@appspot.gserviceaccount.com', @@ -17,7 +26,6 @@ export function openFirebaseDashboardApp() { // The line-breaks need to persist in the service account private key. private_key: decode(process.env['MATERIAL2_BOARD_FIREBASE_SERVICE_KEY']) }), - databaseURL: 'https://material2-board.firebaseio.com' }); } diff --git a/tools/gulp/util/travis-ci.ts b/tools/gulp/util/travis-ci.ts index bd8e328b29c8..7267fd99029b 100644 --- a/tools/gulp/util/travis-ci.ts +++ b/tools/gulp/util/travis-ci.ts @@ -2,3 +2,7 @@ export function isTravisMasterBuild() { return process.env['TRAVIS_PULL_REQUEST'] === 'false'; } + +export function isTravisBuild() { + return process.env['TRAVIS'] === 'true'; +}