-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathwebhook.ts
More file actions
58 lines (48 loc) · 1.56 KB
/
Copy pathwebhook.ts
File metadata and controls
58 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { createFileRoute } from '@tanstack/react-router'
import { Webhooks } from '@octokit/webhooks'
import { Octokit } from '@octokit/rest'
import { env } from '~/utils/env'
import { changesetPreview } from "~/github/changesetPreview"
const webhooks = new Webhooks({
secret: env.GITHUB_WEBHOOK_SECRET,
})
function getOctokit() {
return new Octokit({
auth: env.GITHUB_AUTH_TOKEN
})
}
// Register event handlers
webhooks.on(['pull_request.opened', 'pull_request.synchronize'], async ({ payload }) => {
console.log('PR opened:', payload.pull_request.title)
const octokit = getOctokit()
await changesetPreview({
owner: payload.pull_request.head.repo.owner!.login,
repo: payload.pull_request.head.repo.name,
ref: payload.pull_request.head.ref,
pull_number: payload.number,
octokit
})
})
webhooks.onError((error) => {
console.error('Webhook error:', error)
})
export const Route = createFileRoute('/api/github/webhook')({
server: {
handlers: {
POST: async ({ request }) => {
const signature = request.headers.get('x-hub-signature-256') ?? ''
const body = await request.text()
const isValid = await webhooks.verify(body, signature)
if (!isValid) {
return new Response('Invalid signature', { status: 401 })
}
await webhooks.receive({
id: request.headers.get('x-github-delivery') ?? '',
name: request.headers.get('x-github-event') as any,
payload: JSON.parse(body),
})
return new Response('OK', { status: 200 })
},
},
},
})