File tree Expand file tree Collapse file tree 9 files changed +76
-7
lines changed
Expand file tree Collapse file tree 9 files changed +76
-7
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ ts_library(
1717 "index.ts" ,
1818 ],
1919 deps = [
20+ "//apps/functions/githubWebhook" ,
2021 "@npm//firebase-functions" ,
2122 ],
2223)
@@ -26,7 +27,7 @@ esbuild(
2627 entry_points = [
2728 "index.ts" ,
2829 ],
29- format = "cjs " ,
30+ format = "esm " ,
3031 visibility = ["//apps:__pkg__" ],
3132 deps = [
3233 ":functions" ,
Original file line number Diff line number Diff line change 1+ load ("//tools:defaults.bzl" , "ts_library" )
2+
3+ package (default_visibility = ["//visibility:private" ])
4+
5+ ts_library (
6+ name = "githubWebhook" ,
7+ srcs = [
8+ "index.ts" ,
9+ ],
10+ visibility = [
11+ "//apps/functions:__pkg__" ,
12+ ],
13+ deps = [
14+ ":webhooks" ,
15+ "@npm//firebase-admin" ,
16+ "@npm//firebase-functions" ,
17+ ],
18+ )
19+
20+ ts_library (
21+ name = "webhooks" ,
22+ srcs = [
23+ "pull-request.ts" ,
24+ ],
25+ deps = [
26+ "//apps/shared/models:server" ,
27+ "@npm//@octokit/webhooks-types" ,
28+ ],
29+ )
Original file line number Diff line number Diff line change 1+ import * as functions from 'firebase-functions' ;
2+ import * as admin from 'firebase-admin' ;
3+ import { handlePullRequestEvent } from './pull-request' ;
4+
5+ admin . initializeApp ( { ...functions . firebaseConfig ( ) } ) ;
6+
7+ /**
8+ * Firebase function to handle all incoming webhooks from Github. This function checks the incoming
9+ * webhook to ensure it is a valid request from Github, and then delegates processing of a payload
10+ * to one of the other githubWebhook functions.
11+ */
12+ export const githubWebhook = functions
13+ . runWith ( { invoker : 'public' , timeoutSeconds : 30 , minInstances : 1 } )
14+ . https . onRequest ( async ( request , response ) => {
15+ if ( request . method !== 'POST' ) {
16+ response . status ( 405 ) ;
17+ response . send ( 'Requests must be made using the POST method.' ) ;
18+ return ;
19+ }
20+ if ( request . headers [ 'content-type' ] !== 'application/json' ) {
21+ response . status ( 415 ) ;
22+ response . send ( 'Request payloads must be "application/json".' ) ;
23+ return ;
24+ }
25+
26+ if ( request . get ( 'X-GitHub-Event' ) === 'pull_request' ) {
27+ await handlePullRequestEvent ( request . body ) ;
28+ response . send ( `Handled pull request update for pr #${ request . body . pull_request . number } ` ) ;
29+ response . end ( ) ;
30+ return ;
31+ }
32+
33+ response . end ( ) ;
34+ } ) ;
Original file line number Diff line number Diff line change 1+ import { PullRequestEvent } from '@octokit/webhooks-types' ;
2+ import { PullRequest } from '../../shared/models/server-models' ;
3+
4+ export async function handlePullRequestEvent ( event : PullRequestEvent ) {
5+ const docRef = PullRequest . converter . getFirestoreRefForGithubModel ( event . pull_request ) ;
6+ const pullRequest = PullRequest . converter . fromGithub ( event . pull_request ) ;
7+ await docRef . set ( pullRequest ) ;
8+ }
Original file line number Diff line number Diff line change 1- import * as functions from 'firebase-functions ' ;
1+ export { githubWebhook } from './githubWebhook/index ' ;
Original file line number Diff line number Diff line change @@ -21,7 +21,6 @@ export const Status = forApp(GithubStatus);
2121export const Team = forApp ( GithubTeam ) ;
2222export const Check = forApp ( GithubCheck ) ;
2323
24-
2524/**
2625 * Mixin for models, allowing them to be used in web app environments leveraging Firestore. This
2726 * mixin provides the `converter` object used for reading and writing to Firestore, using the
Original file line number Diff line number Diff line change 1-
21/**
32 * Because it is less expensive to store strings than Firestore references, with no change in the
43 * value it provides, we have this local type to express this usage.
@@ -21,4 +20,3 @@ export abstract class GithubBaseModel<T> extends BaseModel<T> {
2120 /** Helper functions for translating Github objects to Firestore. */
2221 protected static githubHelpers : GithubHelperFunctions < any , any > | undefined ;
2322}
24-
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import {GithubTeam} from './team';
66import { GithubUser , isUserFromGithub } from './user' ;
77
88interface FirestorePullRequest {
9- owner : FirestoreReference < GithubUser > ;
9+ owner : FirestoreReference < GithubUser > ;
1010 repo : string ;
1111 node : string ;
1212 state : PullRequest [ 'state' ] ;
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ type WithAdminFirestoreDataConverter<Model> = Model & {
3131/**
3232 * A model which has been setup for usage in a Firebase functions (firebase-admin) environment,
3333 * with additional expectation/support for converting from a Github webhook payload.
34- */
34+ */
3535type WithAdminFirestoreDataConverterAndGithub < Model , GithubModel > = Model & {
3636 converter : AdminFirestoreDataConverter < Model > & {
3737 fromGithub : ( model : GithubModel ) => Model ;
You can’t perform that action at this time.
0 commit comments