Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Jun 8, 2017
1 parent 32f78ed commit d7b618b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
20 changes: 15 additions & 5 deletions tools/gulp/tasks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {task} from 'gulp';
import {join} from 'path';
import {statSync} from 'fs';
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
import {openFirebaseDashboardApp} from '../util/firebase';
import {buildConfig} from '../packaging/build-config';
import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase';

// These imports lack of type definitions.
const request = require('request');
Expand Down Expand Up @@ -33,7 +33,9 @@ task('payload', ['material:clean-build'], async () => {

if (isTravisBuild()) {
// Open a connection to Firebase. For PRs the connection will be established as a guest.
const firebaseApp = openFirebaseDashboardApp(!isTravisMasterBuild());
const firebaseApp = isTravisMasterBuild() ?
openFirebaseDashboardApp() :
openFirebaseDashboardAppAsGuest();
const database = firebaseApp.database();
const currentSha = process.env['TRAVIS_PULL_REQUEST_SHA'] || process.env['TRAVIS_COMMIT'];

Expand Down Expand Up @@ -75,6 +77,12 @@ async function calculatePayloadDiff(database: any, currentSha: string, currentPa

const previousPayload = await getLastPayloadResults(database);

if (!previousPayload) {
console.warn('There are no previous payload results uploaded. Cannot calculate payload ' +
'difference for this job');
return;
}

// 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;
Expand Down Expand Up @@ -126,7 +134,9 @@ async function getLastPayloadResults(database: admin.database.Database) {
.limitToLast(1)
.once('value');

// 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]];
// 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.hasChildren() ? results[Object.keys(results)[0]] : null;
}
20 changes: 10 additions & 10 deletions tools/gulp/util/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ const cloudStorage = require('@google-cloud/storage');
// Firebase configuration for the Screenshot project. Use the config from the screenshot functions.
const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json');

/** Opens a connection to the Firebase dashboard app. */
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 });
}
/** Database URL of the dashboard firebase project.*/
const dashboardDatabaseUrl = 'https://material2-board.firebaseio.com';

/** Opens a connection to the Firebase dashboard app using a service account. */
export function openFirebaseDashboardApp(asGuest = false) {
// 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,
databaseURL: dashboardDatabaseUrl,
credential: firebaseAdmin.credential.cert({
project_id: 'material2-board',
client_email: 'material2-board@appspot.gserviceaccount.com',
Expand All @@ -29,6 +24,11 @@ export function openFirebaseDashboardApp(asGuest = false) {
});
}

/** Opens a connection to the Firebase dashboard app with no authentication. */
export function openFirebaseDashboardAppAsGuest() {
return firebase.initializeApp({ databaseURL: dashboardDatabaseUrl });
}

/**
* Open Google Cloud Storage for screenshots.
* The files uploaded to google cloud are also available to firebase storage.
Expand Down

0 comments on commit d7b618b

Please sign in to comment.