Files
languageservices/workflow-parser/src/model/converter/workflow-dispatch.ts
T

113 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-01-09 19:02:19 -05:00
import {TemplateContext} from "../../templates/template-context";
import {MappingToken} from "../../templates/tokens/mapping-token";
import {ScalarToken} from "../../templates/tokens/scalar-token";
import {InputConfig, InputType, WorkflowDispatchConfig} from "../workflow-template";
import {convertStringList} from "./string-list";
export function convertEventWorkflowDispatchInputs(
context: TemplateContext,
token: MappingToken
): WorkflowDispatchConfig {
2023-01-09 19:02:19 -05:00
const result: WorkflowDispatchConfig = {};
for (const item of token) {
2023-01-09 19:02:19 -05:00
const key = item.key.assertString("workflow dispatch input key");
switch (key.value) {
case "inputs":
2023-01-09 19:02:19 -05:00
result.inputs = convertWorkflowDispatchInputs(context, item.value.assertMapping("workflow dispatch inputs"));
break;
}
}
2023-01-09 19:02:19 -05:00
return result;
}
export function convertWorkflowDispatchInputs(
context: TemplateContext,
token: MappingToken
): {
2023-01-09 19:02:19 -05:00
[inputName: string]: InputConfig;
} {
2023-01-09 19:02:19 -05:00
const result: {[inputName: string]: InputConfig} = {};
for (const item of token) {
2023-01-09 19:02:19 -05:00
const inputName = item.key.assertString("input name");
const inputMapping = item.value.assertMapping("input configuration");
2023-01-09 19:02:19 -05:00
result[inputName.value] = convertWorkflowDispatchInput(context, inputMapping);
}
2023-01-09 19:02:19 -05:00
return result;
}
2023-01-09 19:02:19 -05:00
export function convertWorkflowDispatchInput(context: TemplateContext, token: MappingToken): InputConfig {
const result: InputConfig = {
2023-01-09 19:02:19 -05:00
type: InputType.string // Default to string
};
2023-01-09 19:02:19 -05:00
let defaultValue: undefined | ScalarToken;
for (const item of token) {
2023-01-09 19:02:19 -05:00
const key = item.key.assertString("workflow dispatch input key");
switch (key.value) {
case "description":
2023-01-09 19:02:19 -05:00
result.description = item.value.assertString("input description").value;
break;
case "required":
2023-01-09 19:02:19 -05:00
result.required = item.value.assertBoolean("input required").value;
break;
case "default":
2023-01-09 19:02:19 -05:00
defaultValue = item.value.assertScalar("input default");
break;
case "type":
2023-01-09 19:02:19 -05:00
result.type = InputType[item.value.assertString("input type").value as keyof typeof InputType];
break;
case "options":
2023-01-09 19:02:19 -05:00
result.options = convertStringList("input options", item.value.assertSequence("input options"));
break;
default:
2023-01-09 19:02:19 -05:00
context.error(item.key, `Invalid key '${key.value}'`);
}
}
// Validate default value
if (defaultValue !== undefined) {
try {
switch (result.type) {
case InputType.boolean:
2023-01-09 19:02:19 -05:00
result.default = defaultValue.assertBoolean("input default").value;
2023-01-09 19:02:19 -05:00
break;
case InputType.string:
case InputType.choice:
case InputType.environment:
2023-01-09 19:02:19 -05:00
result.default = defaultValue.assertString("input default").value;
break;
}
} catch (e) {
2023-01-09 19:02:19 -05:00
context.error(defaultValue, e);
}
}
// Validate `options` for `choice` type
if (result.type === InputType.choice) {
if (result.options === undefined || result.options.length === 0) {
2023-01-09 19:02:19 -05:00
context.error(token, "Missing 'options' for choice input");
}
} else {
if (result.options !== undefined) {
2023-01-09 19:02:19 -05:00
context.error(token, "Input type is not 'choice', but 'options' is defined");
}
}
2023-01-09 19:02:19 -05:00
return result;
}