0

Get summary metrics and trends for a project across it's workflows and branches

by
Published Dec 20, 2024

Get summary metrics and trends for a project at workflow and branch level.

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