@@ -102,56 +102,57 @@ async function readResponseText(response) {
102102 }
103103}
104104
105+ function sleep ( ms ) {
106+ return new Promise ( ( resolve ) => {
107+ setTimeout ( resolve , ms )
108+ } )
109+ }
110+
105111export async function updateFirefoxVersionNotes ( {
106112 extensionId,
107113 version,
108114 jwtIssuer,
109115 jwtSecret,
110116 fetchImpl = fetch ,
111117 logger = console . log ,
118+ maxAttempts = 6 ,
119+ retryDelayMs = 10000 ,
120+ sleepImpl = sleep ,
112121} ) {
113122 const amoId = encodeURIComponent ( stripFirefoxExtensionId ( extensionId ) )
114123 const authHeader = `JWT ${ createFirefoxJwt ( jwtIssuer , jwtSecret ) } `
115- const versionsUrl = `${ AMO_BASE_URL } /api/v5/addons/addon/${ amoId } /versions/?page_size=50`
116- const versionsResponse = await fetchImpl ( versionsUrl , {
117- headers : {
118- Authorization : authHeader ,
119- } ,
120- } )
121-
122- if ( ! versionsResponse . ok ) {
123- const body = await readResponseText ( versionsResponse )
124- throw new Error ( `Failed to fetch Firefox versions: ${ versionsResponse . status } ${ body } ` )
125- }
126-
127- const versions = await versionsResponse . json ( )
128- const matchedVersion = versions . results ?. find ( ( item ) => item . version === version )
129- if ( ! matchedVersion ?. id ) {
130- throw new Error ( `Could not find Firefox AMO version ${ version } to update release notes` )
131- }
132-
124+ const amoVersion = encodeURIComponent ( `v${ version } ` )
133125 const releaseNotes = buildFirefoxReleaseNotes ( version )
134- const patchUrl = `${ AMO_BASE_URL } /api/v5/addons/addon/${ amoId } /versions/${ matchedVersion . id } /`
135- const patchResponse = await fetchImpl ( patchUrl , {
136- method : 'PATCH' ,
137- headers : {
138- Authorization : authHeader ,
139- 'Content-Type' : 'application/json' ,
140- } ,
141- body : JSON . stringify ( {
142- compatibility : FIREFOX_COMPATIBILITY ,
143- release_notes : {
144- 'en-US' : releaseNotes ,
126+ const patchUrl = `${ AMO_BASE_URL } /api/v5/addons/addon/${ amoId } /versions/${ amoVersion } /`
127+
128+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt += 1 ) {
129+ const patchResponse = await fetchImpl ( patchUrl , {
130+ method : 'PATCH' ,
131+ headers : {
132+ Authorization : authHeader ,
133+ 'Content-Type' : 'application/json' ,
145134 } ,
146- } ) ,
147- } )
135+ body : JSON . stringify ( {
136+ compatibility : FIREFOX_COMPATIBILITY ,
137+ release_notes : {
138+ 'en-US' : releaseNotes ,
139+ } ,
140+ } ) ,
141+ } )
142+
143+ if ( patchResponse . ok ) {
144+ logger ( `Updated Firefox version metadata: ${ releaseNotes } ` )
145+ return
146+ }
148147
149- if ( ! patchResponse . ok ) {
150148 const body = await readResponseText ( patchResponse )
151- throw new Error ( `Failed to update Firefox version notes: ${ patchResponse . status } ${ body } ` )
152- }
149+ if ( patchResponse . status !== 404 || attempt === maxAttempts ) {
150+ throw new Error ( `Failed to update Firefox version metadata: ${ patchResponse . status } ${ body } ` )
151+ }
153152
154- logger ( `Updated Firefox version metadata: ${ releaseNotes } ` )
153+ logger ( `Firefox AMO version ${ version } is not ready yet, retrying metadata update` )
154+ await sleepImpl ( retryDelayMs )
155+ }
155156}
156157
157158function resolvePublishExtensionBin ( ) {
0 commit comments