0

Add label watcher

by
Published Oct 17, 2025

Adds a user as a watcher to a label.

Script confluence
  • Submitted by hugo697 Bun
    Created 268 days ago
    1
    //native
    2
    type Confluence = {
    3
    	email: string
    4
    	apiToken: string
    5
    	domain: string
    6
    }
    7
    /**
    8
     * Add label watcher
    9
     * Adds a user as a watcher to a label.
    10
     */
    11
    export async function main(
    12
    	auth: Confluence,
    13
    	labelName: string,
    14
    	key: string | undefined,
    15
    	username: string | undefined,
    16
    	accountId: string | undefined,
    17
    	X_Atlassian_Token: string
    18
    ) {
    19
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/user/watch/label/${labelName}`)
    20
    	for (const [k, v] of [
    21
    		['key', key],
    22
    		['username', username],
    23
    		['accountId', accountId]
    24
    	]) {
    25
    		if (v !== undefined && v !== '' && k !== undefined) {
    26
    			url.searchParams.append(k, v)
    27
    		}
    28
    	}
    29
    	const response = await fetch(url, {
    30
    		method: 'POST',
    31
    		headers: {
    32
    			'X-Atlassian-Token': X_Atlassian_Token,
    33
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    34
    		},
    35
    		body: undefined
    36
    	})
    37
    	if (!response.ok) {
    38
    		const text = await response.text()
    39
    		throw new Error(`${response.status} ${text}`)
    40
    	}
    41
    	return await response.text()
    42
    }
    43