Skip to content

Commit 5c564b3

Browse files
authored
extract registry from url for other ecosystems (#1520)
1 parent 9d2ccea commit 5c564b3

4 files changed

Lines changed: 103 additions & 24 deletions

File tree

__tests__/updater.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,66 @@ describe('Updater', () => {
214214
})
215215
})
216216

217+
describe('when given docker credentials with a URL and not a registry', () => {
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: 'docker_registry',
228+
url: 'https://ghcr.io/some/path',
229+
username: 'user',
230+
token: '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: 'docker_registry',
240+
registry: 'ghcr.io',
241+
url: 'https://ghcr.io/some/path'
242+
}
243+
])
244+
})
245+
})
246+
247+
describe('when given composer credentials with a URL and not a registry', () => {
248+
const jobDetails = {...mockJobDetails}
249+
250+
new Updater(
251+
'MOCK_UPDATER_IMAGE_NAME',
252+
'MOCK_PROXY_IMAGE_NAME',
253+
mockApiClient,
254+
jobDetails,
255+
[
256+
{
257+
type: 'composer_repository',
258+
url: 'https://example.com/some/path',
259+
username: 'user',
260+
token: 'token'
261+
}
262+
],
263+
workingDirectory
264+
)
265+
266+
it('generates credentials metadata with the registry from the URL', () => {
267+
expect(jobDetails['credentials-metadata']).toEqual([
268+
{
269+
type: 'composer_repository',
270+
registry: 'example.com',
271+
url: 'https://example.com/some/path'
272+
}
273+
])
274+
})
275+
})
276+
217277
describe('when given npm_registry credentials with a URL and not a registry, but the URL is malformed', () => {
218278
const jobDetails = {...mockJobDetails}
219279

dist/main/index.js

Lines changed: 20 additions & 11 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: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,7 @@ export class Updater {
7676
if (credential.url !== undefined) {
7777
obj.url = credential.url
7878
}
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-
}
79+
this.setRegistryFromUrl(obj, credential)
9180
if (credential['index-url'] !== undefined) {
9281
obj['index-url'] = credential['index-url']
9382
}
@@ -115,6 +104,27 @@ export class Updater {
115104
return result
116105
}
117106

107+
private setRegistryFromUrl(obj: Credential, credential: Credential): void {
108+
const typesThatUseRegistryAsHost = [
109+
'npm_registry',
110+
'composer_repository',
111+
'docker_registry'
112+
]
113+
114+
if (!typesThatUseRegistryAsHost.includes(credential.type)) {
115+
return
116+
}
117+
118+
if (!credential.registry && credential.url) {
119+
try {
120+
obj.registry = new URL(credential.url).hostname
121+
} catch {
122+
// If the URL is invalid, we skip setting the registry
123+
// as it will fall back to the default registry for the given type (e.g., npm, Docker, or Composer).
124+
}
125+
}
126+
}
127+
118128
private async runUpdate(proxy: Proxy): Promise<void> {
119129
const name = `dependabot-job-${this.apiClient.params.jobId}`
120130
const container = await this.createContainer(proxy, name, {

0 commit comments

Comments
 (0)