Skip to content

Commit

Permalink
Remove firebase, use firebase-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Jan 27, 2017
1 parent 45e9e51 commit 49a05d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@angular/http": "^2.2.0",
"@angular/platform-browser": "^2.2.0",
"core-js": "^2.4.1",
"firebase": "^3.6.7",
"google-cloud": "^0.45.1",
"image-diff": "^1.6.3",
"rxjs": "5.0.0-beta.12",
Expand Down
10 changes: 7 additions & 3 deletions tools/gulp/task_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,19 @@ export function isTravisPushBuild() {
return process.env['TRAVIS_PULL_REQUEST'] === 'false';
}

/** Decode the token for Travis to use. */
function decode(str: string): string {
return (str || '').split('\\n').reverse().join('\\n').replace(/\\n/g, '\n');
}

/** Open Google Cloud Storage for screenshots */
export function openScreenshotsCloudStorage() {
// Enable Storage
let gcs = gcloud.storage({
projectId: 'material2-screenshots',
credentials: {
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
private_key: (process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'] || '').replace(/\\n/g, '\n')
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
},
});

Expand All @@ -253,14 +258,13 @@ export function openScreenshotsCloudStorage() {
export function openFirebaseScreenshotsDatabase() {
// Initialize the Firebase application with admin credentials.
// Credentials need to be for a Service Account, which can be created in the Firebase console.
console.log(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY']);
let screenshotApp = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert({
project_id: 'material2-screenshots',
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
// The line-breaks need to persist in the service account private key.
private_key: (process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'] || '').replace(/\\n/g, '\n')
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
}),
databaseURL: 'https://material2-screenshots.firebaseio.com'
}, 'material2-screenshots');
Expand Down
38 changes: 19 additions & 19 deletions tools/gulp/tasks/screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {task} from 'gulp';
import {readdirSync, statSync, existsSync, mkdirSync} from 'fs';
import {openScreenshotsCloudStorage, openFirebaseScreenshotsDatabase} from '../task_helpers';
import * as path from 'path';
import * as firebase from 'firebase';
import * as admin from 'firebase-admin';
const imageDiff = require('image-diff');

const SCREENSHOT_DIR = './screenshots';
Expand All @@ -25,33 +25,33 @@ task('screenshots', () => {
}
});

function updateFileResult(database: firebase.database.Database, prNumber: string,
filenameKey: string, result: boolean): firebase.Promise<void>{
function updateFileResult(database: admin.database.Database, prNumber: string,
filenameKey: string, result: boolean): admin.Promise<void>{
return database.ref(FIREBASE_REPORT).child(`${prNumber}/results/${filenameKey}`).set(result);
}

function updateResult(database: firebase.database.Database, prNumber: string,
result: boolean): firebase.Promise<void> {
function updateResult(database: admin.database.Database, prNumber: string,
result: boolean): admin.Promise<void> {
return database.ref(FIREBASE_REPORT).child(`${prNumber}/result`).set(result);
}

function updateCommit(database: firebase.database.Database,
prNumber: string): firebase.Promise<void> {
function updateCommit(database: admin.database.Database,
prNumber: string): admin.Promise<void> {
return database.ref(FIREBASE_REPORT).child(`${prNumber}/commit`)
.set(process.env['TRAVIS_COMMIT']);
}

/** Get a list of filenames from firebase database. */
function getScreenFilenames(database: firebase.database.Database): firebase.Promise<string[]> {
function getScreenFilenames(database: admin.database.Database): admin.Promise<string[]> {
return database.ref(FIREBASE_FILELIST).once('value')
.then((snapshots: firebase.database.DataSnapshot) => {
.then((snapshots: admin.database.DataSnapshot) => {
return snapshots.val();
});
}

/** Upload a list of filenames to firebase database as gold. */
function setScreenFilenames(database: firebase.database.Database,
reportKey?: string): firebase.Promise<void> {
function setScreenFilenames(database: admin.database.Database,
reportKey?: string): admin.Promise<void> {
let filenames: string[] = [];
readdirSync(SCREENSHOT_DIR).map((file: string) => {
let fullName = path.join(SCREENSHOT_DIR, file);
Expand All @@ -78,7 +78,7 @@ function setScreenFilenames(database: firebase.database.Database,
function uploadScreenshots(reportKey?: string, mode?: 'test' | 'diff') {
let bucket = openScreenshotsCloudStorage();

let promises: firebase.Promise<void>[] = [];
let promises: admin.Promise<void>[] = [];
let localDir = mode == 'diff' ? `${SCREENSHOT_DIR}/diff` : SCREENSHOT_DIR;
readdirSync(localDir).map((file: string) => {
let fileName = path.join(localDir, file);
Expand All @@ -90,7 +90,7 @@ function uploadScreenshots(reportKey?: string, mode?: 'test' | 'diff') {
promises.push(bucket.upload(fileName, { destination: destination }));
}
});
return firebase.Promise.all(promises);
return admin.Promise.all(promises);
}

/** Check whether the directory exists. If not then create one. */
Expand All @@ -102,11 +102,11 @@ function _makeDir(dirName: string) {

/** Download golds screenshots. */
function downloadAllGolds(
filenames: string[], database: firebase.database.Database,
reportKey: string): firebase.Promise<boolean> {
filenames: string[], database: admin.database.Database,
reportKey: string): admin.Promise<boolean> {
_makeDir(`${SCREENSHOT_DIR}/golds`);

return firebase.Promise.all(filenames.map((filename: string) => {
return admin.Promise.all(filenames.map((filename: string) => {
return downloadGold(filename).then(() => diffScreenshot(filename, database, reportKey));
})).then((results: boolean[]) => results.every((value: boolean) => value == true));
}
Expand All @@ -119,16 +119,16 @@ function downloadGold(filename: string): Promise<void> {
});
}

function diffScreenshot(filename: string, database: firebase.database.Database,
reportKey: string): firebase.Promise<boolean> {
function diffScreenshot(filename: string, database: admin.database.Database,
reportKey: string): admin.Promise<boolean> {
// TODO(tinayuangao): Run the downloads and diffs in parallel.
let goldUrl = `${SCREENSHOT_DIR}/golds/${filename}`;
let pullRequestUrl = `${SCREENSHOT_DIR}/${filename}`;
let diffUrl = `${SCREENSHOT_DIR}/diff/${filename}`;
let filenameKey = filename.replace('.screenshot.png', '');

if (existsSync(goldUrl) && existsSync(pullRequestUrl)) {
return new firebase.Promise((resolve: any, reject: any) => {
return new admin.Promise((resolve: any, reject: any) => {
imageDiff({
actualImage: pullRequestUrl,
expectedImage: goldUrl,
Expand Down

0 comments on commit 49a05d8

Please sign in to comment.