Edits history of script submission #10635 for ' Creates policy bundle for the context (circleci)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Circleci = {
    	token: string
    }
    /**
     * Creates policy bundle for the context
     * This endpoint replaces the current policy bundle with the provided policy bundle
     */
    export async function main(
    	auth: Circleci,
    	ownerID: string,
    	context: string,
    	dry: string | undefined,
    	body: { policies?: {} }
    ) {
    	const url = new URL(
    		`https://circleci.com/api/v2/owner/${ownerID}/context/${context}/policy-bundle`
    	)
    	for (const [k, v] of [['dry', dry]]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			'Circle-Token': auth.token
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 536 days ago