Skip to content

Commit 52881b5

Browse files
committed
small fix
1 parent f404af0 commit 52881b5

2 files changed

Lines changed: 78 additions & 57 deletions

File tree

scripts/submit-stores.mjs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
105111
export 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

157158
function resolvePublishExtensionBin() {

tests/unit/release/submit-stores.test.mjs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ test('updateFirefoxVersionNotes patches release notes and compatibility for the
8989
const fetchImpl = async (url, init) => {
9090
calls.push({ url, init })
9191

92-
if (String(url).includes('/versions/?page_size=50')) {
93-
return {
94-
ok: true,
95-
async json() {
96-
return {
97-
results: [
98-
{ id: 100, version: '2.6.0' },
99-
{ id: 101, version: '2.6.1' },
100-
],
101-
}
102-
},
103-
}
104-
}
105-
10692
return {
10793
ok: true,
10894
async text() {
@@ -120,20 +106,54 @@ test('updateFirefoxVersionNotes patches release notes and compatibility for the
120106
logger: () => {},
121107
})
122108

123-
assert.equal(calls.length, 2)
109+
assert.equal(calls.length, 1)
124110
assert.equal(
125111
calls[0].url,
126-
'https://addons.mozilla.org/api/v5/addons/addon/chatgptbox/versions/?page_size=50',
112+
'https://addons.mozilla.org/api/v5/addons/addon/chatgptbox/versions/v2.6.1/',
127113
)
128-
assert.equal(
129-
calls[1].url,
130-
'https://addons.mozilla.org/api/v5/addons/addon/chatgptbox/versions/101/',
131-
)
132-
assert.equal(calls[1].init.method, 'PATCH')
133-
assert.deepEqual(JSON.parse(calls[1].init.body), {
114+
assert.equal(calls[0].init.method, 'PATCH')
115+
assert.deepEqual(JSON.parse(calls[0].init.body), {
134116
compatibility: FIREFOX_COMPATIBILITY,
135117
release_notes: {
136118
'en-US': 'https://github.com/josStorer/chatGPTBox/releases/tag/v2.6.1',
137119
},
138120
})
139121
})
122+
123+
test('updateFirefoxVersionNotes retries when the AMO version endpoint is not ready yet', async () => {
124+
const calls = []
125+
const fetchImpl = async (url, init) => {
126+
calls.push({ url, init })
127+
128+
if (calls.length < 3) {
129+
return {
130+
ok: false,
131+
status: 404,
132+
async text() {
133+
return 'not found'
134+
},
135+
}
136+
}
137+
138+
return {
139+
ok: true,
140+
async text() {
141+
return ''
142+
},
143+
}
144+
}
145+
146+
await updateFirefoxVersionNotes({
147+
extensionId: 'chatgptbox',
148+
version: '2.6.1',
149+
jwtIssuer: 'issuer',
150+
jwtSecret: 'secret',
151+
fetchImpl,
152+
logger: () => {},
153+
retryDelayMs: 0,
154+
maxAttempts: 3,
155+
})
156+
157+
assert.equal(calls.length, 3)
158+
assert.ok(calls.every((call) => call.url.endsWith('/versions/v2.6.1/')))
159+
})

0 commit comments

Comments
 (0)