11import { TemplateContext } from "../../templates/template-context" ;
22import { MappingToken , TemplateToken } from "../../templates/tokens" ;
33import { isMapping } from "../../templates/tokens/type-guards" ;
4- import { SecretConfig , WorkflowCallConfig } from "../workflow-template" ;
4+ import { SecretConfig , WorkflowCallConfig , InputConfig , InputType } from "../workflow-template" ;
5+ import { convertStringList } from "./string-list" ;
6+ import { ScalarToken } from "../../templates/tokens/scalar-token" ;
57
68export function convertEventWorkflowCall ( context : TemplateContext , token : MappingToken ) : WorkflowCallConfig {
79 const result : WorkflowCallConfig = { } ;
@@ -11,7 +13,7 @@ export function convertEventWorkflowCall(context: TemplateContext, token: Mappin
1113
1214 switch ( key . value ) {
1315 case "inputs" :
14- // Ignore, these are handled by convertEventWorkflowDispatchInputs
16+ result . inputs = convertWorkflowInputs ( context , item . value . assertMapping ( "workflow dispatch inputs" ) ) ;
1517 break ;
1618
1719 case "secrets" :
@@ -27,6 +29,94 @@ export function convertEventWorkflowCall(context: TemplateContext, token: Mappin
2729 return result ;
2830}
2931
32+ export function convertWorkflowInputs (
33+ context : TemplateContext ,
34+ token : MappingToken
35+ ) : {
36+ [ inputName : string ] : InputConfig ;
37+ } {
38+ const result : { [ inputName : string ] : InputConfig } = { } ;
39+
40+ for ( const item of token ) {
41+ const inputName = item . key . assertString ( "input name" ) ;
42+ const inputMapping = item . value . assertMapping ( "input configuration" ) ;
43+
44+ result [ inputName . value ] = convertWorkflowInput ( context , inputMapping ) ;
45+ }
46+
47+ return result ;
48+ }
49+
50+ export function convertWorkflowInput ( context : TemplateContext , token : MappingToken ) : InputConfig {
51+ const result : InputConfig = {
52+ type : InputType . string // Default to string
53+ } ;
54+
55+ let defaultValue : undefined | ScalarToken ;
56+
57+ for ( const item of token ) {
58+ const key = item . key . assertString ( "workflow dispatch input key" ) ;
59+
60+ switch ( key . value ) {
61+ case "description" :
62+ result . description = item . value . assertString ( "input description" ) . value ;
63+ break ;
64+
65+ case "required" :
66+ result . required = item . value . assertBoolean ( "input required" ) . value ;
67+ break ;
68+
69+ case "default" :
70+ defaultValue = item . value . assertScalar ( "input default" ) ;
71+ break ;
72+
73+ case "type" :
74+ result . type = InputType [ item . value . assertString ( "input type" ) . value as keyof typeof InputType ] ;
75+ break ;
76+
77+ case "options" :
78+ result . options = convertStringList ( "input options" , item . value . assertSequence ( "input options" ) ) ;
79+ break ;
80+
81+ default :
82+ context . error ( item . key , `Invalid key '${ key . value } '` ) ;
83+ }
84+ }
85+
86+ // Validate default value
87+ if ( defaultValue !== undefined && ! defaultValue . isExpression ) {
88+ try {
89+ switch ( result . type ) {
90+ case InputType . boolean :
91+ result . default = defaultValue . assertBoolean ( "input default" ) . value ;
92+
93+ break ;
94+
95+ case InputType . string :
96+ case InputType . choice :
97+ case InputType . environment :
98+ result . default = defaultValue . assertString ( "input default" ) . value ;
99+ break ;
100+ }
101+ } catch ( e ) {
102+ context . error ( defaultValue , e ) ;
103+ }
104+ }
105+
106+ // Validate `options` for `choice` type
107+ if ( result . type === InputType . choice ) {
108+ if ( result . options === undefined || result . options . length === 0 ) {
109+ context . error ( token , "Missing 'options' for choice input" ) ;
110+ }
111+ } else {
112+ if ( result . options !== undefined ) {
113+ context . error ( token , "Input type is not 'choice', but 'options' is defined" ) ;
114+ }
115+ }
116+
117+ return result ;
118+ }
119+
30120function convertWorkflowCallSecrets (
31121 context : TemplateContext ,
32122 token : MappingToken
0 commit comments