Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
Change-Id: I413138eae8dde71bbb7e110e011536c555490410
  • Loading branch information
kroikie committed Oct 17, 2018
1 parent 3a7b59f commit 0128076
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 0 deletions.
6 changes: 6 additions & 0 deletions remote-config-diff/README.md
@@ -0,0 +1,6 @@
## Get the diff between the previous and current Remote Config templates.

With the Firebase Remote Config Cloud Function trigger you can take action when
your template has been updated.

Here the diff between the previous and current templates is logged.
7 changes: 7 additions & 0 deletions remote-config-diff/firebase.json
@@ -0,0 +1,7 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
123 changes: 123 additions & 0 deletions remote-config-diff/functions/.eslintrc.json
@@ -0,0 +1,123 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 6
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",

// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",

// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",

// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",

// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",

// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",

// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,

// Warn if return statements do not either always or never specify values
"consistent-return": 1,

// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,

// Require the use of === and !==
"eqeqeq": 2,

// Disallow the use of alert, confirm, and prompt
"no-alert": 2,

// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,

// Disallow null comparisons without type-checking operators
"no-eq-null": 2,

// Disallow the use of eval()
"no-eval": 2,

// Warn against extending native types
"no-extend-native": 1,

// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,

// Warn against unnecessary labels
"no-extra-label": 1,

// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,

// Warn against shorthand type conversions
"no-implicit-coercion": 1,

// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,

// Disallow new operators with the Function object
"no-new-func": 2,

// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,

// Disallow throwing literals as exceptions
"no-throw-literal": 2,

// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,

// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,

// Enforce return statements in getters
"getter-return": 2,

// Disallow await inside of loops
"no-await-in-loop": 2,

// Disallow comparing against -0
"no-compare-neg-zero": 2,

// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,

// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,

// Enforce return statements in callbacks of array methods
"callback-return": 2,

// Require error handling in callbacks
"handle-callback-err": 2,

// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,

// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,

// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,

//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,

// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
53 changes: 53 additions & 0 deletions remote-config-diff/functions/index.js
@@ -0,0 +1,53 @@
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const rp = require('request-promise');
const jsonDiff = require('json-diff');

admin.initializeApp();

exports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata => {
return admin.credential.applicationDefault().getAccessToken()
.then(accessTokenObj => {
return accessTokenObj.access_token;
})
.then(accessToken => {
const currentVersion = versionMetadata.versionNumber;
const templatePromises = [];
templatePromises.push(getTemplate(currentVersion, accessToken));
templatePromises.push(getTemplate(currentVersion - 1, accessToken));

return Promise.all(templatePromises);
})
.then(results => {
const currentTemplate = results[0];
const previousTemplate = results[1];

const diff = jsonDiff.diffString(previousTemplate, currentTemplate);

console.log(diff);

return null;
}).catch(error => {
console.error(error);
return null;
});
});

function getTemplate(version, accessToken) {
const options = {
uri: 'https://firebaseremoteconfig.googleapis.com/v1/projects/remote-config-function/remoteConfig',
qs: {
versionNumber: version
},
headers: {
Authorization: 'Bearer ' + accessToken
},
json: true // Automatically parses the JSON string in the response
};
return rp(options).then(resp => {
return Promise.resolve(resp);
}).catch(err => {
console.error(err);
return Promise.resolve(null);
});
}
24 changes: 24 additions & 0 deletions remote-config-diff/functions/package.json
@@ -0,0 +1,24 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "file:firebase-functions-2.0.5-remoteconfig.tgz",
"json-diff": "latest",
"request": "latest",
"request-promise": "latest"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}

0 comments on commit 0128076

Please sign in to comment.