Skip to content

Commit 62e217c

Browse files
josephperrottjelbourn
authored andcommitted
chore: check in firebase function used for populating caretaker app (#20120)
* chore: update caretaker resyncing script to include new fields. New fields have been added to the synced information from each PR in the caretaker application. This change updates the resync script to also include these new fields. * chore: check in firebase function used for populating caretaker app Previously the caretaker app's firebase function were only stored locally on a computer rather than checked into github. Checking in the currently running code provides us with a bit more resilience long term around any issues with redeployments and deprecations. (cherry picked from commit a79a71d)
1 parent 37b9664 commit 62e217c

File tree

8 files changed

+4182
-1
lines changed

8 files changed

+4182
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Compiled JavaScript files
2+
**/*.js
3+
**/*.js.map
4+
5+
node_modules/
6+
7+
firebase-debug.log
8+
ui-debug.log
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"functions": {
3+
"source": "./",
4+
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "caretaker-firebase-functions",
3+
"scripts": {
4+
"build": "tsc",
5+
"serve": "yarn build && firebase emulators:start --only functions --project test-jperrott",
6+
"deploy": "yarn build && firebase deploy --only functions --project test-jperrott"
7+
},
8+
"engines": {
9+
"node": "12"
10+
},
11+
"main": "lib/index.js",
12+
"dependencies": {
13+
"firebase-admin": "^9.0.0",
14+
"firebase-functions": "^3.8.0"
15+
},
16+
"devDependencies": {
17+
"typescript": "^3.9.7",
18+
"firebase-tools": "^8.6.0"
19+
},
20+
"private": true
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as functions from 'firebase-functions';
2+
import * as admin from 'firebase-admin';
3+
4+
export const githubPR = functions.https.onRequest(async (req, resp) => {
5+
const github = req.body;
6+
const action = req.get('X-GitHub-Event');
7+
const fullRepoName = github.repository.full_name.replace(/[\.\#\$\[\]]/g, '');
8+
let actionTaken = 'None';
9+
let prEffected = 'Unknown';
10+
if (action === 'pull_request') {
11+
const pullsRef = admin.database().ref(`/pulls/${fullRepoName}/${github.number}/github`);
12+
const pull_request = github.pull_request;
13+
pullsRef.update({
14+
'branch': pull_request.base.ref,
15+
'title': pull_request.title,
16+
'pull_number': pull_request.number,
17+
'state': pull_request.state,
18+
'commit_sha': pull_request.head.sha,
19+
'author': pull_request.user,
20+
'labels': pull_request.labels,
21+
'created_at': pull_request.created_at,
22+
'updated_at': pull_request.updated_at,
23+
});
24+
prEffected = pull_request.number;
25+
actionTaken = 'Updated PR';
26+
} else if (action === 'status') {
27+
const ref = admin
28+
.database()
29+
.ref(`/pulls/${fullRepoName}`)
30+
.orderByChild('github/commit_sha')
31+
.equalTo(github.sha);
32+
await ref.once('value', snapshot => {
33+
snapshot.forEach(d => {
34+
d.child(`statuses`).ref.update({
35+
[github.context.replace(/\W/g, '')]: {
36+
context: github.context,
37+
build_url: github.target_url,
38+
status: github.state
39+
}
40+
});
41+
prEffected = d.val().github.pull_number;
42+
actionTaken = 'Updated Status';
43+
});
44+
});
45+
}
46+
47+
const logAndOutputString = `Repo: ${fullRepoName} | PR: ${prEffected} | Action: ${actionTaken}`;
48+
console.log(logAndOutputString);
49+
resp.send(logAndOutputString);
50+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as functions from 'firebase-functions';
2+
import * as admin from 'firebase-admin';
3+
4+
admin.initializeApp(functions.config().firebase);
5+
6+
export {githubPR} from './github-pr';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitReturns": true,
5+
"noUnusedLocals": true,
6+
"outDir": "lib",
7+
"sourceMap": true,
8+
"strict": true,
9+
"target": "es2017",
10+
"typeRoots": [],
11+
"types": []
12+
},
13+
"compileOnSave": true,
14+
"include": [
15+
"src"
16+
]
17+
}

0 commit comments

Comments
 (0)