Skip to content

chore: check in firebase function used for populating caretaker app #20120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scripts/caretaking/firebase-functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Compiled JavaScript files
**/*.js
**/*.js.map

node_modules/

firebase-debug.log
ui-debug.log
6 changes: 6 additions & 0 deletions scripts/caretaking/firebase-functions/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"functions": {
"source": "./",
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
}
}
21 changes: 21 additions & 0 deletions scripts/caretaking/firebase-functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "caretaker-firebase-functions",
"scripts": {
"build": "tsc",
"serve": "yarn build && firebase emulators:start --only functions --project test-jperrott",
"deploy": "yarn build && firebase deploy --only functions --project test-jperrott"
},
"engines": {
"node": "12"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.0.0",
"firebase-functions": "^3.8.0"
},
"devDependencies": {
"typescript": "^3.9.7",
"firebase-tools": "^8.6.0"
},
"private": true
}
50 changes: 50 additions & 0 deletions scripts/caretaking/firebase-functions/src/github-pr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

export const githubPR = functions.https.onRequest(async (req, resp) => {
const github = req.body;
const action = req.get('X-GitHub-Event');
const fullRepoName = github.repository.full_name.replace(/[\.\#\$\[\]]/g, '');
let actionTaken = 'None';
let prEffected = 'Unknown';
if (action === 'pull_request') {
const pullsRef = admin.database().ref(`/pulls/${fullRepoName}/${github.number}/github`);
const pull_request = github.pull_request;
pullsRef.update({
'branch': pull_request.base.ref,
'title': pull_request.title,
'pull_number': pull_request.number,
'state': pull_request.state,
'commit_sha': pull_request.head.sha,
'author': pull_request.user,
'labels': pull_request.labels,
'created_at': pull_request.created_at,
'updated_at': pull_request.updated_at,
});
prEffected = pull_request.number;
actionTaken = 'Updated PR';
} else if (action === 'status') {
const ref = admin
.database()
.ref(`/pulls/${fullRepoName}`)
.orderByChild('github/commit_sha')
.equalTo(github.sha);
await ref.once('value', snapshot => {
snapshot.forEach(d => {
d.child(`statuses`).ref.update({
[github.context.replace(/\W/g, '')]: {
context: github.context,
build_url: github.target_url,
status: github.state
}
});
prEffected = d.val().github.pull_number;
actionTaken = 'Updated Status';
});
});
}

const logAndOutputString = `Repo: ${fullRepoName} | PR: ${prEffected} | Action: ${actionTaken}`;
console.log(logAndOutputString);
resp.send(logAndOutputString);
});
6 changes: 6 additions & 0 deletions scripts/caretaking/firebase-functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

export {githubPR} from './github-pr';
17 changes: 17 additions & 0 deletions scripts/caretaking/firebase-functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
"typeRoots": [],
"types": []
},
"compileOnSave": true,
"include": [
"src"
]
}
Loading