Skip to content

Commit 694b9a6

Browse files
committed
fix: use central parser in imagename
1 parent 077241f commit 694b9a6

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

lib/extractor/image.ts

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseImageReference } from "../image-reference";
12
import { PluginOptions } from "../types";
23

34
export { ImageName, ImageDigest, getImageNames };
@@ -10,50 +11,12 @@ class ImageName {
1011
targetImage: string,
1112
digests: { manifest?: string; index?: string } = {},
1213
) {
13-
// this regex has been copied from
14-
// https://github.com/distribution/distribution/blob/fb2188868d771aa27e5781a32bf78d4c113c18a6/reference/regexp.go#L101
15-
// (code has been modified to print the regex), and then adjusted to
16-
// Javascript. The required modifications were replacing `[:xdigit:]` with
17-
// `[a-fA-F0-9]` and escaping the slashes.
18-
// Note that the digest matched in this Regex will match digests that have
19-
// uppercase-letters, while the regex used in validateDigest does NOT match
20-
// uppercase-letters. This simply matches the behaviour from the upstream
21-
// `reference` and `go-digest `packages.
22-
//
23-
// we're matching pattern: <registry:port_number>(optional)/<image_name>(mandatory):<image_tag>(optional)@<tag_identifier>(optional)
24-
// This Regex contains three capture groups:
25-
// 1) The repository / image name
26-
// 2) tag
27-
// 3) digest
28-
const re =
29-
/^((?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))+)?(?::[0-9]+)?\/)?[a-z0-9]+(?:(?:(?:[._]|__|[-]*)[a-z0-9]+)+)?(?:(?:\/[a-z0-9]+(?:(?:(?:[._]|__|[-]*)[a-z0-9]+)+)?)+)?)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][A-Fa-f0-9]{32,}))?$/gi;
30-
31-
const groups = re.exec(targetImage);
32-
if (groups === null) {
33-
if (targetImage === "") {
34-
throw new Error("image name is empty");
35-
}
36-
if (re.exec(targetImage.toLowerCase()) !== null) {
37-
throw new Error("image repository contains uppercase letter");
38-
}
39-
throw new Error("invalid image reference format");
40-
}
41-
42-
const parsedGroups = {
43-
name: groups[1],
44-
tag: groups[2],
45-
digest: groups[3],
46-
};
47-
48-
this.name = parsedGroups.name;
49-
50-
const NameTotalLengthMax = 255;
51-
if (this.name.length > NameTotalLengthMax) {
52-
throw new Error("image repository name is more than 255 characters");
53-
}
14+
const parsed = parseImageReference(targetImage);
15+
this.name = parsed.registry ? parsed.registry + "/" + parsed.repository : parsed.repository;
5416

55-
this.tag =
56-
parsedGroups.tag || parsedGroups.digest ? parsedGroups.tag : "latest";
17+
// If the image name has a tag, use it. If the image name has
18+
// nether a tag nor a digest, use "latest".
19+
this.tag = parsed.tag || parsed.digest ? parsed.tag : "latest";
5720

5821
this.digests = {};
5922
if (digests.index) {
@@ -63,11 +26,9 @@ class ImageName {
6326
this.digests.manifest = new ImageDigest(digests.manifest);
6427
}
6528

66-
if (parsedGroups.digest) {
67-
const digest = new ImageDigest(parsedGroups.digest);
68-
if (this.digests.manifest !== digest && this.digests.index !== digest) {
69-
this.digests.unknown = digest;
70-
}
29+
// If the image name has a digest, and it's not the same as the manifest or index digest, add it as the unknown digest.
30+
if (parsed.digest && !this.digests.manifest?.equals(parsed.digest) && !this.digests.index?.equals(parsed.digest)) {
31+
this.digests.unknown = new ImageDigest(parsed.digest);
7132
}
7233
}
7334

@@ -131,6 +92,13 @@ class ImageDigest {
13192
public toString(): string {
13293
return this.alg + ":" + this.hex;
13394
}
95+
96+
public equals(other: ImageDigest | string): boolean {
97+
if (typeof other === "string") {
98+
return this.toString() === other;
99+
}
100+
return this.alg === other.alg && this.hex === other.hex;
101+
}
134102
}
135103

136104
function getImageNames(

0 commit comments

Comments
 (0)