0

Get audit records for time period

by
Published Oct 17, 2025

Returns records from the audit log, for a time period back from the current date.

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
 * Get audit records for time period
9
 * Returns records from the audit log, for a time period back from the current
10
date.
11
 */
12
export async function main(
13
	auth: Confluence,
14
	number: string | undefined,
15
	units:
16
		| 'NANOS'
17
		| 'MICROS'
18
		| 'MILLIS'
19
		| 'SECONDS'
20
		| 'MINUTES'
21
		| 'HOURS'
22
		| 'HALF_DAYS'
23
		| 'DAYS'
24
		| 'WEEKS'
25
		| 'MONTHS'
26
		| 'YEARS'
27
		| 'DECADES'
28
		| 'CENTURIES'
29
		| undefined,
30
	searchString: string | undefined,
31
	start: string | undefined,
32
	limit: string | undefined
33
) {
34
	const url = new URL(`https://${auth.domain}/wiki/rest/api/audit/since`)
35
	for (const [k, v] of [
36
		['number', number],
37
		['units', units],
38
		['searchString', searchString],
39
		['start', start],
40
		['limit', limit]
41
	]) {
42
		if (v !== undefined && v !== '' && k !== undefined) {
43
			url.searchParams.append(k, v)
44
		}
45
	}
46
	const response = await fetch(url, {
47
		method: 'GET',
48
		headers: {
49
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
50
		},
51
		body: undefined
52
	})
53
	if (!response.ok) {
54
		const text = await response.text()
55
		throw new Error(`${response.status} ${text}`)
56
	}
57
	return await response.json()
58
}
59