0

Get summary metrics with trends for the entire org, and for each project.

by
Published Dec 20, 2024

Gets aggregated summary metrics with trends for the entire org. Also gets aggregated metrics and trends for each project belonging to the org.

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 with trends for the entire org, and for each project.
7
 * Gets aggregated summary metrics with trends for the entire org.
8
              Also gets aggregated metrics and trends for each project belonging to the org.
9
 */
10
export async function main(
11
	auth: Circleci,
12
	org_slug: string,
13
	reporting_window:
14
		| 'last-7-days'
15
		| 'last-90-days'
16
		| 'last-24-hours'
17
		| 'last-30-days'
18
		| 'last-60-days'
19
		| undefined,
20
	project_names: string | undefined
21
) {
22
	const url = new URL(`https://circleci.com/api/v2/insights/${org_slug}/summary`)
23
	for (const [k, v] of [
24
		['reporting-window', reporting_window],
25
		['project-names', project_names]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			'Circle-Token': auth.token
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44