@@ -97,6 +97,13 @@ export async function downloadJiraAttachments(
9797 return downloaded
9898}
9999
100+ function normalizeDomain ( domain : string ) : string {
101+ return `https://${ domain
102+ . trim ( )
103+ . replace ( / ^ h t t p s ? : \/ \/ / i, '' )
104+ . replace ( / \/ + $ / , '' ) } `. toLowerCase ( )
105+ }
106+
100107export async function getJiraCloudId ( domain : string , accessToken : string ) : Promise < string > {
101108 const response = await fetchWithRetry (
102109 'https://api.atlassian.com/oauth/token/accessible-resources' ,
@@ -116,18 +123,25 @@ export async function getJiraCloudId(domain: string, accessToken: string): Promi
116123
117124 const resources = await response . json ( )
118125
119- if ( Array . isArray ( resources ) && resources . length > 0 ) {
120- const normalizedInput = `https:// ${ domain } ` . toLowerCase ( )
121- const matchedResource = resources . find ( ( r ) => r . url . toLowerCase ( ) === normalizedInput )
126+ if ( ! Array . isArray ( resources ) || resources . length === 0 ) {
127+ throw new Error ( 'No Jira resources found' )
128+ }
122129
123- if ( matchedResource ) {
124- return matchedResource . id
125- }
130+ const normalized = normalizeDomain ( domain )
131+ const match = resources . find (
132+ ( r : { url : string } ) => r . url . toLowerCase ( ) . replace ( / \/ + $ / , '' ) === normalized
133+ )
134+
135+ if ( match ) {
136+ return match . id
126137 }
127138
128- if ( Array . isArray ( resources ) && resources . length > 0 ) {
139+ if ( resources . length === 1 ) {
129140 return resources [ 0 ] . id
130141 }
131142
132- throw new Error ( 'No Jira resources found' )
143+ throw new Error (
144+ `Could not match Jira domain "${ domain } " to any accessible resource. ` +
145+ `Available sites: ${ resources . map ( ( r : { url : string } ) => r . url ) . join ( ', ' ) } `
146+ )
133147}
0 commit comments