33import * as firebaseFunctions from 'firebase-functions' ;
44import * as firebaseAdmin from 'firebase-admin' ;
55
6- import { verifyJWTAndUpdateData } from './data ' ;
6+ import { verifyJwtAndTransferResultToTrustedLocation } from './verify-and-copy-report ' ;
77import { convertGoldenImagesToData } from './image_data' ;
88import { convertTestImageDataToFiles } from './data_image' ;
99import { copyTestImagesToGoldens } from './test_goldens' ;
1010import { updateGithubStatus } from './github' ;
1111
1212/**
1313 * Usage: Firebase functions only accept javascript file index.js
14- * tsc
14+ * tsc -p tools/screenshot-test/functions/tsconfig.json
15+ * cd functions
16+ * npm install
1517 * firebase deploy --only functions
1618 *
1719 *
1820 * Data and images handling for Screenshot test.
1921 *
20- * All users can post data to temporary folder. These Functions will check the data with JsonWebToken and
21- * move the valid data out of temporary folder.
22+ * All users can post data to temporary folder. These Functions will check the data with
23+ * JsonWebToken and move the valid data out of temporary folder.
2224 *
2325 * For valid data posted to database /$temp/screenshot/reports/$prNumber/$secureToken, move it to
2426 * /screenshot/reports/$prNumber.
25- * These are data for screenshot results (success or failure), GitHub PR/commit and TravisCI job information
27+ * These are data for screenshot results (success or failure), GitHub PR/commit and TravisCI job
28+ * information.
2629 *
27- * For valid image results written to database /$temp/screenshot/images/$prNumber/$secureToken/, save the image
28- * data to image files and upload to google cloud storage under location /screenshots/$prNumber
29- * These are screenshot test result images, and difference images generated from screenshot comparison.
30+ * For valid image results written to database /$temp/screenshot/images/$prNumber/$secureToken/,
31+ * save the image data to image files and upload to google cloud storage under
32+ * location /screenshots/$prNumber
33+ * These are screenshot test result images, and difference images generated from screenshot
34+ * comparison.
3035 *
31- * For golden images uploaded to /goldens, read the data from images files and write the data to Firebase database
32- * under location /screenshot/goldens
33- * Screenshot tests can only read restricted database data with no credentials, and they cannot access
34- * Google Cloud Storage. Therefore we copy the image data to database to make it available to screenshot tests.
36+ * For golden images uploaded to /goldens, read the data from images files and write the data to
37+ * Firebase database under location /screenshot/goldens
38+ * Screenshot tests can only read restricted database data with no credentials, and they cannot
39+ * access.
40+ * Google Cloud Storage. Therefore we copy the image data to database to make it available to
41+ * screenshot tests.
3542 *
36- * The JWT is stored in the data path, so every write to database needs a valid JWT to be copied to database/storage.
43+ * The JWT is stored in the data path, so every write to database needs a valid JWT to be copied to
44+ * database/storage.
3745 * All invalid data will be removed.
3846 * The JWT has 3 parts: header, payload and signature. These three parts are joint by '/' in path.
3947 */
@@ -65,11 +73,11 @@ const trustedReportPath = `screenshot/reports/{prNumber}`;
6573 * sha (github PR info), result (true or false for all the tests), travis job number
6674 */
6775const testDataPath = `${ reportPath } /{dataType}` ;
68- exports . testData = firebaseFunctions . database . ref ( testDataPath )
76+ export let testData = firebaseFunctions . database . ref ( testDataPath )
6977 . onWrite ( ( event : any ) => {
7078 const dataType = event . params . dataType ;
7179 if ( dataTypes . includes ( dataType ) ) {
72- return verifyJWTAndUpdateData ( event , dataType ) ;
80+ return verifyJwtAndTransferResultToTrustedLocation ( event , dataType ) ;
7381 }
7482} ) ;
7583
@@ -79,9 +87,9 @@ exports.testData = firebaseFunctions.database.ref(testDataPath)
7987 * Data copied: test result for each file/test with ${filename}. The value should be true or false.
8088 */
8189const testResultsPath = `${ reportPath } /results/{filename}` ;
82- exports . testResults = firebaseFunctions . database . ref ( testResultsPath )
90+ export let testResults = firebaseFunctions . database . ref ( testResultsPath )
8391 . onWrite ( ( event : any ) => {
84- return verifyJWTAndUpdateData ( event , `results/${ event . params . filename } ` ) ;
92+ return verifyJwtAndTransferResultToTrustedLocation ( event , `results/${ event . params . filename } ` ) ;
8593} ) ;
8694
8795/**
@@ -90,14 +98,14 @@ exports.testResults = firebaseFunctions.database.ref(testResultsPath)
9098 * Data copied: test result images. Convert from data to image files in storage.
9199 */
92100const imageDataToFilePath = `${ imagePath } /{dataType}/{filename}` ;
93- exports . imageDataToFile = firebaseFunctions . database . ref ( imageDataToFilePath )
101+ export let imageDataToFile = firebaseFunctions . database . ref ( imageDataToFilePath )
94102 . onWrite ( convertTestImageDataToFiles ) ;
95103
96104/**
97105 * Copy valid goldens from storage /goldens/ to database /screenshot/goldens/
98106 * so we can read the goldens without credentials.
99107 */
100- exports . goldenImageToData = firebaseFunctions . storage . bucket (
108+ export let goldenImageToData = firebaseFunctions . storage . bucket (
101109 firebaseFunctions . config ( ) . firebase . storageBucket ) . object ( ) . onChange ( ( event : any ) => {
102110 return convertGoldenImagesToData ( event . data . name , event . data . resourceState , event . data . bucket ) ;
103111} ) ;
@@ -107,7 +115,8 @@ exports.goldenImageToData = firebaseFunctions.storage.bucket(
107115 * Copy images from /screenshot/$prNumber/test/ to /goldens/
108116 */
109117const approveImagesPath = `${ trustedReportPath } /approved` ;
110- exports . approveImages = firebaseFunctions . database . ref ( approveImagesPath ) . onWrite ( ( event : any ) => {
118+ export let approveImages = firebaseFunctions . database . ref ( approveImagesPath )
119+ . onWrite ( ( event : any ) => {
111120 return copyTestImagesToGoldens ( event . params . prNumber ) ;
112121} ) ;
113122
@@ -117,4 +126,5 @@ exports.approveImages = firebaseFunctions.database.ref(approveImagesPath).onWrit
117126 * The Github Status Token is set in config.secret.github
118127 */
119128const githubStatusPath = `${ trustedReportPath } /result/{sha}` ;
120- exports . githubStatus = firebaseFunctions . database . ref ( githubStatusPath ) . onWrite ( updateGithubStatus ) ;
129+ export let githubStatus = firebaseFunctions . database . ref ( githubStatusPath )
130+ . onWrite ( updateGithubStatus ) ;
0 commit comments