0

Get summary metrics for a project workflow's jobs.

by
Published Dec 20, 2024

Get summary metrics for a project workflow's jobs.

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