Skip to content

Commit 7159915

Browse files
committed
build: payload github status
* Show payload deta on PRs using the Github Statuses API.
1 parent 6121fa1 commit 7159915

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

tools/gulp/tasks/payload.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import {task} from 'gulp';
22
import {join} from 'path';
33
import {statSync} from 'fs';
44
import {DIST_ROOT} from '../constants';
5-
import {spawnSync} from 'child_process';
6-
import {isTravisMasterBuild} from '../util/travis-ci';
5+
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
76
import {openFirebaseDashboardDatabase} from '../util/firebase';
7+
import {setGithubStatus} from '../util/github';
8+
9+
// These imports lack of types.
10+
const firebase = require('firebase');
811

912
const bundlesDir = join(DIST_ROOT, 'bundles');
1013

1114
/** Task which runs test against the size of material. */
12-
task('payload', ['material:clean-build'], () => {
13-
14-
let results = {
15+
task('payload', ['material:clean-build'], async () => {
16+
const results = {
1517
timestamp: Date.now(),
1618
// Material bundles
1719
material_umd: getBundleSize('material.umd.js'),
@@ -28,11 +30,32 @@ task('payload', ['material:clean-build'], () => {
2830
// Print the results to the console, so we can read it from the CI.
2931
console.log('Payload Results:', JSON.stringify(results, null, 2));
3032

31-
// Publish the results to firebase when it runs on Travis and not as a PR.
32-
if (isTravisMasterBuild()) {
33-
return publishResults(results);
34-
}
33+
if (isTravisBuild()) {
34+
// Open a connection to Firebase. For master builds the database will use an admin account.
35+
const database = openFirebaseDashboardDatabase(isTravisMasterBuild());
36+
const currentSha = process.env['TRAVIS_COMMIT'];
37+
const previousPayload = await getLastPayloadResults(database);
38+
39+
// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
40+
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
41+
const currentSize = results.cdk_fesm_2015 + results.material_fesm_2015;
42+
const deltaSize = currentSize - previousSize;
3543

44+
setGithubStatus(process.env['TRAVIS_COMMIT'], {
45+
result: true,
46+
name: 'Library Payload',
47+
url: `https://travis-ci.org/angular/material2/jobs/${process.env['TRAVIS_JOB_ID']}`,
48+
description: `${deltaSize > 0 ? `+` : ''} ${deltaSize.toFixed(2)}KB`
49+
});
50+
51+
// Publish the results to firebase when it runs on Travis and not as a PR.
52+
if (isTravisMasterBuild()) {
53+
await database.ref('payloads').child(currentSha).set(results);
54+
}
55+
56+
// Disconnect database connection because Firebase otherwise prevents Gulp from exiting.
57+
database.goOffline();
58+
}
3659
});
3760

3861
/** Returns the size of the given library bundle. */
@@ -45,12 +68,14 @@ function getFilesize(filePath: string) {
4568
return statSync(filePath).size / 1000;
4669
}
4770

48-
/** Publishes the given results to the firebase database. */
49-
function publishResults(results: any) {
50-
let latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim();
51-
let database = openFirebaseDashboardDatabase();
71+
/** Gets the last payload uploaded from previous Travis builds. */
72+
async function getLastPayloadResults(database: admin.database.Database) {
73+
const snapshot = await database.ref('payloads')
74+
.orderByChild('timestamp')
75+
.limitToLast(1)
76+
.once('value');
5277

53-
// Write the results to the payloads object with the latest Git SHA as key.
54-
return database.ref('payloads').child(latestSha).set(results)
55-
.then(() => database.goOffline(), () => database.goOffline());
78+
// The value of the DataSnapshot is an object with the SHA as a key. Only return the
79+
// value of the object because the SHA is not necessary.
80+
return snapshot.val()[Object.keys(snapshot.val())[0]];
5681
}

tools/gulp/util/firebase.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@ const cloudStorage = require('@google-cloud/storage');
66
const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json');
77

88
/** Opens a connection to the firebase realtime database. */
9-
export function openFirebaseDashboardDatabase() {
10-
// Initialize the Firebase application with firebaseAdmin credentials.
11-
// Credentials need to be for a Service Account, which can be created in the Firebase console.
12-
firebaseAdmin.initializeApp({
13-
credential: firebaseAdmin.credential.cert({
9+
export function openFirebaseDashboardDatabase(adminLogin = true) {
10+
// Firebase client can be either the AdminSDK or the normal client. This is necessary because
11+
// for Pull Requests the service account credentials are not available on the CI.
12+
const firebaseClient = adminLogin ? firebaseAdmin : firebase;
13+
const config: any = {
14+
databaseURL: 'https://material2-dashboard.firebaseio.com'
15+
};
16+
17+
if (adminLogin) {
18+
// Credentials need to be for a Service Account, which can be created in the Firebase console.
19+
config['credential'] = firebaseAdmin.credential.cert({
1420
project_id: 'material2-dashboard',
1521
client_email: 'firebase-adminsdk-ch1ob@material2-dashboard.iam.gserviceaccount.com',
1622
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
1723
// The line-breaks need to persist in the service account private key.
1824
private_key: decode(process.env['MATERIAL2_FIREBASE_PRIVATE_KEY'])
19-
}),
20-
databaseURL: 'https://material2-dashboard.firebaseio.com'
21-
});
25+
});
26+
}
27+
28+
// Initialize the firebase client.
29+
firebaseClient.initializeApp(config);
2230

23-
return firebaseAdmin.database();
31+
return firebaseClient.database();
2432
}
2533

2634
/**

tools/gulp/util/travis-ci.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
export function isTravisMasterBuild() {
33
return process.env['TRAVIS_PULL_REQUEST'] === 'false';
44
}
5+
6+
export function isTravisBuild() {
7+
return process.env['TRAVIS'] === 'true';
8+
}

0 commit comments

Comments
 (0)