Skip to content

Commit ef092ed

Browse files
committed
Addressed comments.
1 parent 6f7fe77 commit ef092ed

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

functions/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const jwt = require('jsonwebtoken');
77
const fs = require('fs');
88

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

131+
/**
132+
* Handle data written to temporary folder. Validate the JWT and move the data out of
133+
* temporary folder if the token is valid.
134+
*/
127135
function handleDataChange(event, path) {
128136
// Only edit data when it is first created. Exit when the data is deleted.
129137
if (event.data.previous.exists() || !event.data.exists()) {
@@ -137,6 +145,7 @@ function handleDataChange(event, path) {
137145
return validateSecureToken(secureToken, prNumber).then((payload) => {
138146
return firebaseAdmin.database().ref().child('screenshot/reports')
139147
.child(prNumber).child(path).set(original).then(() => {
148+
// Clear the data in temporary folder after processed.
140149
return event.data.ref.parent.set(null);
141150
});
142151
}).catch((error) => {

tools/gulp/tasks/screenshots.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,13 @@ function getSecureToken() {
119119
function uploadScreenshotsData(database: firebase.database.Database,
120120
mode: 'test' | 'diff', prNumber: string) {
121121
let localDir = mode == 'diff' ? path.join(SCREENSHOT_DIR, 'diff') : SCREENSHOT_DIR;
122-
let promises: any[] = [];
123-
getLocalScreenshotFiles(localDir).forEach((file: string) => {
122+
let promises = getLocalScreenshotFiles(localDir).map((file: string) => {
124123
let fileName = path.join(localDir, file);
125124
let filenameKey = extractScreenshotName(fileName);
126125
let secureToken = getSecureToken();
127126
let data = readFileSync(fileName);
128-
promises.push(database.ref(FIREBASE_IMAGE).child(prNumber)
129-
.child(secureToken).child(mode).child(filenameKey).set(data));
127+
return database.ref(FIREBASE_IMAGE).child(prNumber)
128+
.child(secureToken).child(mode).child(filenameKey).set(data);
130129
});
131130
return Promise.all(promises);
132131
}
@@ -195,17 +194,13 @@ function updateGithubStatus(prNumber: number, result: boolean) {
195194
});
196195
}
197196

198-
/**
199-
* Upload screenshots to google cloud storage.
200-
*/
197+
/** Upload screenshots to google cloud storage. */
201198
function uploadScreenshots() {
202199
let bucket = openScreenshotsBucket();
203-
204-
let promises: any[] = [];
205-
getLocalScreenshotFiles(SCREENSHOT_DIR).forEach((file: string) => {
200+
let promises = getLocalScreenshotFiles(SCREENSHOT_DIR).map((file: string) => {
206201
let fileName = path.join(SCREENSHOT_DIR, file);
207202
let destination = `golds/${file}`;
208-
promises.push(bucket.upload(fileName, { destination: destination }));
203+
return bucket.upload(fileName, { destination: destination });
209204
});
210205
return Promise.all(promises);
211206
}

0 commit comments

Comments
 (0)