Skip to content

Commit

Permalink
refactor: convert enums to unions (#18900)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Nov 20, 2022
1 parent 987421b commit 8e4b523
Show file tree
Hide file tree
Showing 49 changed files with 501 additions and 551 deletions.
73 changes: 46 additions & 27 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '../../../constants/error-messages';
import * as _hostRules from '../../../util/host-rules';
import { Http } from '../../../util/http';
import { MediaType } from './types';
import { DockerDatasource, getAuthHeaders, getRegistryRepository } from '.';

const hostRules = mocked(_hostRules);
Expand Down Expand Up @@ -616,11 +615,14 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.manifestV2 })
.reply(200, '', {
'content-type':
'application/vnd.docker.distribution.manifest.v2+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/library/some-dep/blobs/some-config-digest')
Expand All @@ -637,7 +639,8 @@ describe('modules/datasource/docker/index', () => {
.get('/library/some-dep/manifests/some-new-value')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestListV2,
mediaType:
'application/vnd.docker.distribution.manifest.list.v2+json',
manifests: [
{
digest:
Expand Down Expand Up @@ -704,11 +707,14 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.manifestV2 })
.reply(200, '', {
'content-type':
'application/vnd.docker.distribution.manifest.v2+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/library/some-dep/blobs/some-config-digest')
Expand All @@ -726,7 +732,8 @@ describe('modules/datasource/docker/index', () => {
.get('/library/some-dep/manifests/some-new-value')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestListV2,
mediaType:
'application/vnd.docker.distribution.manifest.list.v2+json',
manifests: [
{
digest:
Expand Down Expand Up @@ -792,11 +799,13 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.ociManifestV1 })
.reply(200, '', {
'content-type': 'application/vnd.oci.image.manifest.v1+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.ociManifestV1,
mediaType: 'application/vnd.oci.image.manifest.v1+json',
config: { digest: 'some-config-digest' },
})
.get('/library/some-dep/blobs/some-config-digest')
Expand All @@ -815,7 +824,7 @@ describe('modules/datasource/docker/index', () => {
200,
{
schemaVersion: 2,
mediaType: MediaType.ociManifestIndexV1,
mediaType: 'application/vnd.oci.image.index.v1+json',
manifests: [
{
digest: 'some-new-image-digest',
Expand Down Expand Up @@ -864,7 +873,9 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.ociManifestV1 })
.reply(200, '', {
'content-type': 'application/vnd.oci.image.manifest.v1+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(200, {
schemaVersion: 2,
Expand Down Expand Up @@ -929,7 +940,10 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.manifestV2 })
.reply(200, '', {
'content-type':
'application/vnd.docker.distribution.manifest.v2+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(404, {});
httpMock
Expand All @@ -945,7 +959,8 @@ describe('modules/datasource/docker/index', () => {
.get('/library/some-dep/manifests/some-new-value')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestListV2,
mediaType:
'application/vnd.docker.distribution.manifest.list.v2+json',
manifests: [
{
digest:
Expand Down Expand Up @@ -1008,7 +1023,9 @@ describe('modules/datasource/docker/index', () => {
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/some-dep:pull"',
})
.head('/library/some-dep/manifests/' + currentDigest)
.reply(200, '', { 'content-type': MediaType.ociManifestV1 })
.reply(200, '', {
'content-type': 'application/vnd.oci.image.manifest.v1+json',
})
.get('/library/some-dep/manifests/' + currentDigest)
.reply(200, {
schemaVersion: 2,
Expand Down Expand Up @@ -1298,7 +1315,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/some')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/')
Expand Down Expand Up @@ -1698,7 +1715,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/2-alpine')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/node/blobs/some-config-digest')
Expand Down Expand Up @@ -1758,7 +1775,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/2-alpine')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/node/blobs/some-config-digest')
Expand Down Expand Up @@ -1798,13 +1815,14 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/abc')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestListV2,
mediaType:
'application/vnd.docker.distribution.manifest.list.v2+json',
manifests: [{ digest: 'some-image-digest' }],
})
.get('/node/manifests/some-image-digest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/node/blobs/some-config-digest')
Expand Down Expand Up @@ -1839,7 +1857,8 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/latest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestListV2,
mediaType:
'application/vnd.docker.distribution.manifest.list.v2+json',
manifests: [],
});
const res = await getPkgReleases({
Expand All @@ -1864,7 +1883,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/latest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV1,
mediaType: 'application/vnd.docker.distribution.manifest.v1+json',
});
const res = await getPkgReleases({
datasource: DockerDatasource.id,
Expand Down Expand Up @@ -1910,13 +1929,13 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/1')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.ociManifestIndexV1,
mediaType: 'application/vnd.oci.image.index.v1+json',
manifests: [{ digest: 'some-image-digest' }],
})
.get('/node/manifests/some-image-digest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.ociManifestV1,
mediaType: 'application/vnd.oci.image.manifest.v1+json',
config: { digest: 'some-config-digest' },
})
.get('/node/blobs/some-config-digest')
Expand Down Expand Up @@ -1956,7 +1975,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/1')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.ociManifestIndexV1,
mediaType: 'application/vnd.oci.image.index.v1+json',
manifests: [{ digest: 'some-image-digest' }],
})
.get('/node/manifests/some-image-digest')
Expand Down Expand Up @@ -2000,7 +2019,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/latest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.ociManifestIndexV1,
mediaType: 'application/vnd.oci.image.index.v1+json',
manifests: [],
});
const res = await getPkgReleases({
Expand Down Expand Up @@ -2038,7 +2057,7 @@ describe('modules/datasource/docker/index', () => {
.get('/node/manifests/latest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/node/blobs/some-config-digest')
Expand Down Expand Up @@ -2096,7 +2115,7 @@ describe('modules/datasource/docker/index', () => {
.get('/visualon/drone-git/manifests/latest')
.reply(200, {
schemaVersion: 2,
mediaType: MediaType.manifestV2,
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
config: { digest: 'some-config-digest' },
})
.get('/visualon/drone-git/blobs/some-config-digest')
Expand Down
35 changes: 21 additions & 14 deletions lib/modules/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ import {
import { Datasource } from '../datasource';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
import { gitRefLabel, isArtifactoryServer, sourceLabels } from './common';
import {
import type {
Image,
ImageConfig,
ImageList,
MediaType,
OciImage,
OciImageList,
RegistryRepository,
Expand Down Expand Up @@ -407,10 +406,10 @@ export class DockerDatasource extends Datasource {
return null;
}
headers.accept = [
MediaType.manifestListV2,
MediaType.manifestV2,
MediaType.ociManifestV1,
MediaType.ociManifestIndexV1,
'application/vnd.docker.distribution.manifest.list.v2+json',
'application/vnd.docker.distribution.manifest.v2+json',
'application/vnd.oci.image.manifest.v1+json',
'application/vnd.oci.image.index.v1+json',
].join(', ');
const url = `${registryHost}/v2/${dockerRepository}/manifests/${tag}`;
const manifestResponse = await this.http[mode](url, {
Expand Down Expand Up @@ -540,7 +539,10 @@ export class DockerDatasource extends Datasource {
return null;
}

if (manifest.mediaType === MediaType.manifestListV2) {
if (
manifest.mediaType ===
'application/vnd.docker.distribution.manifest.list.v2+json'
) {
if (manifest.manifests.length) {
logger.trace(
{ registry, dockerRepository, tag },
Expand All @@ -561,15 +563,16 @@ export class DockerDatasource extends Datasource {
}

if (
manifest.mediaType === MediaType.manifestV2 &&
manifest.mediaType ===
'application/vnd.docker.distribution.manifest.v2+json' &&
is.string(manifest.config?.digest)
) {
return manifest.config?.digest;
}

// OCI image lists are not required to specify a mediaType
if (
manifest.mediaType === MediaType.ociManifestIndexV1 ||
manifest.mediaType === 'application/vnd.oci.image.index.v1+json' ||
(!manifest.mediaType && 'manifests' in manifest)
) {
if (manifest.manifests.length) {
Expand All @@ -593,7 +596,7 @@ export class DockerDatasource extends Datasource {

// OCI manifests are not required to specify a mediaType
if (
(manifest.mediaType === MediaType.ociManifestV1 ||
(manifest.mediaType === 'application/vnd.oci.image.manifest.v1+json' ||
(!manifest.mediaType && 'config' in manifest)) &&
is.string(manifest.config?.digest)
) {
Expand Down Expand Up @@ -627,8 +630,10 @@ export class DockerDatasource extends Datasource {
);

if (
manifestResponse?.headers['content-type'] !== MediaType.manifestV2 &&
manifestResponse?.headers['content-type'] !== MediaType.ociManifestV1
manifestResponse?.headers['content-type'] !==
'application/vnd.docker.distribution.manifest.v2+json' &&
manifestResponse?.headers['content-type'] !==
'application/vnd.oci.image.manifest.v1+json'
) {
return null;
}
Expand Down Expand Up @@ -1058,8 +1063,10 @@ export class DockerDatasource extends Datasource {
| OciImage;
if (
manifestList.schemaVersion === 2 &&
(manifestList.mediaType === MediaType.manifestListV2 ||
manifestList.mediaType === MediaType.ociManifestIndexV1 ||
(manifestList.mediaType ===
'application/vnd.docker.distribution.manifest.list.v2+json' ||
manifestList.mediaType ===
'application/vnd.oci.image.index.v1+json' ||
(!manifestList.mediaType && 'manifests' in manifestList))
) {
for (const manifest of manifestList.manifests) {
Expand Down
Loading

0 comments on commit 8e4b523

Please sign in to comment.