|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as firebaseFunctions from 'firebase-functions'; |
| 4 | +import * as firebaseAdmin from 'firebase-admin'; |
| 5 | + |
| 6 | +import {verifyJWTAndUpdateData} from './data'; |
| 7 | +import {convertGoldenImagesToData} from './image_data'; |
| 8 | +import {convertTestImageDataToFiles} from './data_image'; |
| 9 | +import {copyTestImagesToGoldens} from './test_goldens'; |
| 10 | +import {updateGithubStatus} from './github'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Usage: Firebase functions only accept javascript file index.js |
| 14 | + * tsc |
| 15 | + * firebase deploy --only functions |
| 16 | + * |
| 17 | + * |
| 18 | + * Data and images handling for Screenshot test. |
| 19 | + * |
| 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 | + * |
| 23 | + * For valid data posted to database /$temp/screenshot/reports/$prNumber/$secureToken, move it to |
| 24 | + * /screenshot/reports/$prNumber. |
| 25 | + * These are data for screenshot results (success or failure), GitHub PR/commit and TravisCI job information |
| 26 | + * |
| 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 | + * |
| 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. |
| 35 | + * |
| 36 | + * The JWT is stored in the data path, so every write to database needs a valid JWT to be copied to database/storage. |
| 37 | + * All invalid data will be removed. |
| 38 | + * The JWT has 3 parts: header, payload and signature. These three parts are joint by '/' in path. |
| 39 | + */ |
| 40 | + |
| 41 | +// Initailize the admin app |
| 42 | +firebaseAdmin.initializeApp(firebaseFunctions.config().firebase); |
| 43 | + |
| 44 | +/** The valid data types database accepts */ |
| 45 | +const dataTypes = ['result', 'sha', 'travis']; |
| 46 | + |
| 47 | +/** The Json Web Token format. The token is stored in data path. */ |
| 48 | +const jwtFormat = '{jwtHeader}/{jwtPayload}/{jwtSignature}'; |
| 49 | + |
| 50 | +/** The temporary folder name for screenshot data that needs to be validated via JWT. */ |
| 51 | +const tempFolder = '/untrustedInbox'; |
| 52 | + |
| 53 | + |
| 54 | +/** Untrusted report data for a PR */ |
| 55 | +const reportPath = `${tempFolder}/screenshot/reports/{prNumber}/${jwtFormat}/`; |
| 56 | +/** Untrusted image data for a PR */ |
| 57 | +const imagePath = `${tempFolder}/screenshot/images/{prNumber}/${jwtFormat}/`; |
| 58 | +/** Trusted report data for a PR */ |
| 59 | +const trustedReportPath = `screenshot/reports/{prNumber}`; |
| 60 | + |
| 61 | +/** |
| 62 | + * Copy valid data from /$temp/screenshot/reports/$prNumber/$secureToken/ |
| 63 | + * to /screenshot/reports/$prNumber |
| 64 | + * Data copied: filenames(image results names), commit(github PR info), |
| 65 | + * sha (github PR info), result (true or false for all the tests), travis job number |
| 66 | + */ |
| 67 | +const testDataPath = `${reportPath}/{dataType}`; |
| 68 | +exports.testData = firebaseFunctions.database.ref(testDataPath) |
| 69 | + .onWrite((event: any) => { |
| 70 | + const dataType = event.params.dataType; |
| 71 | + if (dataTypes.includes(dataType)) { |
| 72 | + return verifyJWTAndUpdateData(event, dataType); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +/** |
| 77 | + * Copy valid data from /$temp/screenshot/reports/$prNumber/$secureToken/ |
| 78 | + * to /screenshot/reports/$prNumber |
| 79 | + * Data copied: test result for each file/test with ${filename}. The value should be true or false. |
| 80 | + */ |
| 81 | +const testResultsPath = `${reportPath}/results/{filename}`; |
| 82 | +exports.testResults = firebaseFunctions.database.ref(testResultsPath) |
| 83 | + .onWrite((event: any) => { |
| 84 | + return verifyJWTAndUpdateData(event, `results/${event.params.filename}`); |
| 85 | +}); |
| 86 | + |
| 87 | +/** |
| 88 | + * Copy valid data from database /$temp/screenshot/images/$prNumber/$secureToken/ |
| 89 | + * to storage /screenshots/$prNumber |
| 90 | + * Data copied: test result images. Convert from data to image files in storage. |
| 91 | + */ |
| 92 | +const imageDataToFilePath = `${imagePath}/{dataType}/{filename}`; |
| 93 | +exports.imageDataToFile = firebaseFunctions.database.ref(imageDataToFilePath) |
| 94 | + .onWrite(convertTestImageDataToFiles); |
| 95 | + |
| 96 | +/** |
| 97 | + * Copy valid goldens from storage /goldens/ to database /screenshot/goldens/ |
| 98 | + * so we can read the goldens without credentials. |
| 99 | + */ |
| 100 | +exports.goldenImageToData = firebaseFunctions.storage.bucket( |
| 101 | + firebaseFunctions.config().firebase.storageBucket).object().onChange((event: any) => { |
| 102 | + return convertGoldenImagesToData(event.data.name, event.data.resourceState, event.data.bucket); |
| 103 | +}); |
| 104 | + |
| 105 | +/** |
| 106 | + * Copy test result images for PR to Goldens. |
| 107 | + * Copy images from /screenshot/$prNumber/test/ to /goldens/ |
| 108 | + */ |
| 109 | +const approveImagesPath = `${trustedReportPath}/approved`; |
| 110 | +exports.approveImages = firebaseFunctions.database.ref(approveImagesPath).onWrite((event: any) => { |
| 111 | + return copyTestImagesToGoldens(event.params.prNumber); |
| 112 | +}); |
| 113 | + |
| 114 | +/** |
| 115 | + * Update github status. When the result is true, update github commit status to `success`, |
| 116 | + * otherwise update github status to `failure`. |
| 117 | + * The Github Status Token is set in config.secret.github |
| 118 | + */ |
| 119 | +const githubStatusPath = `${trustedReportPath}/result`; |
| 120 | +exports.githubStatus = firebaseFunctions.database.ref(githubStatusPath).onWrite(updateGithubStatus); |
0 commit comments