Skip to content

Commit

Permalink
Addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Mar 20, 2017
1 parent 6f7fe77 commit fbcead2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 10 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const jwt = require('jsonwebtoken');
const fs = require('fs');

/**
* Data and images handling for Screenshot test
* Data and images handling for Screenshot test.
*
* All users can post data to temporary folder. These Functions will check the data with JsonWebToken and
* move the valid data out of temporary folder.
*
* For valid data posted to database /$temp/screenshot/reports/$prNumber/$secureToken, move it to
* /screenshot/reports/$prNumber.
Expand Down Expand Up @@ -87,6 +90,7 @@ exports.copyImage = firebaseFunctions.database.ref(copyImagePath).onWrite(event
const binaryData = new Buffer(event.data.val(), 'base64').toString('binary');
fs.writeFile(tempPath, binaryData, 'binary');
return bucket.upload(tempPath, {destination: filePath}).then(() => {
// Clear the data in temporary folder after processed.
return event.data.ref.parent.set(null);
});
}).catch((error) => {
Expand Down Expand Up @@ -124,6 +128,10 @@ exports.copyGoldens = firebaseFunctions.storage.bucket(firebaseFunctions.config(
});
});

/**
* Handle data written to temporary folder. Validate the JWT and move the data out of
* temporary folder if the token is valid.
*/
function handleDataChange(event, path) {
// Only edit data when it is first created. Exit when the data is deleted.
if (event.data.previous.exists() || !event.data.exists()) {
Expand All @@ -137,6 +145,7 @@ function handleDataChange(event, path) {
return validateSecureToken(secureToken, prNumber).then((payload) => {
return firebaseAdmin.database().ref().child('screenshot/reports')
.child(prNumber).child(path).set(original).then(() => {
// Clear the data in temporary folder after processed.
return event.data.ref.parent.set(null);
});
}).catch((error) => {
Expand Down
15 changes: 6 additions & 9 deletions tools/gulp/tasks/screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ function uploadScreenshotsData(database: firebase.database.Database,
mode: 'test' | 'diff', prNumber: string) {
let localDir = mode == 'diff' ? path.join(SCREENSHOT_DIR, 'diff') : SCREENSHOT_DIR;
let promises: any[] = [];
getLocalScreenshotFiles(localDir).forEach((file: string) => {
let promises = getLocalScreenshotFiles(localDir).map((file: string) => {
let fileName = path.join(localDir, file);
let filenameKey = extractScreenshotName(fileName);
let secureToken = getSecureToken();
let data = readFileSync(fileName);
promises.push(database.ref(FIREBASE_IMAGE).child(prNumber)
.child(secureToken).child(mode).child(filenameKey).set(data));
return database.ref(FIREBASE_IMAGE).child(prNumber)
.child(secureToken).child(mode).child(filenameKey).set(data);
});
return Promise.all(promises);
}
Expand Down Expand Up @@ -195,17 +195,14 @@ function updateGithubStatus(prNumber: number, result: boolean) {
});
}

/**
* Upload screenshots to google cloud storage.
*/
/** Upload screenshots to google cloud storage. */
function uploadScreenshots() {
let bucket = openScreenshotsBucket();

let promises: any[] = [];
getLocalScreenshotFiles(SCREENSHOT_DIR).forEach((file: string) => {
let promises = getLocalScreenshotFiles(SCREENSHOT_DIR).map((file: string) => {
let fileName = path.join(SCREENSHOT_DIR, file);
let destination = `golds/${file}`;
promises.push(bucket.upload(fileName, { destination: destination }));
return bucket.upload(fileName, { destination: destination });
});
return Promise.all(promises);
}

0 comments on commit fbcead2

Please sign in to comment.