Edits history of script submission #15243 for ' Create relationship (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Create relationship
     * Creates a relationship between two entities (user, space, content).
     */
    export async function main(
    	auth: Confluence,
    	relationName: string,
    	sourceType: 'user' | 'content' | 'space',
    	sourceKey: string,
    	targetType: 'user' | 'content' | 'space',
    	targetKey: string,
    	sourceStatus: string | undefined,
    	targetStatus: string | undefined,
    	sourceVersion: string | undefined,
    	targetVersion: string | undefined
    ) {
    	const url = new URL(
    		`https://${auth.domain}/wiki/rest/api/relation/${relationName}/from/${sourceType}/${sourceKey}/to/${targetType}/${targetKey}`
    	)
    	for (const [k, v] of [
    		['sourceStatus', sourceStatus],
    		['targetStatus', targetStatus],
    		['sourceVersion', sourceVersion],
    		['targetVersion', targetVersion]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'PUT',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago