Skip to content

Commit

Permalink
code review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joshspicer authored Apr 7, 2023
1 parent db00742 commit 4e97da9
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/spec-configuration/containerCollectionsOCI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ export function getRef(output: Log, input: string): OCIRef | undefined {
const splitOnColon = digestWithHashingAlgorithm.split(':');
if (splitOnColon.length !== 2) {
output.write(`Failed to parse digest '${digestWithHashingAlgorithm}'. Expected format: 'sha256:abcdefghijk'`, LogLevel.Error);
return undefined;
return;
}

if (splitOnColon[0] !== 'sha256') {
output.write(`Digest algorithm for input '${input}' failed validation. Expected hashing algorithm to be 'sha256'.`, LogLevel.Error);
return undefined;
return;
}

if (!regexForVersionOrDigest.test(splitOnColon[1])) {
Expand All @@ -160,7 +160,7 @@ export function getRef(output: Log, input: string): OCIRef | undefined {
digest = digestWithHashingAlgorithm;
} else {
// In both cases, assume 'latest' tag.
if (indexOfLastColon === -1 || indexOfLastColon < input.indexOf('/')) {
if (indexOfLastColon === -1 || indexOfLastColon < input.lastIndexOf('/')) {
// 1. The final colon is before the first slash (a port)
// eg: ghcr.io:8081/codspace/features/ruby
// 2. There is no tag at all
Expand All @@ -176,6 +176,11 @@ export function getRef(output: Log, input: string): OCIRef | undefined {
}
}

if (tag && !regexForVersionOrDigest.test(tag)) {
output.write(`Tag '${tag}' for input '${input}' failed validation. Expected digest to match regex '${regexForVersionOrDigest}'.`, LogLevel.Error);
return;
}

const splitOnSlash = resource.split('/');

const id = splitOnSlash[splitOnSlash.length - 1]; // Aka 'featureName' - Eg: 'ruby'
Expand All @@ -185,6 +190,11 @@ export function getRef(output: Log, input: string): OCIRef | undefined {

const path = `${namespace}/${id}`;

if (!regexForPath.exec(path)) {
output.write(`Path '${path}' for input '${input}' failed validation. Expected path to match regex '${regexForPath}'.`, LogLevel.Error);
return;
}

const version = digest || tag || 'latest'; // The most specific version.

output.write(`> input: ${input}`, LogLevel.Trace);
Expand All @@ -200,23 +210,6 @@ export function getRef(output: Log, input: string): OCIRef | undefined {
output.write(`> tag?: ${tag}`, LogLevel.Trace);
output.write(`> digest?: ${digest}`, LogLevel.Trace);

// -- Validate results of parse.

if (!regexForPath.exec(path)) {
output.write(`Path '${path}' for input '${input}' failed validation. Expected path to match regex '${regexForPath}'.`, LogLevel.Error);
return undefined;
}

if (digest && tag) {
output.write(`Parsed both a digest ('${digest}') and a tag ('${tag}') for input '${input}'. Resource can only have one.`, LogLevel.Error);
return undefined;
}

if (tag && !regexForVersionOrDigest.test(tag)) {
output.write(`Tag '${tag}' for input '${input}' failed validation. Expected digest to match regex '${regexForVersionOrDigest}'.`, LogLevel.Error);
return undefined;
}

return {
id,
owner,
Expand Down

0 comments on commit 4e97da9

Please sign in to comment.