0

Get metrics and trends for workflows

by
Published Dec 20, 2024

Get the metrics and trends for a particular workflow on a single branch or all branches

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 metrics and trends for workflows
7
 * Get the metrics and trends for a particular workflow on a single branch or all branches
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
) {
16
	const url = new URL(
17
		`https://circleci.com/api/v2/insights/${project_slug}/workflows/${workflow_name}/summary`
18
	)
19
	for (const [k, v] of [
20
		['all-branches', all_branches],
21
		['branch', branch]
22
	]) {
23
		if (v !== undefined && v !== '' && k !== undefined) {
24
			url.searchParams.append(k, v)
25
		}
26
	}
27
	const response = await fetch(url, {
28
		method: 'GET',
29
		headers: {
30
			'Circle-Token': auth.token
31
		},
32
		body: undefined
33
	})
34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38
	return await response.json()
39
}
40