Add new option to specify behavior if no files found (#104)

* Add new option to specify behavior if no files found
This commit is contained in:
Konrad Pabjan
2020-07-31 17:27:37 +02:00
committed by GitHub
parent 5f948bc1f0
commit 5ba29a7d5b
7 changed files with 188 additions and 25 deletions

View File

@ -1,8 +1,22 @@
export enum Inputs {
Name = 'name',
Path = 'path'
Path = 'path',
IfNoFilesFound = 'if-no-files-found'
}
export function getDefaultArtifactName(): string {
return 'artifact'
export enum NoFileOptions {
/**
* Default. Output a warning but do not fail the action
*/
warn = 'warn',
/**
* Fail the action with an error message
*/
error = 'error',
/**
* Do not output any warnings or errors, the action does not fail
*/
ignore = 'ignore'
}

30
src/input-helper.ts Normal file
View File

@ -0,0 +1,30 @@
import * as core from '@actions/core'
import {Inputs, NoFileOptions} from './constants'
import {UploadInputs} from './upload-inputs'
/**
* Helper to get all the inputs for the action
*/
export function getInputs(): UploadInputs {
const name = core.getInput(Inputs.Name)
const path = core.getInput(Inputs.Path, {required: true})
const ifNoFilesFound = core.getInput(Inputs.IfNoFilesFound)
const noFileBehavior: NoFileOptions = NoFileOptions[ifNoFilesFound]
if (!noFileBehavior) {
core.setFailed(
`Unrecognized ${
Inputs.IfNoFilesFound
} input. Provided: ${ifNoFilesFound}. Available options: ${Object.keys(
NoFileOptions
)}`
)
}
return {
artifactName: name,
searchPath: path,
ifNoFilesFound: noFileBehavior
}
}

View File

@ -1,21 +1,38 @@
import * as core from '@actions/core'
import {create, UploadOptions} from '@actions/artifact'
import {Inputs, getDefaultArtifactName} from './constants'
import {findFilesToUpload} from './search'
import {getInputs} from './input-helper'
import {NoFileOptions} from './constants'
async function run(): Promise<void> {
try {
const name = core.getInput(Inputs.Name, {required: false})
const path = core.getInput(Inputs.Path, {required: true})
const searchResult = await findFilesToUpload(path)
const inputs = getInputs()
const searchResult = await findFilesToUpload(inputs.searchPath)
if (searchResult.filesToUpload.length === 0) {
core.warning(
`No files were found for the provided path: ${path}. No artifacts will be uploaded.`
)
// No files were found, different use cases warrant different types of behavior if nothing is found
switch (inputs.ifNoFilesFound) {
case NoFileOptions.warn: {
core.warning(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
case NoFileOptions.error: {
core.setFailed(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
case NoFileOptions.ignore: {
core.info(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
}
} else {
core.info(
`With the provided path, there will be ${searchResult.filesToUpload.length} files uploaded`
`With the provided path, there will be ${searchResult.filesToUpload.length} file(s) uploaded`
)
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
@ -24,7 +41,7 @@ async function run(): Promise<void> {
continueOnError: false
}
const uploadResponse = await artifactClient.uploadArtifact(
name || getDefaultArtifactName(),
inputs.artifactName,
searchResult.filesToUpload,
searchResult.rootDirectory,
options

18
src/upload-inputs.ts Normal file
View File

@ -0,0 +1,18 @@
import {NoFileOptions} from './constants'
export interface UploadInputs {
/**
* The name of the artifact that will be uploaded
*/
artifactName: string
/**
* The search path used to describe what to upload as part of the artifact
*/
searchPath: string
/**
* The desired behavior if no files are found with the provided search path
*/
ifNoFilesFound: NoFileOptions
}