Edits history of script submission #15227 for ' Add new custom content permission to space (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Add new custom content permission to space
     * Adds new custom content permission to space.
    
    If the permission to be added is a group permission, the group can be identified
    by its group name or group id.
    
    Note: Only apps can access this REST resource and only make changes to the respective app permissions.
    
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    'Admin' permission for the space.
     */
    export async function main(
    	auth: Confluence,
    	spaceKey: string,
    	body: {
    		subject: { type: 'user' | 'group'; identifier: string }
    		operations: {
    			key: 'read' | 'create' | 'delete'
    			target: string
    			access: false | true
    		}[]
    	}
    ) {
    	const url = new URL(
    		`https://${auth.domain}/wiki/rest/api/space/${spaceKey}/permission/custom-content`
    	)
    
    	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