Allow inputs to accept stringified JSON

This commit is contained in:
BToersche 2021-05-13 22:27:39 +02:00
parent 0b90c2eee6
commit 5442a4e8a3
No known key found for this signature in database
GPG Key ID: CC102EACD57CB45A
4 changed files with 102 additions and 49 deletions

View File

@ -67,6 +67,14 @@ steps:
!path/**/*.tmp !path/**/*.tmp
``` ```
### Upload multiple artifacts using a JSON string
```yaml
- uses: actions/upload-artifact@v2
with:
name: '["my-artifact", "my-artifact-2"]'
path: '["path/to/artifact/1/", "path/to/artifact/2/"]'
```
For supported wildcards along with behavior and documentation, see [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) which is used internally to search for files. For supported wildcards along with behavior and documentation, see [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) which is used internally to search for files.
If a wildcard pattern is used, the path hierarchy will be preserved after the first wildcard pattern. If a wildcard pattern is used, the path hierarchy will be preserved after the first wildcard pattern.

View File

@ -3,8 +3,7 @@ description: 'Upload a build artifact that can be used by subsequent workflow st
author: 'GitHub' author: 'GitHub'
inputs: inputs:
name: name:
description: 'Artifact name' description: 'Artifact name, default is "artifact"'
default: 'artifact'
path: path:
description: 'A file, directory or wildcard pattern that describes what to upload' description: 'A file, directory or wildcard pattern that describes what to upload'
required: true required: true

51
dist/index.js vendored
View File

@ -6581,12 +6581,16 @@ const constants_1 = __webpack_require__(694);
function getInputs() { function getInputs() {
const name = core.getInput(constants_1.Inputs.Name); const name = core.getInput(constants_1.Inputs.Name);
const path = core.getInput(constants_1.Inputs.Path, { required: true }); const path = core.getInput(constants_1.Inputs.Path, { required: true });
const searchPath = Array.isArray(path) ? path : [path]; const searchPath = parseFromJSON(path) || [path];
const defaultArtifactName = 'artifact'; const defaultArtifactName = 'artifact';
// Accepts an individual value or an array as input, if array sizes don't match, use default value instead // Accepts an individual value or an array as input, if array sizes don't match, use default value instead
const artifactName = Array.isArray(name) const artifactName = parseParamaterToArrayFromInput(name, searchPath.length, defaultArtifactName, (defaultInput, index) => {
? name.concat(new Array(Math.max(0, searchPath.length - name.length)).fill(defaultArtifactName)) const artifactIndexStr = index == 0 ? '' : `_${index + 1}`;
: new Array(searchPath.length).fill(name || defaultArtifactName); return `${defaultInput}${artifactIndexStr}`;
});
// Accepts an individual value or an array as input
const retention = core.getInput(constants_1.Inputs.RetentionDays);
const retentionDays = parseParamaterToArrayFromInput(retention, searchPath.length, undefined, defaultInput => defaultInput).map(parseRetentionDays);
const ifNoFilesFound = core.getInput(constants_1.Inputs.IfNoFilesFound); const ifNoFilesFound = core.getInput(constants_1.Inputs.IfNoFilesFound);
const noFileBehavior = constants_1.NoFileOptions[ifNoFilesFound]; const noFileBehavior = constants_1.NoFileOptions[ifNoFilesFound];
if (!noFileBehavior) { if (!noFileBehavior) {
@ -6595,35 +6599,44 @@ function getInputs() {
const inputs = { const inputs = {
artifactName, artifactName,
searchPath, searchPath,
retentionDays,
ifNoFilesFound: noFileBehavior ifNoFilesFound: noFileBehavior
}; };
// Accepts an individual value or an array as input
const retentionDays = core.getInput(constants_1.Inputs.RetentionDays);
if (Array.isArray(retentionDays)) {
// If array sizes don't match, use default value instead
inputs.retentionDays = retentionDays
.map(parseRetentionDays)
.concat(new Array(Math.max(0, searchPath.length - retentionDays.length)).fill(undefined));
}
else {
const retention = parseRetentionDays(retentionDays);
inputs.retentionDays = new Array(searchPath.length).fill(retention);
}
return inputs; return inputs;
} }
exports.getInputs = getInputs; exports.getInputs = getInputs;
function parseParamaterToArrayFromInput(input, requiredLength, defaultInput, defaultFunc) {
// Accepts an individual value or an array as input, if array size doesn't match the required length, fill the rest with a default value
const inputArray = parseFromJSON(input || '[]');
if (inputArray != null) {
// If a stringified JSON array is provided, use it and concat it with the default when required
return inputArray.concat(Array.from({ length: Math.max(0, requiredLength - inputArray.length) }, (_, index) => defaultFunc(defaultInput, index)));
}
// If a string is provided, fill the array with that value
return Array.from({ length: Math.max(0, requiredLength) }, (_, index) => defaultFunc(input || defaultInput, index));
}
function parseFromJSON(jsonStr) {
try {
const json = JSON.parse(jsonStr);
if (Array.isArray(json)) {
return json;
}
}
catch (_err) {
// Input wasn't a stringified JSON array (string[]), return undefined to signal an invalid JSON was provided
}
return undefined;
}
function parseRetentionDays(retentionDaysStr) { function parseRetentionDays(retentionDaysStr) {
if (retentionDaysStr) { if (retentionDaysStr != null) {
const retentionDays = parseInt(retentionDaysStr); const retentionDays = parseInt(retentionDaysStr);
if (isNaN(retentionDays)) { if (isNaN(retentionDays)) {
core.setFailed('Invalid retention-days'); core.setFailed('Invalid retention-days');
} }
return retentionDays; return retentionDays;
} }
else {
return undefined; return undefined;
} }
}
/***/ }), /***/ }),

