Edits history of script submission #13938 for ' Resolve a Git Branch to a Xata branch (xata)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xata = {
    	apiKey: string
    	workspaceUrl: string
    }
    /**
     * Resolve a Git Branch to a Xata branch
     * In order to resolve the database branch, the following algorithm is used:
    * if the `gitBranch` was provided and is found in the git branches mapping, the associated Xata branch is returned
    * else, if a Xata branch with the exact same name as `gitBranch` exists, return it
    * else, if `fallbackBranch` is provided and a branch with that name exists, return it
    * else, return the default branch of the DB (`main` or the first branch)
    
    Example call:
    
    ```json
    // GET https://tutorial-ng7s8c.
     */
    export async function main(
    	auth: Xata,
    	db_name: string,
    	gitBranch: string | undefined,
    	fallbackBranch: string | undefined
    ) {
    	const url = new URL(`${auth.workspaceUrl}/dbs/${db_name}/resolveBranch`)
    	for (const [k, v] of [
    		['gitBranch', gitBranch],
    		['fallbackBranch', fallbackBranch]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago