Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/upload/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {info, setFailed, warning} from '@actions/core'

/* eslint-disable no-unused-vars */
export enum Inputs {
Name = 'name',
Expand Down Expand Up @@ -25,3 +27,9 @@ export enum NoFileOptions {
*/
ignore = 'ignore'
}

export const NoFileFunctionMap = {
[NoFileOptions.error]: setFailed,
[NoFileOptions.ignore]: info,
[NoFileOptions.warn]: warning
} as const
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NoFileFunctionMap could potentially be called with an undefined key if inputs.ifNoFilesFound contains an unexpected value. Consider adding a type annotation or default case to handle this scenario gracefully.

Suggested change
} as const
} as const
/**
* Safely retrieves the function for the given NoFileOptions value.
* Defaults to warning if the value is unrecognized.
*/
export function getNoFileFunction(
option: string
): typeof setFailed | typeof info | typeof warning {
if (option === NoFileOptions.error) {
return setFailed;
} else if (option === NoFileOptions.ignore) {
return info;
} else if (option === NoFileOptions.warn) {
return warning;
} else {
// Default to warning for unexpected values
warning(`Unknown if-no-files-found option: ${option}, defaulting to 'warn'.`);
return warning;
}
}

Copilot uses AI. Check for mistakes.
68 changes: 26 additions & 42 deletions src/upload/upload-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -30,51 +30,35 @@ export async function run(): Promise<void> {
)
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`
NoFileFunctionMap[inputs.ifNoFilesFound]?.(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
Comment on lines +33 to 35
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using optional chaining here means that if inputs.ifNoFilesFound is an unexpected value, the function will silently do nothing instead of providing feedback about the invalid option. Consider adding explicit error handling for unsupported values.

Suggested change
NoFileFunctionMap[inputs.ifNoFilesFound]?.(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
const noFileHandler = NoFileFunctionMap[inputs.ifNoFilesFound];
if (noFileHandler) {
noFileHandler(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
);
} else {
core.setFailed(
`Invalid value for 'if-no-files-found': '${inputs.ifNoFilesFound}'. Supported options are: ${Object.keys(NoFileFunctionMap).join(', ')}.`
);
}

Copilot uses AI. Check for mistakes.
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
return
}

if (inputs.overwrite) {
await deleteArtifactIfExists(inputs.artifactName)
}
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}`)

const options: UploadArtifactOptions = {}
if (inputs.retentionDays) {
options.retentionDays = inputs.retentionDays
}
if (inputs.overwrite) {
await deleteArtifactIfExists(inputs.artifactName)
}

if (typeof inputs.compressionLevel !== 'undefined') {
options.compressionLevel = inputs.compressionLevel
}
const options: UploadArtifactOptions = {}
if (inputs.retentionDays) {
options.retentionDays = inputs.retentionDays
}

await uploadArtifact(
inputs.artifactName,
searchResult.filesToUpload,
searchResult.rootDirectory,
options
)
if (typeof inputs.compressionLevel !== 'undefined') {
options.compressionLevel = inputs.compressionLevel
}

await uploadArtifact(
inputs.artifactName,
searchResult.filesToUpload,
searchResult.rootDirectory,
options
)
}