-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfluence.ts
More file actions
96 lines (85 loc) · 2.26 KB
/
Copy pathconfluence.ts
File metadata and controls
96 lines (85 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { env } from "../env.mjs"
const CONFLUENCE_PAGE_ID = "3083567105"
function isConfluenceConfigured(): boolean {
return !!(
env.CONFLUENCE_CLOUD_ID &&
env.CONFLUENCE_EMAIL &&
env.CONFLUENCE_API_TOKEN
)
}
export interface ConfluencePage {
id: string
title: string
body: {
view?: { value: string }
storage?: { value: string }
}
version?: {
createdAt: string
number: number
}
_links?: {
webui?: string
}
}
export interface ConfluenceFetchResult {
success: true
page: ConfluencePage
}
export interface ConfluenceFetchError {
success: false
error: string
status?: number
}
export type ConfluenceFetchResponse =
| ConfluenceFetchResult
| ConfluenceFetchError
export async function fetchConfluencePage(): Promise<ConfluenceFetchResponse> {
if (!isConfluenceConfigured()) {
return {
success: false,
error:
"Confluence is not configured. Set CONFLUENCE_CLOUD_ID, CONFLUENCE_EMAIL, and CONFLUENCE_API_TOKEN.",
}
}
const baseUrl = `https://api.atlassian.com/ex/confluence/${env.CONFLUENCE_CLOUD_ID}`
const auth = Buffer.from(
`${env.CONFLUENCE_EMAIL}:${env.CONFLUENCE_API_TOKEN}`
).toString("base64")
const headers = {
Authorization: `Basic ${auth}`,
Accept: "application/json",
}
async function fetchWithFormat(
bodyFormat: "view" | "storage"
): Promise<ConfluenceFetchResponse> {
const url = `${baseUrl}/wiki/api/v2/pages/${CONFLUENCE_PAGE_ID}?body-format=${bodyFormat}`
const response = await fetch(url, { headers })
if (!response.ok) {
return {
success: false,
error: `Confluence API error (${bodyFormat}): ${response.status} ${response.statusText}`,
status: response.status,
}
}
const page = (await response.json()) as ConfluencePage
return { success: true, page }
}
try {
let result = await fetchWithFormat("view")
if (
result.success &&
!result.page.body?.view?.value &&
!result.page.body?.storage?.value
) {
result = await fetchWithFormat("storage")
}
return result
} catch (err) {
const message = err instanceof Error ? err.message : "Unknown error"
return {
success: false,
error: `Failed to fetch Confluence page: ${message}`,
}
}
}