0

Create an outbound webhook

by
Published Dec 20, 2024

Creates an outbound webhook.

Script circleci Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Circleci = {
3
	token: string
4
}
5
/**
6
 * Create an outbound webhook
7
 * Creates an outbound webhook.
8
 */
9
export async function main(
10
	auth: Circleci,
11
	body: {
12
		name: string
13
		events: 'workflow-completed' | 'job-completed'[]
14
		url: string
15
		'verify-tls': false | true
16
		'signing-secret': string
17
		scope: { id: string; type: 'project' }
18
	}
19
) {
20
	const url = new URL(`https://circleci.com/api/v2/webhook`)
21

22
	const response = await fetch(url, {
23
		method: 'POST',
24
		headers: {
25
			'Content-Type': 'application/json',
26
			'Circle-Token': auth.token
27
		},
28
		body: JSON.stringify(body)
29
	})
30
	if (!response.ok) {
31
		const text = await response.text()
32
		throw new Error(`${response.status} ${text}`)
33
	}
34
	return await response.json()
35
}
36