Skip to content

Commit d7b618b

Browse files
committed
Address feedback
1 parent 32f78ed commit d7b618b

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
@@ -2,8 +2,8 @@ import {task} from 'gulp';
22
import {join} from 'path';
33
import {statSync} from 'fs';
44
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
5-
import {openFirebaseDashboardApp} from '../util/firebase';
65
import {buildConfig} from '../packaging/build-config';
6+
import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase';
77

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

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

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

7678
const previousPayload = await getLastPayloadResults(database);
7779

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

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

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)