Skip to content

Commit 27133d8

Browse files
authored
Merge pull request #200 from pmrotule/fix/input-types
fix: allow all possible workflow input types
2 parents 0f1ab23 + fde0df9 commit 27133d8

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ inputs:
2424
description: Workflow to return an ID for. Can be the ID or the workflow filename.
2525
required: true
2626
workflow_inputs:
27-
description: A flat JSON object, only supports strings (as per workflow inputs API).
27+
description: A flat JSON object, only supports strings, numbers, and booleans (as per workflow inputs API).
2828
workflow_timeout_seconds:
2929
description: Time until giving up waiting for the start of the workflow run.
3030
default: 300

src/action.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,22 @@ describe("Action", () => {
9191
expect(() => getConfig()).toThrowError();
9292
});
9393

94-
it("should throw if a workflow inputs JSON is contains non-strings", () => {
94+
it("should handle workflow inputs JSON containing strings numbers or booleans", () => {
9595
mockEnvConfig.workflow_inputs =
96-
'{"cake":"delicious","pie":{"powerLevel":9001}}';
96+
'{"cake":"delicious","pie":9001,"parfait":false}';
9797

98-
expect(() => getConfig()).toThrowError();
98+
expect(() => getConfig()).not.toThrowError();
99+
});
100+
101+
it("should throw if a workflow inputs JSON doesn't contain strings numbers or booleans", () => {
102+
mockEnvConfig.workflow_inputs = '{"pie":{"powerLevel":9001}}';
103+
expect(() => getConfig()).toThrowError('"pie" value is object');
104+
105+
mockEnvConfig.workflow_inputs = '{"vegetable":null}';
106+
expect(() => getConfig()).toThrowError('"vegetable" value is null');
107+
108+
mockEnvConfig.workflow_inputs = '{"fruit":[]}';
109+
expect(() => getConfig()).toThrowError('"fruit" value is Array');
99110
});
100111
});
101112
});

src/action.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface ActionConfig {
4343
}
4444

4545
interface ActionWorkflowInputs {
46-
[input: string]: string;
46+
[input: string]: string | number | boolean;
4747
}
4848

4949
export enum ActionOutputs {
@@ -59,7 +59,7 @@ export function getConfig(): ActionConfig {
5959
workflow: getWorkflowValue(core.getInput("workflow", { required: true })),
6060
workflowInputs: getWorkflowInputs(core.getInput("workflow_inputs")),
6161
workflowTimeoutSeconds:
62-
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ||
62+
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ??
6363
WORKFLOW_TIMEOUT_SECONDS,
6464
};
6565
}
@@ -92,10 +92,22 @@ function getWorkflowInputs(
9292
try {
9393
const parsedJson = JSON.parse(workflowInputs);
9494
for (const key of Object.keys(parsedJson)) {
95-
const type = typeof parsedJson[key];
96-
if (type !== "string") {
95+
const value = parsedJson[key];
96+
const type = (() => {
97+
switch (true) {
98+
case value === null: {
99+
return "null";
100+
}
101+
case Array.isArray(value): {
102+
return "Array";
103+
}
104+
default:
105+
return typeof value;
106+
}
107+
})();
108+
if (!["string", "number", "boolean"].includes(type)) {
97109
throw new Error(
98-
`Expected values to be strings, ${key} value is ${type}`,
110+
`Expected value to be string, number, or boolean. "${key}" value is ${type}`,
99111
);
100112
}
101113
}

0 commit comments

Comments
 (0)