( domain: string, accessToken: string, retryOptions?: RetryOptions )
| 13 | } |
| 14 | |
| 15 | export async function getConfluenceCloudId( |
| 16 | domain: string, |
| 17 | accessToken: string, |
| 18 | retryOptions?: RetryOptions |
| 19 | ): Promise<string> { |
| 20 | const response = await fetchWithRetry( |
| 21 | 'https://api.atlassian.com/oauth/token/accessible-resources', |
| 22 | { |
| 23 | method: 'GET', |
| 24 | headers: { |
| 25 | Authorization: `Bearer ${accessToken}`, |
| 26 | Accept: 'application/json', |
| 27 | }, |
| 28 | }, |
| 29 | retryOptions |
| 30 | ) |
| 31 | |
| 32 | const resources = await response.json() |
| 33 | |
| 34 | if (!Array.isArray(resources) || resources.length === 0) { |
| 35 | throw new Error('No Confluence resources found') |
| 36 | } |
| 37 | |
| 38 | const normalized = `https://${normalizeConfluenceDomainHost(domain)}`.toLowerCase() |
| 39 | const match = resources.find( |
| 40 | (r: { url: string }) => r.url.toLowerCase().replace(/\/+$/, '') === normalized |
| 41 | ) |
| 42 | |
| 43 | if (match) { |
| 44 | return match.id |
| 45 | } |
| 46 | |
| 47 | if (resources.length === 1) { |
| 48 | return resources[0].id |
| 49 | } |
| 50 | |
| 51 | throw new Error( |
| 52 | `Could not match Confluence domain "${domain}" to any accessible resource. ` + |
| 53 | `Available sites: ${resources.map((r: { url: string }) => r.url).join(', ')}` |
| 54 | ) |
| 55 | } |
| 56 | |
| 57 | function decodeHtmlEntities(text: string): string { |
| 58 | let decoded = text |
no test coverage detected