|
| 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 | +}); |
0 commit comments