From 347401464b4b438f931b00f4cafa88f5e9891263 Mon Sep 17 00:00:00 2001 From: Nikita Dhamapurkar Date: Sun, 8 Nov 2026 08:53:57 +0530 Subject: [PATCH] feat: allow path parameter to accept YAML list format This change modifies getInputAsArray to support both YAML list format (which GitHub Actions converts to JSON arrays) and the existing newline-separated string format. When the input starts with '[', it attempts to parse as JSON array. If successful and the result is an array, it processes those items. Otherwise, it falls back to the existing newline-separated behavior. This enables users to write: ```yaml path: - ~/cache - **/node_modules # inline comments work! ``` Instead of being limited to: ```yaml path: | ~/cache **/node_modules ``` Fixes #510 --- __tests__/actionUtils.test.ts | 32 ++++++++++++++++++++++++++++++++ dist/restore-only/index.js | 18 +++++++++++++++++- dist/restore/index.js | 18 +++++++++++++++++- dist/save-only/index.js | 18 +++++++++++++++++- dist/save/index.js | 18 +++++++++++++++++- src/utils/actionUtils.ts | 20 ++++++++++++++++++-- 6 files changed, 118 insertions(+), 6 deletions(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index 304952f..fae7d71 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -176,6 +176,38 @@ test("getInputAsArray removes spaces after ! at the beginning", () => { ]); }); +test("getInputAsArray handles YAML list format (JSON array)", () => { + testUtils.setInput("foo", '["bar", "baz", "qux"]'); + expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz", "qux"]); +}); + +test("getInputAsArray handles YAML list format with exclusion patterns", () => { + testUtils.setInput( + "foo", + '["~/cache", "! ~/cache/exclude", "**/node_modules"]' + ); + expect(actionUtils.getInputAsArray("foo")).toEqual([ + "~/cache", + "!~/cache/exclude", + "**/node_modules" + ]); +}); + +test("getInputAsArray handles YAML list format with empty strings", () => { + testUtils.setInput("foo", '["bar", "", "baz", " ", "qux"]'); + expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz", "qux"]); +}); + +test("getInputAsArray handles invalid JSON gracefully and falls back to newline parsing", () => { + testUtils.setInput("foo", "[invalid json"); + expect(actionUtils.getInputAsArray("foo")).toEqual(["[invalid json"]); +}); + +test("getInputAsArray handles JSON that is not an array", () => { + testUtils.setInput("foo", '{"key": "value"}'); + expect(actionUtils.getInputAsArray("foo")).toEqual(['{"key": "value"}']); +}); + test("getInputAsInt returns undefined if input not set", () => { expect(actionUtils.getInputAsInt("undefined")).toBeUndefined(); }); diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index bd24321..3728dc8 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -94969,7 +94969,23 @@ function isValidEvent() { return RefKey in process.env && Boolean(process.env[RefKey]); } function getInputAsArray(name, options) { - return getInput(name, options) + const input = getInput(name, options); + // Try to parse as JSON array (YAML list format) + if (input.startsWith("[")) { + try { + const parsed = JSON.parse(input); + if (Array.isArray(parsed)) { + return parsed + .map(s => String(s).replace(/^!\s+/, "!").trim()) + .filter(x => x !== ""); + } + } + catch { + // Not valid JSON, fall through to newline parsing + } + } + // Fall back to newline-separated format + return input .split("\n") .map(s => s.replace(/^!\s+/, "!").trim()) .filter(x => x !== ""); diff --git a/dist/restore/index.js b/dist/restore/index.js index c7cb184..7679898 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -94969,7 +94969,23 @@ function isValidEvent() { return RefKey in process.env && Boolean(process.env[RefKey]); } function getInputAsArray(name, options) { - return getInput(name, options) + const input = getInput(name, options); + // Try to parse as JSON array (YAML list format) + if (input.startsWith("[")) { + try { + const parsed = JSON.parse(input); + if (Array.isArray(parsed)) { + return parsed + .map(s => String(s).replace(/^!\s+/, "!").trim()) + .filter(x => x !== ""); + } + } + catch { + // Not valid JSON, fall through to newline parsing + } + } + // Fall back to newline-separated format + return input .split("\n") .map(s => s.replace(/^!\s+/, "!").trim()) .filter(x => x !== ""); diff --git a/dist/save-only/index.js b/dist/save-only/index.js index 1b81de3..7a8d980 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -94974,7 +94974,23 @@ function isValidEvent() { return RefKey in process.env && Boolean(process.env[RefKey]); } function getInputAsArray(name, options) { - return getInput(name, options) + const input = getInput(name, options); + // Try to parse as JSON array (YAML list format) + if (input.startsWith("[")) { + try { + const parsed = JSON.parse(input); + if (Array.isArray(parsed)) { + return parsed + .map(s => String(s).replace(/^!\s+/, "!").trim()) + .filter(x => x !== ""); + } + } + catch { + // Not valid JSON, fall through to newline parsing + } + } + // Fall back to newline-separated format + return input .split("\n") .map(s => s.replace(/^!\s+/, "!").trim()) .filter(x => x !== ""); diff --git a/dist/save/index.js b/dist/save/index.js index 59f6659..d3f249d 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -94974,7 +94974,23 @@ function isValidEvent() { return RefKey in process.env && Boolean(process.env[RefKey]); } function getInputAsArray(name, options) { - return getInput(name, options) + const input = getInput(name, options); + // Try to parse as JSON array (YAML list format) + if (input.startsWith("[")) { + try { + const parsed = JSON.parse(input); + if (Array.isArray(parsed)) { + return parsed + .map(s => String(s).replace(/^!\s+/, "!").trim()) + .filter(x => x !== ""); + } + } + catch { + // Not valid JSON, fall through to newline parsing + } + } + // Fall back to newline-separated format + return input .split("\n") .map(s => s.replace(/^!\s+/, "!").trim()) .filter(x => x !== ""); diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 260d4fd..3f4a69f 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -40,8 +40,24 @@ export function getInputAsArray( name: string, options?: core.InputOptions ): string[] { - return core - .getInput(name, options) + const input = core.getInput(name, options); + + // Try to parse as JSON array (YAML list format) + if (input.startsWith("[")) { + try { + const parsed = JSON.parse(input); + if (Array.isArray(parsed)) { + return parsed + .map(s => String(s).replace(/^!\s+/, "!").trim()) + .filter(x => x !== ""); + } + } catch { + // Not valid JSON, fall through to newline parsing + } + } + + // Fall back to newline-separated format + return input .split("\n") .map(s => s.replace(/^!\s+/, "!").trim()) .filter(x => x !== "");