豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 28d1d81

Browse files
committed
feat(apps): create githubWebhook function
Create initial function for handling githubWebhooks to store pull request information.
1 parent 82ecd90 commit 28d1d81

File tree

9 files changed

+76
-7
lines changed

9 files changed

+76
-7
lines changed

apps/functions/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

apps/functions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import * as functions from 'firebase-functions';
1+
export {githubWebhook} from './githubWebhook/index';

apps/shared/models/app-models.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const Status = forApp(GithubStatus);
2121
export const Team = forApp(GithubTeam);
2222
export 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

apps/shared/models/base.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
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-

apps/shared/models/pull-request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {GithubTeam} from './team';
66
import {GithubUser, isUserFromGithub} from './user';
77

88
interface FirestorePullRequest {
9-
owner: FirestoreReference<GithubUser>;
9+
owner: FirestoreReference<GithubUser>;
1010
repo: string;
1111
node: string;
1212
state: PullRequest['state'];

apps/shared/models/server-models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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+
*/
3535
type WithAdminFirestoreDataConverterAndGithub<Model, GithubModel> = Model & {
3636
converter: AdminFirestoreDataConverter<Model> & {
3737
fromGithub: (model: GithubModel) => Model;

0 commit comments

Comments
 (0)