mirror of
https://github.com/actions/setup-node.git
synced 2026-08-22 08:32:54 +00:00
Avoid action metadata parser backtracking
This commit is contained in:
parent
219d4c3cc1
commit
d0cafc58c4
@ -198,6 +198,7 @@ describe('main tests', () => {
|
|||||||
${'{"devEngines": {"runtime": {"name": "node", "version": "22.0.0"}}}'} | ${'22.0.0'}
|
${'{"devEngines": {"runtime": {"name": "node", "version": "22.0.0"}}}'} | ${'22.0.0'}
|
||||||
${'{"devEngines": {"runtime": [{"name": "bun"}, {"name": "node", "version": "22.0.0"}]}}'} | ${'22.0.0'}
|
${'{"devEngines": {"runtime": [{"name": "bun"}, {"name": "node", "version": "22.0.0"}]}}'} | ${'22.0.0'}
|
||||||
${"name: 'My Action'\nruns:\n using: 'node24'\n main: 'dist/index.js'\n"} | ${'24'}
|
${"name: 'My Action'\nruns:\n using: 'node24'\n main: 'dist/index.js'\n"} | ${'24'}
|
||||||
|
${'runs:\n using: node24 # action runtime\n'} | ${'24'}
|
||||||
`.it('parses "$contents"', ({contents, expected}: any) => {
|
`.it('parses "$contents"', ({contents, expected}: any) => {
|
||||||
const existsSpy = jest.spyOn(fs, 'existsSync');
|
const existsSpy = jest.spyOn(fs, 'existsSync');
|
||||||
existsSpy.mockImplementation(() => true);
|
existsSpy.mockImplementation(() => true);
|
||||||
|
|||||||
30
dist/cache-save/index.js
vendored
30
dist/cache-save/index.js
vendored
@ -92709,9 +92709,33 @@ function getNodeVersionFromFile(versionFilePath) {
|
|||||||
catch {
|
catch {
|
||||||
core.info('Node version file is not JSON file');
|
core.info('Node version file is not JSON file');
|
||||||
}
|
}
|
||||||
const actionRuntime = contents.match(/^runs\s*:\s*$(?:\r?\n(?:[ \t]+[^\r\n]*|[ \t]*))*?\r?\n[ \t]+using:\s*['"]?node(?<version>\d+)['"]?\s*(?:#.*)?$/m);
|
let inRunsSection = false;
|
||||||
if (actionRuntime?.groups?.version) {
|
for (const line of contents.split(/\r?\n/)) {
|
||||||
return actionRuntime.groups.version;
|
const trimmedLine = line.trim();
|
||||||
|
if (!inRunsSection) {
|
||||||
|
inRunsSection =
|
||||||
|
trimmedLine === 'runs:' || trimmedLine.startsWith('runs: #');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!trimmedLine || trimmedLine.startsWith('#')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!line.match(/^\s/)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const separatorIndex = trimmedLine.indexOf(':');
|
||||||
|
if (trimmedLine.slice(0, separatorIndex) !== 'using') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let runtime = trimmedLine.slice(separatorIndex + 1).trim();
|
||||||
|
runtime = runtime.split('#', 1)[0].trim();
|
||||||
|
if ((runtime.startsWith("'") && runtime.endsWith("'")) ||
|
||||||
|
(runtime.startsWith('"') && runtime.endsWith('"'))) {
|
||||||
|
runtime = runtime.slice(1, -1);
|
||||||
|
}
|
||||||
|
if (runtime.startsWith('node') && /^\d+$/.test(runtime.slice(4))) {
|
||||||
|
return runtime.slice(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
||||||
return found?.groups?.version ?? contents.trim();
|
return found?.groups?.version ?? contents.trim();
|
||||||
|
|||||||
30
dist/setup/index.js
vendored
30
dist/setup/index.js
vendored
@ -98103,9 +98103,33 @@ function getNodeVersionFromFile(versionFilePath) {
|
|||||||
catch {
|
catch {
|
||||||
core_info('Node version file is not JSON file');
|
core_info('Node version file is not JSON file');
|
||||||
}
|
}
|
||||||
const actionRuntime = contents.match(/^runs\s*:\s*$(?:\r?\n(?:[ \t]+[^\r\n]*|[ \t]*))*?\r?\n[ \t]+using:\s*['"]?node(?<version>\d+)['"]?\s*(?:#.*)?$/m);
|
let inRunsSection = false;
|
||||||
if (actionRuntime?.groups?.version) {
|
for (const line of contents.split(/\r?\n/)) {
|
||||||
return actionRuntime.groups.version;
|
const trimmedLine = line.trim();
|
||||||
|
if (!inRunsSection) {
|
||||||
|
inRunsSection =
|
||||||
|
trimmedLine === 'runs:' || trimmedLine.startsWith('runs: #');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!trimmedLine || trimmedLine.startsWith('#')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!line.match(/^\s/)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const separatorIndex = trimmedLine.indexOf(':');
|
||||||
|
if (trimmedLine.slice(0, separatorIndex) !== 'using') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let runtime = trimmedLine.slice(separatorIndex + 1).trim();
|
||||||
|
runtime = runtime.split('#', 1)[0].trim();
|
||||||
|
if ((runtime.startsWith("'") && runtime.endsWith("'")) ||
|
||||||
|
(runtime.startsWith('"') && runtime.endsWith('"'))) {
|
||||||
|
runtime = runtime.slice(1, -1);
|
||||||
|
}
|
||||||
|
if (runtime.startsWith('node') && /^\d+$/.test(runtime.slice(4))) {
|
||||||
|
return runtime.slice(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
||||||
return found?.groups?.version ?? contents.trim();
|
return found?.groups?.version ?? contents.trim();
|
||||||
|
|||||||
37
src/util.ts
37
src/util.ts
@ -68,11 +68,38 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null {
|
|||||||
core.info('Node version file is not JSON file');
|
core.info('Node version file is not JSON file');
|
||||||
}
|
}
|
||||||
|
|
||||||
const actionRuntime = contents.match(
|
let inRunsSection = false;
|
||||||
/^runs\s*:\s*$(?:\r?\n(?:[ \t]+[^\r\n]*|[ \t]*))*?\r?\n[ \t]+using:\s*['"]?node(?<version>\d+)['"]?\s*(?:#.*)?$/m
|
for (const line of contents.split(/\r?\n/)) {
|
||||||
);
|
const trimmedLine = line.trim();
|
||||||
if (actionRuntime?.groups?.version) {
|
if (!inRunsSection) {
|
||||||
return actionRuntime.groups.version;
|
inRunsSection =
|
||||||
|
trimmedLine === 'runs:' || trimmedLine.startsWith('runs: #');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!trimmedLine || trimmedLine.startsWith('#')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!line.match(/^\s/)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const separatorIndex = trimmedLine.indexOf(':');
|
||||||
|
if (trimmedLine.slice(0, separatorIndex) !== 'using') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let runtime = trimmedLine.slice(separatorIndex + 1).trim();
|
||||||
|
runtime = runtime.split('#', 1)[0].trim();
|
||||||
|
if (
|
||||||
|
(runtime.startsWith("'") && runtime.endsWith("'")) ||
|
||||||
|
(runtime.startsWith('"') && runtime.endsWith('"'))
|
||||||
|
) {
|
||||||
|
runtime = runtime.slice(1, -1);
|
||||||
|
}
|
||||||
|
if (runtime.startsWith('node') && /^\d+$/.test(runtime.slice(4))) {
|
||||||
|
return runtime.slice(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user