-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathsync-docs-webhooks.ts
More file actions
133 lines (113 loc) · 2.82 KB
/
Copy pathsync-docs-webhooks.ts
File metadata and controls
133 lines (113 loc) · 2.82 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { execFileSync } from 'node:child_process'
import { docsWebhookSources } from '../src/utils/docs-webhook-sources'
type GitHubHook = {
active: boolean
config?: {
url?: string
}
events?: Array<string>
id: number
}
type DocsWebhookSource = {
refs: Array<string>
repo: string
}
const dryRun = process.argv.includes('--dry-run')
const siteUrl = (process.env.SITE_URL || 'https://tanstack.com').replace(
/\/+$/g,
'',
)
const webhookUrl = `${siteUrl}/api/github/webhook`
const webhookSecret = process.env.GITHUB_WEBHOOK_SECRET
if (!dryRun && !webhookSecret) {
throw new Error(
'GITHUB_WEBHOOK_SECRET is required to create or update repository webhooks.',
)
}
if (!dryRun && webhookSecret) {
if (webhookSecret.length < 16 || /\s/.test(webhookSecret)) {
throw new Error(
'GITHUB_WEBHOOK_SECRET must be a raw secret value, not Netlify env:get output for a secret variable.',
)
}
}
function runGh(args: Array<string>) {
return execFileSync('gh', args, {
cwd: process.cwd(),
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim()
}
function listHooks(repo: string) {
const output = runGh(['api', `repos/${repo}/hooks`])
return JSON.parse(output) as Array<GitHubHook>
}
function createWebhook(repo: string) {
if (dryRun) {
console.log(`[dry-run] create webhook for ${repo} -> ${webhookUrl}`)
return
}
runGh([
'api',
`repos/${repo}/hooks`,
'--method',
'POST',
'-F',
'name=web',
'-F',
'active=true',
'-F',
'events[]=push',
'-F',
`config[url]=${webhookUrl}`,
'-F',
'config[content_type]=json',
'-F',
`config[secret]=${webhookSecret}`,
'-F',
'config[insecure_ssl]=0',
])
console.log(`created webhook for ${repo}`)
}
function updateWebhook(repo: string, hookId: number) {
if (dryRun) {
console.log(
`[dry-run] update webhook ${hookId} for ${repo} -> ${webhookUrl}`,
)
return
}
runGh([
'api',
`repos/${repo}/hooks/${hookId}`,
'--method',
'PATCH',
'-F',
'active=true',
'-F',
'events[]=push',
'-F',
`config[url]=${webhookUrl}`,
'-F',
'config[content_type]=json',
'-F',
`config[secret]=${webhookSecret}`,
'-F',
'config[insecure_ssl]=0',
])
console.log(`updated webhook ${hookId} for ${repo}`)
}
const webhookSources = docsWebhookSources as Array<DocsWebhookSource>
console.log(`docs webhook target: ${webhookUrl}`)
console.log('watched repos/refs:')
for (const source of webhookSources) {
console.log(`- ${source.repo} (${source.refs.join(', ')})`)
}
for (const source of webhookSources) {
const hooks = listHooks(source.repo)
const existingHook = hooks.find((hook) => hook.config?.url === webhookUrl)
if (!existingHook) {
createWebhook(source.repo)
continue
}
updateWebhook(source.repo, existingHook.id)
}