0

Get summary metrics for a project's workflows

by
Published Dec 20, 2024

Get summary metrics for a project's workflows.

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 summary metrics for a project's workflows
7
 * Get summary metrics for a project's workflows.
8
 */
9
export async function main(
10
	auth: Circleci,
11
	project_slug: string,
12
	page_token: string | undefined,
13
	all_branches: string | undefined,
14
	branch: string | undefined,
15
	reporting_window:
16
		| 'last-7-days'
17
		| 'last-90-days'
18
		| 'last-24-hours'
19
		| 'last-30-days'
20
		| 'last-60-days'
21
		| undefined
22
) {
23
	const url = new URL(`https://circleci.com/api/v2/insights/${project_slug}/workflows`)
24
	for (const [k, v] of [
25
		['page-token', page_token],
26
		['all-branches', all_branches],
27
		['branch', branch],
28
		['reporting-window', reporting_window]
29
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'GET',
36
		headers: {
37
			'Circle-Token': auth.token
38
		},
39
		body: undefined
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47