View File

@ -9,17 +9,28 @@ export function getInputs(): UploadInputs {
const name = core.getInput(Inputs.Name) const name = core.getInput(Inputs.Name)
const path = core.getInput(Inputs.Path, {required: true}) const path = core.getInput(Inputs.Path, {required: true})
const searchPath = Array.isArray(path) ? path : [path] const searchPath = parseFromJSON(path) || [path]
const defaultArtifactName = 'artifact' const defaultArtifactName = 'artifact'
// Accepts an individual value or an array as input, if array sizes don't match, use default value instead // Accepts an individual value or an array as input, if array sizes don't match, use default value instead
const artifactName = Array.isArray(name) const artifactName = parseParamaterToArrayFromInput(
? name.concat( name,
new Array(Math.max(0, searchPath.length - name.length)).fill( searchPath.length,
defaultArtifactName defaultArtifactName,
(defaultInput, index) => {
const artifactIndexStr = index == 0 ? '' : `_${index + 1}`
return `${defaultInput}${artifactIndexStr}`
}
) )
)
: new Array(searchPath.length).fill(name || defaultArtifactName) // Accepts an individual value or an array as input
const retention = core.getInput(Inputs.RetentionDays)
const retentionDays = parseParamaterToArrayFromInput(
retention,
searchPath.length,
undefined,
defaultInput => defaultInput
).map(parseRetentionDays)
const ifNoFilesFound = core.getInput(Inputs.IfNoFilesFound) const ifNoFilesFound = core.getInput(Inputs.IfNoFilesFound)
const noFileBehavior: NoFileOptions = NoFileOptions[ifNoFilesFound] const noFileBehavior: NoFileOptions = NoFileOptions[ifNoFilesFound]
@ -37,38 +48,60 @@ export function getInputs(): UploadInputs {
const inputs = { const inputs = {
artifactName, artifactName,
searchPath, searchPath,
retentionDays,
ifNoFilesFound: noFileBehavior ifNoFilesFound: noFileBehavior
} as UploadInputs } as UploadInputs
// Accepts an individual value or an array as input return inputs
const retentionDays = core.getInput(Inputs.RetentionDays)
if (Array.isArray(retentionDays)) {
// If array sizes don't match, use default value instead
inputs.retentionDays = retentionDays
.map(parseRetentionDays)
.concat(
new Array(Math.max(0, searchPath.length - retentionDays.length)).fill(
undefined
)
)
} else {
const retention = parseRetentionDays(retentionDays)
inputs.retentionDays = new Array(searchPath.length).fill(retention)
} }
return inputs function parseParamaterToArrayFromInput(
input: string | undefined,
requiredLength: number,
defaultInput: string | undefined,
defaultFunc: (
defaultInput: string | undefined,
index: number
) => string | undefined
): (string | undefined)[] {
// Accepts an individual value or an array as input, if array size doesn't match the required length, fill the rest with a default value
const inputArray = parseFromJSON(input || '[]')
if (inputArray != null) {
// If a stringified JSON array is provided, use it and concat it with the default when required
return (<(string | undefined)[]>inputArray).concat(
Array.from(
{length: Math.max(0, requiredLength - inputArray.length)},
(_, index) => defaultFunc(defaultInput, index)
)
)
}
// If a string is provided, fill the array with that value
return Array.from({length: Math.max(0, requiredLength)}, (_, index) =>
defaultFunc(input || defaultInput, index)
)
}
function parseFromJSON(jsonStr: string): string[] | undefined {
try {
const json = <string[]>JSON.parse(jsonStr)
if (Array.isArray(json)) {
return json
}
} catch (_err) {
// Input wasn't a stringified JSON array (string[]), return undefined to signal an invalid JSON was provided
}
return undefined
} }
function parseRetentionDays( function parseRetentionDays(
retentionDaysStr: string | undefined retentionDaysStr: string | undefined
): number | undefined { ): number | undefined {
if (retentionDaysStr) { if (retentionDaysStr != null) {
const retentionDays = parseInt(retentionDaysStr) const retentionDays = parseInt(retentionDaysStr)
if (isNaN(retentionDays)) { if (isNaN(retentionDays)) {
core.setFailed('Invalid retention-days') core.setFailed('Invalid retention-days')
} }
return retentionDays return retentionDays
} else { }
return undefined return undefined
} }
}