mirror of
https://github.com/actions/cache.git
synced 2026-11-20 09:02:54 +00:00
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
This commit is contained in:
parent
3edfce9056
commit
347401464b
@ -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();
|
||||
});
|
||||
|
||||
18
dist/restore-only/index.js
vendored
18
dist/restore-only/index.js
vendored
@ -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 !== "");
|
||||
|
||||
18
dist/restore/index.js
vendored
18
dist/restore/index.js
vendored
@ -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 !== "");
|
||||
|
||||
18
dist/save-only/index.js
vendored
18
dist/save-only/index.js
vendored
@ -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 !== "");
|
||||
|
||||
18
dist/save/index.js
vendored
18
dist/save/index.js
vendored
@ -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 !== "");
|
||||
|
||||
@ -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 !== "");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user