Edits history of script submission #15229 for ' Add space watcher (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Add space watcher
     * Adds a user as a watcher to a space.
     */
    export async function main(
    	auth: Confluence,
    	spaceKey: string,
    	key: string | undefined,
    	username: string | undefined,
    	accountId: string | undefined,
    	X_Atlassian_Token: string
    ) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/user/watch/space/${spaceKey}`)
    	for (const [k, v] of [
    		['key', key],
    		['username', username],
    		['accountId', accountId]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'X-Atlassian-Token': X_Atlassian_Token,
    			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.text()
    }
    

    Submitted by hugo697 235 days ago