export async function main(
event: string,
delivery_id: string,
payload: Record<string, any>
) {
// `event` is the GitHub event type (e.g. "push", "pull_request", "issues")
// `delivery_id` is the unique ID for this webhook delivery
// `payload` is the full webhook payload from GitHub
// See https://docs.github.com/en/webhooks/webhook-events-and-payloads
switch (event) {
case "push":
return handlePush(payload)
case "pull_request":
return handlePullRequest(payload)
case "issues":
return handleIssue(payload)
default:
return {
event,
action: payload.action,
repository: payload.repository?.full_name,
sender: payload.sender?.login,
}
}
}
function handlePush(payload: Record<string, any>) {
return {
event: "push",
ref: payload.ref,
repository: payload.repository?.full_name,
pusher: payload.pusher?.name,
commits: payload.commits?.map((c: any) => ({
id: c.id?.substring(0, 7),
message: c.message,
author: c.author?.name,
})),
}
}
function handlePullRequest(payload: Record<string, any>) {
return {
event: "pull_request",
action: payload.action,
number: payload.pull_request?.number,
title: payload.pull_request?.title,
author: payload.pull_request?.user?.login,
repository: payload.repository?.full_name,
base: payload.pull_request?.base?.ref,
head: payload.pull_request?.head?.ref,
}
}
function handleIssue(payload: Record<string, any>) {
return {
event: "issues",
action: payload.action,
number: payload.issue?.number,
title: payload.issue?.title,
author: payload.issue?.user?.login,
repository: payload.repository?.full_name,
labels: payload.issue?.labels?.map((l: any) => l.name),
}
}
Submitted by hugo989 21 days ago