( domain: string, accessToken: string, retryOptions?: RetryOptions )
| 168 | } |
| 169 | |
| 170 | export async function getJiraCloudId( |
| 171 | domain: string, |
| 172 | accessToken: string, |
| 173 | retryOptions?: RetryOptions |
| 174 | ): Promise<string> { |
| 175 | const response = await fetchWithRetry( |
| 176 | 'https://api.atlassian.com/oauth/token/accessible-resources', |
| 177 | { |
| 178 | method: 'GET', |
| 179 | headers: { |
| 180 | Authorization: `Bearer ${accessToken}`, |
| 181 | Accept: 'application/json', |
| 182 | }, |
| 183 | }, |
| 184 | retryOptions |
| 185 | ) |
| 186 | |
| 187 | if (!response.ok) { |
| 188 | const errorText = await response.text() |
| 189 | throw new Error(`Failed to fetch Jira accessible resources: ${response.status} - ${errorText}`) |
| 190 | } |
| 191 | |
| 192 | const resources = await response.json() |
| 193 | |
| 194 | if (!Array.isArray(resources) || resources.length === 0) { |
| 195 | throw new Error('No Jira resources found') |
| 196 | } |
| 197 | |
| 198 | const normalized = normalizeDomain(domain) |
| 199 | const match = resources.find( |
| 200 | (r: { url: string }) => r.url.toLowerCase().replace(/\/+$/, '') === normalized |
| 201 | ) |
| 202 | |
| 203 | if (match) { |
| 204 | return match.id |
| 205 | } |
| 206 | |
| 207 | if (resources.length === 1) { |
| 208 | return resources[0].id |
| 209 | } |
| 210 | |
| 211 | throw new Error( |
| 212 | `Could not match Jira domain "${domain}" to any accessible resource. ` + |
| 213 | `Available sites: ${resources.map((r: { url: string }) => r.url).join(', ')}` |
| 214 | ) |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Parse error messages from Atlassian API responses (Jira, JSM, Confluence). |
no test coverage detected