generated from nhcarrigan/template
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import type { IssueCreated,
|
||||
PullRequestCreated,
|
||||
GithubPayload } from "../interfaces/github.js";
|
||||
import type { FastifyRequest, FastifyReply } from "fastify";
|
||||
|
||||
const isIssue = (body: GithubPayload): body is IssueCreated => {
|
||||
return "issue" in body;
|
||||
};
|
||||
|
||||
const isPull = (body: GithubPayload): body is PullRequestCreated => {
|
||||
return "pull_request" in body;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles a payload from a GitHub webhook.
|
||||
* @param request - The Fastify request payload.
|
||||
* @param response - The Fastify reply class.
|
||||
*/
|
||||
export const processGithubEvent = async(
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
request: FastifyRequest<{ Body: GithubPayload }>,
|
||||
response: FastifyReply,
|
||||
): Promise<void> => {
|
||||
const event = request.headers["x-github-event"];
|
||||
if (typeof event !== "string") {
|
||||
await response.status(400).
|
||||
send({ message: "Invalid GitHub event header." });
|
||||
return;
|
||||
}
|
||||
if (event === "ping") {
|
||||
await response.status(200).send({ message: "Pong!" });
|
||||
return;
|
||||
}
|
||||
const { action: _action } = request.body;
|
||||
isIssue(request.body);
|
||||
isPull(request.body);
|
||||
};
|
||||
Reference in New Issue
Block a user