-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apps): create githubWebhook function
Create initial function for handling githubWebhooks to store pull request information.
- Loading branch information
1 parent
82ecd90
commit 28d1d81
Showing
9 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:private"]) | ||
|
||
ts_library( | ||
name = "githubWebhook", | ||
srcs = [ | ||
"index.ts", | ||
], | ||
visibility = [ | ||
"//apps/functions:__pkg__", | ||
], | ||
deps = [ | ||
":webhooks", | ||
"@npm//firebase-admin", | ||
"@npm//firebase-functions", | ||
], | ||
) | ||
|
||
ts_library( | ||
name = "webhooks", | ||
srcs = [ | ||
"pull-request.ts", | ||
], | ||
deps = [ | ||
"//apps/shared/models:server", | ||
"@npm//@octokit/webhooks-types", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as admin from 'firebase-admin'; | ||
import {handlePullRequestEvent} from './pull-request'; | ||
|
||
admin.initializeApp({...functions.firebaseConfig()}); | ||
|
||
/** | ||
* Firebase function to handle all incoming webhooks from Github. This function checks the incoming | ||
* webhook to ensure it is a valid request from Github, and then delegates processing of a payload | ||
* to one of the other githubWebhook functions. | ||
*/ | ||
export const githubWebhook = functions | ||
.runWith({invoker: 'public', timeoutSeconds: 30, minInstances: 1}) | ||
.https.onRequest(async (request, response) => { | ||
if (request.method !== 'POST') { | ||
response.status(405); | ||
response.send('Requests must be made using the POST method.'); | ||
return; | ||
} | ||
if (request.headers['content-type'] !== 'application/json') { | ||
response.status(415); | ||
response.send('Request payloads must be "application/json".'); | ||
return; | ||
} | ||
|
||
if (request.get('X-GitHub-Event') === 'pull_request') { | ||
await handlePullRequestEvent(request.body); | ||
response.send(`Handled pull request update for pr #${request.body.pull_request.number}`); | ||
response.end(); | ||
return; | ||
} | ||
|
||
response.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {PullRequestEvent} from '@octokit/webhooks-types'; | ||
import {PullRequest} from '../../shared/models/server-models'; | ||
|
||
export async function handlePullRequestEvent(event: PullRequestEvent) { | ||
const docRef = PullRequest.converter.getFirestoreRefForGithubModel(event.pull_request); | ||
const pullRequest = PullRequest.converter.fromGithub(event.pull_request); | ||
await docRef.set(pullRequest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import * as functions from 'firebase-functions'; | ||
export {githubWebhook} from './githubWebhook/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters