Skip to content

Commit 9eb310f

Browse files
PURP-12380 - Handle required and default inputs configured in action.yml (#5)
Added input validation to enforce required and default values.
1 parent 43afd88 commit 9eb310f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

dist/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5277,12 +5277,16 @@ async function run () {
52775277
core.info('Reading action.yml')
52785278
const actionFile = readFileSync(`${WORKING_DIR}/action.yml`, 'utf8')
52795279
const action = parse(actionFile)
5280-
5280+
52815281
if (!(action && action.name && action.runs && action.runs.main)) {
52825282
throw new Error('Malformed action.yml found')
52835283
}
52845284

52855285
core.endGroup('Cloning private action')
5286+
5287+
core.startGroup('Input Validation')
5288+
setInputs(action)
5289+
core.endGroup('Input Validation')
52865290

52875291
core.info(`Starting private action ${action.name}`)
52885292
core.startGroup(`${action.name}`)
@@ -5298,6 +5302,32 @@ function parseRepo () {
52985302
}
52995303
}
53005304

5305+
function setInputs(action){
5306+
if (!action.inputs) {
5307+
core.info('No inputs defined in action.');
5308+
return;
5309+
}
5310+
5311+
core.info(`The configured inputs are ${Object.keys(action.inputs)}`)
5312+
5313+
for (const i of Object.keys(action.inputs)) {
5314+
const formattedInputName = `INPUT_${i.toUpperCase()}`;
5315+
5316+
if ((process.env[formattedInputName])) {
5317+
core.info(`Input ${i} already set`);
5318+
continue;
5319+
} else if (!action.inputs[i].required && !action.inputs[i].default) {
5320+
core.info(`Input ${i} not required and has no default`);
5321+
continue;
5322+
} else if (action.inputs[i].required && !action.inputs[i].default) {
5323+
core.error(`Input ${i} required but not provided and no default is set`);
5324+
}
5325+
5326+
core.info(`Input ${i} not set. Using default '${action.inputs[i].default}'`)
5327+
process.env[formattedInputName] = action.inputs[i].default
5328+
}
5329+
}
5330+
53015331
run().then(() => {
53025332
core.info('Action completed successfully')
53035333
}).catch((e) => {

src/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ async function run () {
3636
core.info('Reading action.yml')
3737
const actionFile = readFileSync(`${WORKING_DIR}/action.yml`, 'utf8')
3838
const action = parse(actionFile)
39-
39+
4040
if (!(action && action.name && action.runs && action.runs.main)) {
4141
throw new Error('Malformed action.yml found')
4242
}
4343

4444
core.endGroup('Cloning private action')
45+
46+
core.startGroup('Input Validation')
47+
setInputs(action)
48+
core.endGroup('Input Validation')
4549

4650
core.info(`Starting private action ${action.name}`)
4751
core.startGroup(`${action.name}`)
@@ -57,6 +61,32 @@ function parseRepo () {
5761
}
5862
}
5963

64+
function setInputs(action){
65+
if (!action.inputs) {
66+
core.info('No inputs defined in action.');
67+
return;
68+
}
69+
70+
core.info(`The configured inputs are ${Object.keys(action.inputs)}`)
71+
72+
for (const i of Object.keys(action.inputs)) {
73+
const formattedInputName = `INPUT_${i.toUpperCase()}`;
74+
75+
if ((process.env[formattedInputName])) {
76+
core.info(`Input ${i} already set`);
77+
continue;
78+
} else if (!action.inputs[i].required && !action.inputs[i].default) {
79+
core.info(`Input ${i} not required and has no default`);
80+
continue;
81+
} else if (action.inputs[i].required && !action.inputs[i].default) {
82+
core.error(`Input ${i} required but not provided and no default is set`);
83+
}
84+
85+
core.info(`Input ${i} not set. Using default '${action.inputs[i].default}'`)
86+
process.env[formattedInputName] = action.inputs[i].default
87+
}
88+
}
89+
6090
run().then(() => {
6191
core.info('Action completed successfully')
6292
}).catch((e) => {

0 commit comments

Comments
 (0)