Edits history of script submission #15226 for ' Add member to group by groupId (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Add member to group by groupId
     * Adds a user as a member in a group represented by its groupId
    
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    User must be a site admin.
     */
    export async function main(
    	auth: Confluence,
    	groupId: string | undefined,
    	body: { accountId: string }
    ) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/group/userByGroupId`)
    	for (const [k, v] of [['groupId', groupId]]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.text()
    }
    

    Submitted by hugo697 235 days ago