Edits history of script submission #10694 for ' [Recommended] Trigger a new pipeline (circleci)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Circleci = {
    	token: string
    }
    /**
     * [Recommended] Trigger a new pipeline
     * Trigger a pipeline given a pipeline definition ID. Supports all integrations except GitLab.
     */
    export async function main(
    	auth: Circleci,
    	provider: 'github' | 'gh' | 'bitbucket' | 'bb' | 'circleci',
    	organization: string,
    	project: string,
    	body: {
    		definition_id?: string
    		config?: { branch?: string; tag?: string }
    		checkout?: { branch?: string; tag?: string }
    		parameters?: {}
    	}
    ) {
    	const url = new URL(
    		`https://circleci.com/api/v2/project/${provider}/${organization}/${project}/pipeline/run`
    	)
    
    	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