Skip to content

Commit 9d2ccea

Browse files
authored
handle NPM metadata missing registry key (#1519)
1 parent b4e2049 commit 9d2ccea

4 files changed

Lines changed: 89 additions & 7 deletions

File tree

__tests__/updater.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,65 @@ describe('Updater', () => {
184184
})
185185
})
186186

187+
describe('when given npm_registry credentials with a URL and not a registry', () => {
188+
const jobDetails = {...mockJobDetails}
189+
190+
new Updater(
191+
'MOCK_UPDATER_IMAGE_NAME',
192+
'MOCK_PROXY_IMAGE_NAME',
193+
mockApiClient,
194+
jobDetails,
195+
[
196+
{
197+
type: 'npm_registry',
198+
url: 'https://registry.npmjs.org/some/path',
199+
username: 'npm_user',
200+
token: 'npm_token'
201+
}
202+
],
203+
workingDirectory
204+
)
205+
206+
it('generates credentials metadata with the registry from the URL', () => {
207+
expect(jobDetails['credentials-metadata']).toEqual([
208+
{
209+
type: 'npm_registry',
210+
registry: 'registry.npmjs.org',
211+
url: 'https://registry.npmjs.org/some/path'
212+
}
213+
])
214+
})
215+
})
216+
217+
describe('when given npm_registry credentials with a URL and not a registry, but the URL is malformed', () => {
218+
const jobDetails = {...mockJobDetails}
219+
220+
new Updater(
221+
'MOCK_UPDATER_IMAGE_NAME',
222+
'MOCK_PROXY_IMAGE_NAME',
223+
mockApiClient,
224+
jobDetails,
225+
[
226+
{
227+
type: 'npm_registry',
228+
url: 'not-a-url',
229+
username: 'npm_user',
230+
token: 'npm_token'
231+
}
232+
],
233+
workingDirectory
234+
)
235+
236+
it('generates credentials metadata with the registry from the URL', () => {
237+
expect(jobDetails['credentials-metadata']).toEqual([
238+
{
239+
type: 'npm_registry',
240+
url: 'not-a-url'
241+
}
242+
])
243+
})
244+
})
245+
187246
describe('when given duplicate credentials', () => {
188247
const jobDetails = {...mockJobDetails}
189248

dist/main/index.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/updater.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,27 @@ export class Updater {
7373
if (credential.registry !== undefined) {
7474
obj.registry = credential.registry
7575
}
76+
if (credential.url !== undefined) {
77+
obj.url = credential.url
78+
}
79+
if (
80+
credential.type === 'npm_registry' &&
81+
!credential.registry &&
82+
credential.url
83+
) {
84+
try {
85+
obj.registry = new URL(credential.url).hostname
86+
} catch {
87+
// If the URL is invalid, we skip setting the registry
88+
// as it will be set to the default npm registry.
89+
}
90+
}
7691
if (credential['index-url'] !== undefined) {
7792
obj['index-url'] = credential['index-url']
7893
}
7994
if (credential['env-key'] !== undefined) {
8095
obj['env-key'] = credential['env-key']
8196
}
82-
if (credential.url !== undefined) {
83-
obj.url = credential.url
84-
}
8597
if (credential.organization !== undefined) {
8698
obj.organization = credential.organization
8799
}

0 commit comments

Comments
 (0)