|
| 1 | +/** |
| 2 | + * Copyright 2022 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +// [START all] |
| 19 | +// [START import] |
| 20 | +// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and set up triggers. |
| 21 | +const { onConfigUpdated } = require('firebase-functions/v2/remoteConfig'); |
| 22 | +// The Firebase Admin SDK to obtain access tokens. |
| 23 | +const admin = require('firebase-admin'); |
| 24 | +admin.initializeApp(); |
| 25 | +const rp = require('request-promise'); |
| 26 | +const jsonDiff = require('json-diff'); |
| 27 | +// [END import] |
| 28 | + |
| 29 | +// [START showconfigdiff] |
| 30 | +exports.showconfigdiff = onConfigUpdated((event) => { |
| 31 | + // Obtain the access token from the admin SDK |
| 32 | + return admin.credential.applicationDefault().getAccessToken() |
| 33 | + .then(accessTokenObj => { |
| 34 | + return accessTokenObj.access_token; |
| 35 | + }) |
| 36 | + .then(accessToken => { |
| 37 | + // Get the version number from the event object |
| 38 | + const currentVersion = event.data.versionNumber; |
| 39 | + const templatePromises = []; |
| 40 | + templatePromises.push(getTemplate(currentVersion, accessToken)); |
| 41 | + templatePromises.push(getTemplate(currentVersion - 1, accessToken)); |
| 42 | + // Get the templates |
| 43 | + return Promise.all(templatePromises); |
| 44 | + }) |
| 45 | + .then(results => { |
| 46 | + const currentTemplate = results[0]; |
| 47 | + const previousTemplate = results[1]; |
| 48 | + // Figure out the differences of the templates |
| 49 | + const diff = jsonDiff.diffString(previousTemplate, currentTemplate); |
| 50 | + // Log the difference |
| 51 | + functions.logger.log(diff); |
| 52 | + |
| 53 | + return null; |
| 54 | + }).catch(error => { |
| 55 | + functions.logger.error(error); |
| 56 | + return null; |
| 57 | + }); |
| 58 | +}); |
| 59 | +// [END showconfigdiff] |
| 60 | + |
| 61 | +// [START getTemplate] |
| 62 | +function getTemplate(version, accessToken) { |
| 63 | + const options = { |
| 64 | + uri: 'https://firebaseremoteconfig.googleapis.com/v1/projects/remote-config-function/remoteConfig', |
| 65 | + qs: { |
| 66 | + versionNumber: version |
| 67 | + }, |
| 68 | + headers: { |
| 69 | + Authorization: 'Bearer ' + accessToken |
| 70 | + }, |
| 71 | + json: true // Automatically parses the JSON string in the response |
| 72 | + }; |
| 73 | + |
| 74 | + // Obtain the template from the rest API |
| 75 | + return rp(options).then(resp => { |
| 76 | + return Promise.resolve(resp); |
| 77 | + }).catch(err => { |
| 78 | + functions.logger.error(err); |
| 79 | + return Promise.resolve(null); |
| 80 | + }); |
| 81 | +} |
| 82 | +// [START getTemplate] |
| 83 | +// [END all] |
0 commit comments