0

New or Updated Page

by
Published today

Emits pages created or updated since the last poll, tracked via Windmill state. Optionally restrict to a single space.

Scriptยท trigger confluence Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
import * as wmill from "windmill-client"
4

5
/**
6
 * New or Updated Page
7
 * Emits pages created or updated since the last poll, tracked via Windmill state. Optionally restrict to a single space.
8
 */
9
export async function main(auth: RT.Confluence, space_id: string | undefined) {
10
  const base = auth.baseUrl.replace(/\/$/, "")
11
  const lastChecked: number = (await wmill.getState()) ?? 0
12

13
  const path = space_id
14
    ? `/wiki/api/v2/spaces/${space_id}/pages`
15
    : `/wiki/api/v2/pages`
16
  const url = new URL(`${base}${path}`)
17
  url.searchParams.append("sort", "-modified-date")
18
  url.searchParams.append("limit", "50")
19

20
  const response = await fetch(url, {
21
    method: "GET",
22
    headers: {
23
      Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
24
      Accept: "application/json",
25
    },
26
  })
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const data = (await response.json()) as {
33
    results?: { version?: { createdAt?: string } }[]
34
  }
35
  const pages = data.results ?? []
36

37
  const ts = (p: { version?: { createdAt?: string } }) =>
38
    p.version?.createdAt ? Date.parse(p.version.createdAt) : 0
39
  const maxTs = pages.reduce((m, p) => Math.max(m, ts(p)), 0)
40

41
  // First run: set the watermark to the newest page seen and don't emit a backlog.
42
  if (!lastChecked) {
43
    if (maxTs) await wmill.setState(maxTs)
44
    return []
45
  }
46

47
  const fresh = pages
48
    .filter((p) => ts(p) > lastChecked)
49
    .sort((a, b) => ts(a) - ts(b))
50

51
  if (maxTs > lastChecked) {
52
    await wmill.setState(maxTs)
53
  }
54

55
  return fresh
56
}
57