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";
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
export function convertEventWorkflowDispatchInputs(
|
|
|
|
|
context: TemplateContext,
|
|
|
|
|
token: MappingToken
|
|
|
|
|
): WorkflowDispatchConfig {
|
2023-01-09 19:02:19 -05:00
|
|
|
const result: WorkflowDispatchConfig = {};
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
for (const item of token) {
|
2023-01-09 19:02:19 -05:00
|
|
|
const key = item.key.assertString("workflow dispatch input key");
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
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-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
return result;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function convertWorkflowDispatchInputs(
|
|
|
|
|
context: TemplateContext,
|
|
|
|
|
token: MappingToken
|
|
|
|
|
): {
|
2023-01-09 19:02:19 -05:00
|
|
|
[inputName: string]: InputConfig;
|
2023-01-06 15:54:31 -08:00
|
|
|
} {
|
2023-01-09 19:02:19 -05:00
|
|
|
const result: {[inputName: string]: InputConfig} = {};
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
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-06 15:54:31 -08:00
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
result[inputName.value] = convertWorkflowDispatchInput(context, inputMapping);
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
return result;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
export function convertWorkflowDispatchInput(context: TemplateContext, token: MappingToken): InputConfig {
|
2023-01-06 15:54:31 -08:00
|
|
|
const result: InputConfig = {
|
2023-01-09 19:02:19 -05:00
|
|
|
type: InputType.string // Default to string
|
|
|
|
|
};
|
2023-01-06 15:54:31 -08:00
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
let defaultValue: undefined | ScalarToken;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
for (const item of token) {
|
2023-01-09 19:02:19 -05:00
|
|
|
const key = item.key.assertString("workflow dispatch input key");
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
switch (key.value) {
|
|
|
|
|
case "description":
|
2023-01-09 19:02:19 -05:00
|
|
|
result.description = item.value.assertString("input description").value;
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
case "required":
|
2023-01-09 19:02:19 -05:00
|
|
|
result.required = item.value.assertBoolean("input required").value;
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
case "default":
|
2023-01-09 19:02:19 -05:00
|
|
|
defaultValue = item.value.assertScalar("input default");
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
case "type":
|
2023-01-09 19:02:19 -05:00
|
|
|
result.type = InputType[item.value.assertString("input type").value as keyof typeof InputType];
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
case "options":
|
2023-01-09 19:02:19 -05:00
|
|
|
result.options = convertStringList("input options", item.value.assertSequence("input options"));
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
default:
|
2023-01-09 19:02:19 -05:00
|
|
|
context.error(item.key, `Invalid key '${key.value}'`);
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-06 15:54:31 -08:00
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
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;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2023-01-09 19:02:19 -05:00
|
|
|
context.error(defaultValue, e);
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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");
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
} 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-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
return result;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|