Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datasource/docker): correctly split registry and repository #24186

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 49 additions & 8 deletions lib/modules/datasource/docker/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ const http = new Http(dockerDatasourceId);
jest.mock('../../../util/host-rules');

describe('modules/datasource/docker/common', () => {
beforeEach(() => {
hostRules.find.mockReturnValue({
username: 'some-username',
password: 'some-password',
});
hostRules.hosts.mockReturnValue([]);
});

describe('getRegistryRepository', () => {
it('handles local registries', () => {
const res = getRegistryRepository(
Expand Down Expand Up @@ -71,9 +63,58 @@ describe('modules/datasource/docker/common', () => {
registryHost: 'https://my.local.registry',
});
});

it('supports insecure registryUrls', () => {
hostRules.find.mockReturnValueOnce({ insecureRegistry: true });
const res = getRegistryRepository(
'prefix/image',
'my.local.registry/prefix'
);
expect(res).toStrictEqual({
dockerRepository: 'prefix/prefix/image',
registryHost: 'http://my.local.registry',
});
});

it.each([
{
name: 'strimzi-kafka-operator',
url: 'https://quay.io/strimzi-helm/',
res: {
dockerRepository: 'strimzi-helm/strimzi-kafka-operator',
registryHost: 'https://quay.io',
},
},
{
name: 'strimzi-kafka-operator',
url: 'https://docker.io/strimzi-helm/',
res: {
dockerRepository: 'strimzi-helm/strimzi-kafka-operator',
registryHost: 'https://index.docker.io',
},
},
{
name: 'nginx',
url: 'https://docker.io',
res: {
dockerRepository: 'library/nginx',
registryHost: 'https://index.docker.io',
},
},
])('($name, $url)', ({ name, url, res }) => {
expect(getRegistryRepository(name, url)).toStrictEqual(res);
});
});

describe('getAuthHeaders', () => {
beforeEach(() => {
hostRules.find.mockReturnValue({
username: 'some-username',
password: 'some-password',
});
hostRules.hosts.mockReturnValue([]);
});

it('throw page not found exception', async () => {
httpMock
.scope('https://my.local.registry')
Expand Down
28 changes: 17 additions & 11 deletions lib/modules/datasource/docker/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,25 +249,31 @@ export function getRegistryRepository(
};
}
}
let registryHost: string | undefined;
let registryHost = registryUrl;
const split = packageName.split('/');
if (split.length > 1 && (split[0].includes('.') || split[0].includes(':'))) {
[registryHost] = split;
split.shift();
}
let dockerRepository = split.join('/');
if (!registryHost) {
registryHost = registryUrl.replace(
'https://docker.io',
'https://index.docker.io'
);
}
if (registryHost === 'docker.io') {
registryHost = 'index.docker.io';
}
if (!regEx(/^https?:\/\//).exec(registryHost)) {

if (!regEx(/^https?:\/\//).test(registryHost)) {
registryHost = `https://${registryHost}`;
}

const { path, base } =
regEx(/^(?<base>https:\/\/[^/]+)\/(?<path>.+)$/).exec(registryHost)
?.groups ?? {};
if (base && path) {
registryHost = base;
dockerRepository = `${trimTrailingSlash(path)}/${dockerRepository}`;
}

registryHost = registryHost.replace(
'https://docker.io',
'https://index.docker.io'
);

const opts = hostRules.find({
hostType: dockerDatasourceId,
url: registryHost,
Expand Down
25 changes: 25 additions & 0 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,31 @@ describe('modules/datasource/docker/index', () => {
expect(res?.releases).toHaveLength(1);
});

it('uses quay api 2', async () => {
const tags = [{ name: '5.0.12' }];
httpMock
.scope('https://quay.io')
.get(
'/api/v1/repository/bitnami/redis/tag/?limit=100&page=1&onlyActiveTags=true'
)
.reply(200, { tags, has_additional: true })
.get(
'/api/v1/repository/bitnami/redis/tag/?limit=100&page=2&onlyActiveTags=true'
)
.reply(200, { tags: [], has_additional: false })
.get('/v2/')
.reply(200, '', {})
.get('/v2/bitnami/redis/manifests/5.0.12')
.reply(200, '', {});
const config = {
datasource: DockerDatasource.id,
packageName: 'redis',
registryUrls: ['https://quay.io/bitnami'],
};
const res = await getPkgReleases(config);
expect(res?.releases).toHaveLength(1);
});

it('uses quay api and test error', async () => {
httpMock
.scope('https://quay.io')
Expand Down