Skip to content

Commit ecbfd97

Browse files
committed
Address feedback
1 parent e524246 commit ecbfd97

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

tools/gulp/tasks/payload.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {join} from 'path';
33
import {statSync} from 'fs';
44
import {DIST_ROOT} from '../build-config';
55
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
6-
import {openFirebaseDashboardApp} from '../util/firebase';
6+
import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase';
77

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

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

@@ -74,6 +76,12 @@ async function calculatePayloadDiff(database: any, currentSha: string, currentPa
7476

7577
const previousPayload = await getLastPayloadResults(database);
7678

79+
if (!previousPayload) {
80+
console.warn('There are no previous payload results uploaded. Cannot calculate payload ' +
81+
'difference for this job');
82+
return;
83+
}
84+
7785
// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
7886
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
7987
const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015;
@@ -125,7 +133,9 @@ async function getLastPayloadResults(database: admin.database.Database) {
125133
.limitToLast(1)
126134
.once('value');
127135

128-
// The value of the DataSnapshot is an object with the SHA as a key. Only return the
129-
// value of the object because the SHA is not necessary.
130-
return snapshot.val()[Object.keys(snapshot.val())[0]];
136+
// The value of the DataSnapshot is an object with the SHA as a key. Later only the
137+
// first value of the object will be returned because the SHA is unnecessary.
138+
const results = snapshot.val();
139+
140+
return snapshot.hasChildren() ? results[Object.keys(results)[0]] : null;
131141
}

tools/gulp/util/firebase.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ const cloudStorage = require('@google-cloud/storage');
55
// Firebase configuration for the Screenshot project. Use the config from the screenshot functions.
66
const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json');
77

8-
/** Opens a connection to the Firebase dashboard app. */
9-
export function openFirebaseDashboardApp(asGuest = false) {
10-
const databaseURL = 'https://material2-board.firebaseio.com';
11-
12-
// In some situations the service account credentials are not available and authorizing as
13-
// a guest works fine. For example in Pull Requests the payload task just wants to read data.
14-
if (asGuest) {
15-
return firebase.initializeApp({ databaseURL });
16-
}
8+
/** Database URL of the dashboard firebase project.*/
9+
const dashboardDatabaseUrl = 'https://material2-board.firebaseio.com';
1710

11+
/** Opens a connection to the Firebase dashboard app using a service account. */
12+
export function openFirebaseDashboardApp(asGuest = false) {
1813
// Initialize the Firebase application with firebaseAdmin credentials.
1914
// Credentials need to be for a Service Account, which can be created in the Firebase console.
2015
return firebaseAdmin.initializeApp({
21-
databaseURL,
16+
databaseURL: dashboardDatabaseUrl,
2217
credential: firebaseAdmin.credential.cert({
2318
project_id: 'material2-board',
2419
client_email: '[email protected]',
@@ -29,6 +24,11 @@ export function openFirebaseDashboardApp(asGuest = false) {
2924
});
3025
}
3126

27+
/** Opens a connection to the Firebase dashboard app with no authentication. */
28+
export function openFirebaseDashboardAppAsGuest() {
29+
return firebase.initializeApp({ databaseURL: dashboardDatabaseUrl });
30+
}
31+
3232
/**
3333
* Open Google Cloud Storage for screenshots.
3434
* The files uploaded to google cloud are also available to firebase storage.

0 commit comments

Comments
 (0)