Skip to content

Commit 268ee33

Browse files
authored
Fall back to default inputs (#139)
This PR adds a check so that, if an input is not present in the environment variables, the default value is used from the `action.yml`/`action.yaml` file (if a default is present).
2 parents 6b7a9eb + c1b77d8 commit 268ee33

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

__tests__/stubs/core/core.test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ describe('Core', () => {
178178
expect(getInput('test')).toEqual('test-lower')
179179
})
180180

181+
it('Gets default inputs', () => {
182+
delete process.env.INPUT_TEST
183+
delete process.env.INPUT_test
184+
185+
EnvMeta.inputs = {
186+
test: {
187+
description: 'test',
188+
required: true,
189+
default: 'default'
190+
}
191+
}
192+
193+
expect(getInput('test')).toEqual('default')
194+
})
195+
181196
it('Returns an empty string', () => {
182197
expect(getInput('test-input-missing')).toEqual('')
183198
})
@@ -214,6 +229,21 @@ describe('Core', () => {
214229
])
215230
})
216231

232+
it('Gets default inputs', () => {
233+
delete process.env.INPUT_TEST
234+
delete process.env.INPUT_test
235+
236+
EnvMeta.inputs = {
237+
test: {
238+
description: 'test',
239+
required: true,
240+
default: 'default'
241+
}
242+
}
243+
244+
expect(getMultilineInput('test')).toEqual(['default'])
245+
})
246+
217247
it('Returns an empty list if the input is not found', () => {
218248
expect(getMultilineInput('test-input-missing')).toMatchObject([])
219249
})
@@ -246,6 +276,21 @@ describe('Core', () => {
246276
expect(getBooleanInput('test')).toBeFalsy()
247277
})
248278

279+
it('Gets default inputs', () => {
280+
delete process.env.INPUT_TEST
281+
delete process.env.INPUT_test
282+
283+
EnvMeta.inputs = {
284+
test: {
285+
description: 'test',
286+
required: true,
287+
default: 'false'
288+
}
289+
}
290+
291+
expect(getBooleanInput('test')).toEqual(false)
292+
})
293+
249294
it('Throws an error if the input is required and not found', () => {
250295
expect(() =>
251296
getBooleanInput('test-input-missing', {

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@github/local-action",
33
"description": "Local Debugging for GitHub Actions",
4-
"version": "2.4.0",
4+
"version": "2.5.0",
55
"type": "module",
66
"author": "Nick Alteen <ncalteen@github.com>",
77
"private": false,

src/stubs/core/core.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ export function getInput(name: string, options?: InputOptions): string {
200200
process.env[`INPUT_${name.replace(/ /g, '_')}`] ||
201201
''
202202

203+
// If the input is not present in the environment variables, it has not been
204+
// set. In that case, check the default value.
205+
if (input === '' && EnvMeta.inputs[name]?.default !== undefined)
206+
input = EnvMeta.inputs[name].default.toString()
207+
203208
// Throw an error if the input is required and not supplied
204209
if (options && options.required === true && input === '')
205210
throw new Error(`Input required and not supplied: ${name}`)
@@ -224,14 +229,22 @@ export function getMultilineInput(
224229
options?: InputOptions
225230
): string[] {
226231
// Get input by name, split by newline, and filter out empty strings
227-
const input: string[] = (
232+
let input: string[] = (
228233
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] ||
229234
process.env[`INPUT_${name.replace(/ /g, '_')}`] ||
230235
''
231236
)
232237
.split('\n')
233238
.filter(x => x !== '')
234239

240+
// If the input is not present in the environment variables, it has not been
241+
// set. In that case, check the default value.
242+
if (input.length === 0 && EnvMeta.inputs[name]?.default !== undefined)
243+
input = EnvMeta.inputs[name].default
244+
.toString()
245+
.split('\n')
246+
.filter(x => x !== '')
247+
235248
// Throw an error if the input is required and not supplied
236249
if (options && options.required === true && input.length === 0)
237250
throw new Error(`Input required and not supplied: ${name}`)
@@ -257,12 +270,17 @@ export function getBooleanInput(name: string, options?: InputOptions): boolean {
257270
// using proxyquire's `callThru()` option.
258271

259272
// Get input by name, or an empty string if not found
260-
const input: string = (
273+
let input: string = (
261274
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] ||
262275
process.env[`INPUT_${name.replace(/ /g, '_')}`] ||
263276
''
264277
).trim()
265278

279+
// If the input is not present in the environment variables, it has not been
280+
// set. In that case, check the default value.
281+
if (input === '' && EnvMeta.inputs[name]?.default !== undefined)
282+
input = EnvMeta.inputs[name].default.trim()
283+
266284
// Throw an error if the input is required and not supplied
267285
if (options && options.required === true && input === '')
268286
throw new Error(`Input required and not supplied: ${name}`)

0 commit comments

Comments
 (0)