0

Create relationship

by
Published Oct 17, 2025

Creates a relationship between two entities (user, space, content).

Script confluence Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Confluence = {
3
	email: string
4
	apiToken: string
5
	domain: string
6
}
7
/**
8
 * Create relationship
9
 * Creates a relationship between two entities (user, space, content).
10
 */
11
export async function main(
12
	auth: Confluence,
13
	relationName: string,
14
	sourceType: 'user' | 'content' | 'space',
15
	sourceKey: string,
16
	targetType: 'user' | 'content' | 'space',
17
	targetKey: string,
18
	sourceStatus: string | undefined,
19
	targetStatus: string | undefined,
20
	sourceVersion: string | undefined,
21
	targetVersion: string | undefined
22
) {
23
	const url = new URL(
24
		`https://${auth.domain}/wiki/rest/api/relation/${relationName}/from/${sourceType}/${sourceKey}/to/${targetType}/${targetKey}`
25
	)
26
	for (const [k, v] of [
27
		['sourceStatus', sourceStatus],
28
		['targetStatus', targetStatus],
29
		['sourceVersion', sourceVersion],
30
		['targetVersion', targetVersion]
31
	]) {
32
		if (v !== undefined && v !== '' && k !== undefined) {
33
			url.searchParams.append(k, v)
34
		}
35
	}
36
	const response = await fetch(url, {
37
		method: 'PUT',
38
		headers: {
39
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
40
		},
41
		body: undefined
42
	})
43
	if (!response.ok) {
44
		const text = await response.text()
45
		throw new Error(`${response.status} ${text}`)
46
	}
47
	return await response.json()
48
}
49