|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const functions = require('firebase-functions'); |
| 4 | +const gcs = require('@google-cloud/storage')(); |
| 5 | +const admin = require('firebase-admin'); |
| 6 | +const jwt = require('jsonwebtoken'); |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +admin.initializeApp(functions.config().firebase); |
| 10 | + |
| 11 | +const dataTypes = ['filenames', 'commit', 'result', 'sha', 'travis']; |
| 12 | +const repoSlug = functions.config().repo.slug; |
| 13 | +const secret = functions.config().secret.key; |
| 14 | +const bucket = gcs.bucket(functions.config().firebase.storageBucket); |
| 15 | + |
| 16 | +/** Copy valid data from /temp/screenshot/reports/$prNumber/$secureToken/ to /screenshot/reports/$prNumber */ |
| 17 | +exports.copyData = functions.database.ref('/temp/screenshot/reports/{prNumber}/{token1}/{token2}/{token3}/{dataType}') |
| 18 | + .onWrite(event => { |
| 19 | + const dataType = event.params.dataType; |
| 20 | + if (dataTypes.indexOf(dataType) == -1) { |
| 21 | + return; |
| 22 | + } |
| 23 | + return handleDataChange(event, dataType); |
| 24 | +}); |
| 25 | + |
| 26 | +/** Copy valid data from /temp/screenshot/reports/$prNumber/$secureToken/ to /screenshot/reports/$prNumber */ |
| 27 | +exports.copyDataResult = functions.database.ref('/temp/screenshot/reports/{prNumber}/{token1}/{token2}/{token3}/results/{filename}') |
| 28 | + .onWrite(event => { |
| 29 | + return handleDataChange(event, `results/${event.params.filename}`); |
| 30 | +}); |
| 31 | + |
| 32 | +/** Copy valid data from database /temp/screenshot/images/$prNumber/$secureToken/ to storage /screenshots/$prNumber */ |
| 33 | +exports.copyImage = functions.database.ref('/temp/screenshot/images/{prNumber}/{token1}/{token2}/{token3}/{dataType}/{filename}') |
| 34 | + .onWrite(event => { |
| 35 | + // Only edit data when it is first created. Exit when the data is deleted. |
| 36 | + if (event.data.previous.exists() || !event.data.exists()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const dataType = event.params.dataType; |
| 41 | + const prNumber = event.params.prNumber; |
| 42 | + const secureToken = `${event.params.token1}.${event.params.token2}.${event.params.token3}`; |
| 43 | + const saveFilename = `${event.params.filename}.screenshot.png`; |
| 44 | + |
| 45 | + if (dataType != 'diff' && dataType != 'test') { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + return validateSecureToken(secureToken, prNumber).then((payload) => { |
| 50 | + const tempPath = `/tmp/${dataType}-${saveFilename}` |
| 51 | + const filePath = `screenshots/${prNumber}/${dataType}/${saveFilename}`; |
| 52 | + const binaryData = new Buffer(event.data.val(), 'base64').toString('binary'); |
| 53 | + fs.writeFile(tempPath, binaryData, 'binary'); |
| 54 | + return bucket.upload(tempPath, { |
| 55 | + destination: filePath |
| 56 | + }).then(() => { |
| 57 | + return event.data.ref.parent.set(null); |
| 58 | + }); |
| 59 | + }).catch((error) => { |
| 60 | + console.error(`Invalid secure token ${secureToken} ${error}`); |
| 61 | + return event.data.ref.parent.set(null); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +/** |
| 66 | + * Copy valid goldens from storage /goldens/ to database /screenshot/goldens/ |
| 67 | + * so we can read the goldens without credentials |
| 68 | + */ |
| 69 | +exports.copyGoldens = functions.storage.bucket(functions.config().firebase.storageBucket).object().onChange(event => { |
| 70 | + const filePath = event.data.name; |
| 71 | + |
| 72 | + // Get the file name. |
| 73 | + const fileNames = filePath.split('/'); |
| 74 | + if (fileNames.length != 2 && fileNames[0] != 'goldens') { |
| 75 | + return; |
| 76 | + } |
| 77 | + const filenameKey = fileNames[1].replace('.screenshot.png', ''); |
| 78 | + |
| 79 | + if (event.data.resourceState === 'not_exists') { |
| 80 | + return admin.database().ref(`screenshot/goldens/${filenameKey}`).set(null); |
| 81 | + } |
| 82 | + |
| 83 | + // Download file from bucket. |
| 84 | + const bucket = gcs.bucket(event.data.bucket); |
| 85 | + const tempFilePath = `/tmp/${fileNames[1]}`; |
| 86 | + return bucket.file(filePath).download({ |
| 87 | + destination: tempFilePath |
| 88 | + }).then(() => { |
| 89 | + const data = fs.readFileSync(tempFilePath); |
| 90 | + return admin.database().ref(`screenshot/goldens/${filenameKey}`).set(data); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +function handleDataChange(event, path) { |
| 95 | + // Only edit data when it is first created. Exit when the data is deleted. |
| 96 | + if (event.data.previous.exists() || !event.data.exists()) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const prNumber = event.params.prNumber; |
| 101 | + const secureToken = `${event.params.token1}.${event.params.token2}.${event.params.token3}`; |
| 102 | + const original = event.data.val(); |
| 103 | + |
| 104 | + return validateSecureToken(secureToken, prNumber).then((payload) => { |
| 105 | + return admin.database().ref().child('screenshot/reports').child(prNumber).child(path).set(original).then(() => { |
| 106 | + return event.data.ref.parent.set(null); |
| 107 | + }); |
| 108 | + }).catch((error) => { |
| 109 | + console.error(`Invalid secure token ${secureToken} ${error}`); |
| 110 | + return event.data.ref.parent.set(null); |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +function validateSecureToken(token, prNumber) { |
| 115 | + return new Promise((resolve, reject) => { |
| 116 | + jwt.verify(token, secret, {issuer: 'Travis CI, GmbH'}, (err, payload) => { |
| 117 | + if (err) { |
| 118 | + reject(err.message || err); |
| 119 | + } else if (payload.slug !== repoSlug) { |
| 120 | + reject(`jwt slug invalid. expected: ${repoSlug}`); |
| 121 | + } else if (payload['pull-request'].toString() !== prNumber) { |
| 122 | + reject(`jwt pull-request invalid. expected: ${prNumber} actual: ${payload['pull-request']}`); |
| 123 | + } else { |
| 124 | + resolve(payload); |
| 125 | + } |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
0 commit comments