Edits history of script submission #15228 for ' Add new 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 permission to space
     * Adds new 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: Apps cannot access this REST resource - including when utilizing user impersonation.
    
    **[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 }
    		operation: {
    			key:
    				| 'administer'
    				| 'archive'
    				| 'copy'
    				| 'create'
    				| 'delete'
    				| 'export'
    				| 'move'
    				| 'purge'
    				| 'purge_version'
    				| 'read'
    				| 'restore'
    				| 'restrict_content'
    				| 'update'
    				| 'use'
    			target: 'page' | 'blogpost' | 'comment' | 'attachment' | 'space'
    		}
    		_links?: {}
    	}
    ) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/space/${spaceKey}/permission`)
    
    	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.json()
    }
    

    Submitted by hugo697 235 days ago