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

Commit ba150d2

Browse files
committed
feat(apps): handle status webhook events from github
Process and store statuses from github pull requests.
1 parent a5dd094 commit ba150d2

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

apps/functions/githubWebhook/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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",

apps/functions/githubWebhook/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as functions from 'firebase-functions';
22
import * as admin from 'firebase-admin';
33
import {handlePullRequestEvent} from './pull-request';
4+
import {handleStatusEvent} from './status';
5+
import {StatusEvent} from '@octokit/webhooks-types';
46

57
admin.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
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

apps/shared/models/server-models.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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
};

apps/shared/models/status.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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 {

0 commit comments

Comments
 (0)