snyk-docker-plugin
Version:
Snyk CLI docker plugin
35 lines (28 loc) • 852 B
text/typescript
import { BaseRuntime } from "../../facts";
const VALID_VERSION_PATTERN =
/^(?!.*\.\.)[0-9]+(?:[._+a-zA-Z0-9-]*[a-zA-Z0-9])?$/;
const regex = /^\s*JAVA_VERSION\s*=\s*(?:(["'])(.*?)\1|([^#\r\n]+))/gm;
function isValidJavaVersion(version: string): boolean {
if (!version || version.length === 0) {
return false;
}
return VALID_VERSION_PATTERN.test(version);
}
export function parseJavaRuntimeRelease(content: string): BaseRuntime | null {
if (!content || content.trim().length === 0) {
return null;
}
try {
const matches = [...content.matchAll(regex)];
if (matches.length !== 1) {
return null;
}
const version = (matches[0][2] || matches[0][3] || "").trim();
if (!isValidJavaVersion(version)) {
return null;
}
return { type: "java", version };
} catch (error) {
return null;
}
}