0

Create a schedule

by
Published Dec 20, 2024

Not yet available to projects that use GitLab or GitHub App. Creates a schedule and returns the created schedule.

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 a schedule
7
 * Not yet available to projects that use GitLab or GitHub App. Creates a schedule and returns the created schedule.
8
 */
9
export async function main(
10
	auth: Circleci,
11
	project_slug: string,
12
	body: {
13
		name: string
14
		timetable:
15
			| {
16
					'per-hour': number
17
					'hours-of-day': number[]
18
					'days-of-week': 'TUE' | 'SAT' | 'SUN' | 'MON' | 'THU' | 'WED' | 'FRI'[]
19
					'days-of-month'?: number[]
20
					months?:
21
						| 'MAR'
22
						| 'NOV'
23
						| 'DEC'
24
						| 'JUN'
25
						| 'MAY'
26
						| 'OCT'
27
						| 'FEB'
28
						| 'APR'
29
						| 'SEP'
30
						| 'AUG'
31
						| 'JAN'
32
						| 'JUL'[]
33
			  }
34
			| {
35
					'per-hour': number
36
					'hours-of-day': number[]
37
					'days-of-month': number[]
38
					'days-of-week'?: 'TUE' | 'SAT' | 'SUN' | 'MON' | 'THU' | 'WED' | 'FRI'[]
39
					months?:
40
						| 'MAR'
41
						| 'NOV'
42
						| 'DEC'
43
						| 'JUN'
44
						| 'MAY'
45
						| 'OCT'
46
						| 'FEB'
47
						| 'APR'
48
						| 'SEP'
49
						| 'AUG'
50
						| 'JAN'
51
						| 'JUL'[]
52
			  }
53
		'attribution-actor': 'current' | 'system'
54
		parameters: {}
55
		description?: string
56
	}
57
) {
58
	const url = new URL(`https://circleci.com/api/v2/project/${project_slug}/schedule`)
59

60
	const response = await fetch(url, {
61
		method: 'POST',
62
		headers: {
63
			'Content-Type': 'application/json',
64
			'Circle-Token': auth.token
65
		},
66
		body: JSON.stringify(body)
67
	})
68
	if (!response.ok) {
69
		const text = await response.text()
70
		throw new Error(`${response.status} ${text}`)
71
	}
72
	return await response.json()
73
}
74