Files
languageservices/languageservice/src/context-providers/inputs.ts
T

61 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-02-24 08:53:51 -08:00
import {data, DescriptionDictionary} from "@actions/expressions";
import {InputConfig} from "@actions/workflow-parser/model/workflow-template";
2022-12-02 11:28:03 -08:00
import {WorkflowContext} from "../context/workflow-context";
2023-01-10 17:44:48 -08:00
export function getInputsContext(workflowContext: WorkflowContext): DescriptionDictionary {
const d = new DescriptionDictionary();
2022-12-13 15:25:20 -05:00
const events = workflowContext?.template?.events;
if (!events) {
2022-12-02 11:28:03 -08:00
return d;
}
2022-12-13 15:25:20 -05:00
const dispatch = events["workflow_dispatch"];
if (dispatch?.inputs) {
addInputs(d, dispatch.inputs);
2022-12-02 11:28:03 -08:00
}
2022-12-13 15:25:20 -05:00
const call = events["workflow_call"];
if (call?.inputs) {
addInputs(d, call.inputs);
2022-12-05 15:23:28 -05:00
}
2022-12-13 15:25:20 -05:00
return d;
}
2023-01-10 17:44:48 -08:00
function addInputs(d: DescriptionDictionary, inputs: {[inputName: string]: InputConfig}) {
2022-12-02 11:28:03 -08:00
for (const inputName of Object.keys(inputs)) {
const input = inputs[inputName];
switch (input.type) {
case "choice":
if (input.default) {
2023-01-10 17:44:48 -08:00
d.add(inputName, new data.StringData(input.default as string), input.description);
2022-12-02 11:28:03 -08:00
} else {
// Default to the first input or an empty string
2023-01-10 17:44:48 -08:00
d.add(inputName, new data.StringData((input.options || [""])[0]), input.description);
2022-12-02 11:28:03 -08:00
}
break;
case "environment":
if (input.default) {
2023-01-10 17:44:48 -08:00
d.add(inputName, new data.StringData(input.default as string), input.description);
2022-12-02 11:28:03 -08:00
} else {
// For now default to an empty value if there is no default value. This will always be an environment, so
// we could also dynamically look up environments and default to the first one, but leaving this as a
// future enhancement for now.
d.add(inputName, new data.StringData(""));
}
break;
case "boolean":
2023-01-10 17:44:48 -08:00
d.add(inputName, new data.BooleanData((input.default as boolean) || false), input.description);
2022-12-02 11:28:03 -08:00
break;
case "string":
default:
2023-01-10 17:44:48 -08:00
d.add(inputName, new data.StringData((input.default as string) || inputName), input.description);
2022-12-02 11:28:03 -08:00
break;
}
}
}