0

Get recent runs of a workflow

by
Published Dec 20, 2024

Get recent runs of a workflow.

Script circleci Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Circleci = {
3
	token: string
4
}
5
/**
6
 * Get recent runs of a workflow
7
 * Get recent runs of a workflow.
8
 */
9
export async function main(
10
	auth: Circleci,
11
	project_slug: string,
12
	workflow_name: string,
13
	all_branches: string | undefined,
14
	branch: string | undefined,
15
	page_token: string | undefined,
16
	start_date: string | undefined,
17
	end_date: string | undefined
18
) {
19
	const url = new URL(
20
		`https://circleci.com/api/v2/insights/${project_slug}/workflows/${workflow_name}`
21
	)
22
	for (const [k, v] of [
23
		['all-branches', all_branches],
24
		['branch', branch],
25
		['page-token', page_token],
26
		['start-date', start_date],
27
		['end-date', end_date]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33
	const response = await fetch(url, {
34
		method: 'GET',
35
		headers: {
36
			'Circle-Token': auth.token
37
		},
38
		body: undefined
39
	})
40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44
	return await response.json()
45
}
46