GitHub native trigger template script

Handles GitHub webhook payloads dispatched by GitHub native triggers

Script github Verified

by hugo989 ยท 4/17/2026

The script

Submitted by hugo989 Bun
Verified 21 days ago
1
export async function main(
2
  event: string,
3
  delivery_id: string,
4
  payload: Record<string, any>
5
) {
6
  // `event` is the GitHub event type (e.g. "push", "pull_request", "issues")
7
  // `delivery_id` is the unique ID for this webhook delivery
8
  // `payload` is the full webhook payload from GitHub
9
  // See https://docs.github.com/en/webhooks/webhook-events-and-payloads
10

11
  switch (event) {
12
    case "push":
13
      return handlePush(payload)
14
    case "pull_request":
15
      return handlePullRequest(payload)
16
    case "issues":
17
      return handleIssue(payload)
18
    default:
19
      return {
20
        event,
21
        action: payload.action,
22
        repository: payload.repository?.full_name,
23
        sender: payload.sender?.login,
24
      }
25
  }
26
}
27

28
function handlePush(payload: Record<string, any>) {
29
  return {
30
    event: "push",
31
    ref: payload.ref,
32
    repository: payload.repository?.full_name,
33
    pusher: payload.pusher?.name,
34
    commits: payload.commits?.map((c: any) => ({
35
      id: c.id?.substring(0, 7),
36
      message: c.message,
37
      author: c.author?.name,
38
    })),
39
  }
40
}
41

42
function handlePullRequest(payload: Record<string, any>) {
43
  return {
44
    event: "pull_request",
45
    action: payload.action,
46
    number: payload.pull_request?.number,
47
    title: payload.pull_request?.title,
48
    author: payload.pull_request?.user?.login,
49
    repository: payload.repository?.full_name,
50
    base: payload.pull_request?.base?.ref,
51
    head: payload.pull_request?.head?.ref,
52
  }
53
}
54

55
function handleIssue(payload: Record<string, any>) {
56
  return {
57
    event: "issues",
58
    action: payload.action,
59
    number: payload.issue?.number,
60
    title: payload.issue?.title,
61
    author: payload.issue?.user?.login,
62
    repository: payload.repository?.full_name,
63
    labels: payload.issue?.labels?.map((l: any) => l.name),
64
  }
65
}
66