From 53778d2818d022eddb3de6403dc5a54da251a02d Mon Sep 17 00:00:00 2001 From: Phred Date: Sun, 5 Oct 2025 09:32:58 -0500 Subject: [PATCH] simplified logic for handling "no files" upload notification --- src/upload/constants.ts | 8 ++++ src/upload/upload-artifact.ts | 76 ++++++++++++++--------------------- 2 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/upload/constants.ts b/src/upload/constants.ts index 8407bc1..67fc374 100644 --- a/src/upload/constants.ts +++ b/src/upload/constants.ts @@ -1,3 +1,5 @@ +import {info, setFailed, warning} from '@actions/core' + /* eslint-disable no-unused-vars */ export enum Inputs { Name = 'name', @@ -25,3 +27,9 @@ export enum NoFileOptions { */ ignore = 'ignore' } + +export const NoFileFunctionMap = { + [NoFileOptions.error]: setFailed, + [NoFileOptions.ignore]: info, + [NoFileOptions.warn]: warning +} as const diff --git a/src/upload/upload-artifact.ts b/src/upload/upload-artifact.ts index 6c06d53..5551ea0 100644 --- a/src/upload/upload-artifact.ts +++ b/src/upload/upload-artifact.ts @@ -5,7 +5,7 @@ import artifact, { } from '@actions/artifact' import {findFilesToUpload} from '../shared/search' import {getInputs} from './input-helper' -import {NoFileOptions} from './constants' +import {NoFileFunctionMap, NoFileOptions} from './constants' import {uploadArtifact} from '../shared/upload-artifact' async function deleteArtifactIfExists(artifactName: string): Promise { @@ -30,51 +30,35 @@ export async function run(): Promise { ) if (searchResult.filesToUpload.length === 0) { // 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 { - const s = searchResult.filesToUpload.length === 1 ? '' : 's' - core.info( - `With the provided path, there will be ${searchResult.filesToUpload.length} file${s} uploaded` - ) - core.debug(`Root artifact directory is ${searchResult.rootDirectory}`) - - if (inputs.overwrite) { - await deleteArtifactIfExists(inputs.artifactName) - } - - const options: UploadArtifactOptions = {} - if (inputs.retentionDays) { - options.retentionDays = inputs.retentionDays - } - - if (typeof inputs.compressionLevel !== 'undefined') { - options.compressionLevel = inputs.compressionLevel - } - - await uploadArtifact( - inputs.artifactName, - searchResult.filesToUpload, - searchResult.rootDirectory, - options + NoFileFunctionMap[inputs.ifNoFilesFound]?.( + `No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.` ) + return } + + const s = searchResult.filesToUpload.length === 1 ? '' : 's' + core.info( + `With the provided path, there will be ${searchResult.filesToUpload.length} file${s} uploaded` + ) + core.debug(`Root artifact directory is ${searchResult.rootDirectory}`) + + if (inputs.overwrite) { + await deleteArtifactIfExists(inputs.artifactName) + } + + const options: UploadArtifactOptions = {} + if (inputs.retentionDays) { + options.retentionDays = inputs.retentionDays + } + + if (typeof inputs.compressionLevel !== 'undefined') { + options.compressionLevel = inputs.compressionLevel + } + + await uploadArtifact( + inputs.artifactName, + searchResult.filesToUpload, + searchResult.rootDirectory, + options + ) }