File tree Expand file tree Collapse file tree 5 files changed +39
-5
lines changed
Expand file tree Collapse file tree 5 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ ts_library(
1212 ],
1313 deps = [
1414 ":webhooks" ,
15+ "@npm//@octokit/webhooks-types" ,
1516 "@npm//firebase-admin" ,
1617 "@npm//firebase-functions" ,
1718 ],
@@ -21,6 +22,7 @@ ts_library(
2122 name = "webhooks" ,
2223 srcs = [
2324 "pull-request.ts" ,
25+ "status.ts" ,
2426 ],
2527 deps = [
2628 "//apps/shared/models:server" ,
Original file line number Diff line number Diff line change 11import * as functions from 'firebase-functions' ;
22import * as admin from 'firebase-admin' ;
33import { handlePullRequestEvent } from './pull-request' ;
4+ import { handleStatusEvent } from './status' ;
5+ import { StatusEvent } from '@octokit/webhooks-types' ;
46
57admin . initializeApp ( { ...functions . firebaseConfig ( ) } ) ;
68
@@ -30,5 +32,15 @@ export const githubWebhook = functions
3032 return ;
3133 }
3234
35+ if ( request . get ( 'X-GitHub-Event' ) === 'status' ) {
36+ const statusEvent = request . body as StatusEvent ;
37+ await handleStatusEvent ( statusEvent ) ;
38+ response . send (
39+ `Handled status update for status for commit ${ statusEvent . sha } with the context ${ statusEvent . context } ` ,
40+ ) ;
41+ response . end ( ) ;
42+ return ;
43+ }
44+
3345 response . end ( ) ;
3446 } ) ;
Original file line number Diff line number Diff line change 1+ import { StatusEvent } from '@octokit/webhooks-types' ;
2+ import { firestore } from 'firebase-admin' ;
3+ // TODO(josephperrott): Remove usage of FirestoreReference and fromFirestoreReference.
4+ import {
5+ Status ,
6+ FirestoreReference ,
7+ fromFirestoreReference ,
8+ } from '../../shared/models/server-models' ;
9+
10+ export async function handleStatusEvent ( event : StatusEvent ) {
11+ const { getFirestoreRefForGithubModel, fromGithub} = Status . getGithubHelpers ( ) ;
12+ /** The pull request model for the pull request status data. */
13+ const status = fromGithub ( event ) ;
14+ /** FirestoreReference for the pull request status. */
15+ const firestoreRef = getFirestoreRefForGithubModel ( event ) as FirestoreReference < Status > ;
16+ /** The Firestore document reference for the pull request status. */
17+ const docRef = firestore ( ) . doc ( fromFirestoreReference ( firestoreRef ) ) ;
18+
19+ await docRef . set ( Status . getConverter ( ) . toFirestore ( status ) ) ;
20+ }
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ function forServer<
5050 /**
5151 * Gets the model referenced by the provided FirestoreReference.
5252 */
53- model . prototype . getByReference = function ( ref : FirestoreReference < TBase > ) {
53+ model . prototype . getByReference = async function ( ref : FirestoreReference < TBase > ) {
5454 return firestore ( ) . doc ( fromFirestoreReference ( ref ) ) . withConverter ( converter ) ;
5555 } ;
5656
@@ -64,9 +64,7 @@ function forServer<
6464 return new model ( staticModel . githubHelpers . fromGithub ( githubModel ) ) ;
6565 } ,
6666 getFirestoreRefForGithubModel ( githubModel : GithubModel ) {
67- return firestore ( ) . doc (
68- fromFirestoreReference ( staticModel . githubHelpers . buildRefString ( githubModel ) ) ,
69- ) ;
67+ return fromFirestoreReference ( staticModel . githubHelpers . buildRefString ( githubModel ) ) ;
7068 } ,
7169 } ;
7270 } ;
Original file line number Diff line number Diff line change @@ -14,7 +14,9 @@ export class Status extends GithubBaseModel<FirestoreStatus> {
1414
1515 static override githubHelpers : GithubHelperFunctions < Status , StatusEvent , FirestoreStatus > = {
1616 buildRefString ( model : StatusEvent ) {
17- return toFirestoreReference ( `githubCommit/${ model . sha } /status/${ model . context } ` ) ;
17+ return toFirestoreReference (
18+ `githubCommit/${ model . sha } /status/${ model . context . replace ( / [: / \s ] / g, '_' ) } ` ,
19+ ) ;
1820 } ,
1921 fromGithub ( model : StatusEvent ) {
2022 return {
You can’t perform that action at this time.
0 commit comments