Edits history of script submission #15256 for ' Find target entities related to a source entity (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Find target entities related to a source entity
     * Returns all target entities that have a particular relationship to the
    source entity. Note, relationships are one way.
    
    For example, the following method finds all content that the current user
    has an 'ignore' relationship with:
    `GET /wiki/rest/api/relation/ignore/from/user/current/to/content`
    Note, 'ignore' is an example custom relationship type.
    
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    Permission to view both the target entity and source entity.
     */
    export async function main(
    	auth: Confluence,
    	relationName: string,
    	sourceType: 'user' | 'content' | 'space',
    	sourceKey: string,
    	targetType: 'user' | 'content' | 'space',
    	sourceStatus: string | undefined,
    	targetStatus: string | undefined,
    	sourceVersion: string | undefined,
    	targetVersion: string | undefined,
    	expand: string | undefined,
    	start: string | undefined,
    	limit: string | undefined
    ) {
    	const url = new URL(
    		`https://${auth.domain}/wiki/rest/api/relation/${relationName}/from/${sourceType}/${sourceKey}/to/${targetType}`
    	)
    	for (const [k, v] of [
    		['sourceStatus', sourceStatus],
    		['targetStatus', targetStatus],
    		['sourceVersion', sourceVersion],
    		['targetVersion', targetVersion],
    		['expand', expand],
    		['start', start],
    		['limit', limit]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		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