Edits history of script submission #10658 for ' Get all branches for a project (circleci)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Circleci = {
    	token: string
    }
    /**
     * Get all branches for a project
     * Get a list of all branches for a specified project. The list will only contain branches currently available within Insights. The maximum number of branches returned by this endpoint is 5,000.
     */
    export async function main(
    	auth: Circleci,
    	project_slug: string,
    	workflow_name: string | undefined
    ) {
    	const url = new URL(`https://circleci.com/api/v2/insights/${project_slug}/branches`)
    	for (const [k, v] of [['workflow-name', workflow_name]]) {
    		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