Edits history of script submission #10672 for ' Get summary metrics with trends for the entire org, and for each project. (circleci)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Circleci = {
    	token: string
    }
    /**
     * Get summary metrics with trends for the entire org, and for each project.
     * Gets aggregated summary metrics with trends for the entire org.
                  Also gets aggregated metrics and trends for each project belonging to the org.
     */
    export async function main(
    	auth: Circleci,
    	org_slug: string,
    	reporting_window:
    		| 'last-7-days'
    		| 'last-90-days'
    		| 'last-24-hours'
    		| 'last-30-days'
    		| 'last-60-days'
    		| undefined,
    	project_names: string | undefined
    ) {
    	const url = new URL(`https://circleci.com/api/v2/insights/${org_slug}/summary`)
    	for (const [k, v] of [
    		['reporting-window', reporting_window],
    		['project-names', project_names]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			'Circle-Token': auth.token
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 536 days ago