From e6ce728d79e6c15a69a0eea43b1968c2f9da2e8b Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Thu, 21 Sep 2023 20:47:12 -0600 Subject: [PATCH] feat: add support for restricting the deleted files output to only deleted directories --- src/changedFiles.ts | 13 +++++++++---- src/changedFilesOutput.ts | 23 +++++++++++++++++++++-- src/inputs.ts | 8 ++++++++ src/main.ts | 3 ++- src/utils.ts | 32 +++++++++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/changedFiles.ts b/src/changedFiles.ts index 251c8829..8e62cd0b 100644 --- a/src/changedFiles.ts +++ b/src/changedFiles.ts @@ -24,12 +24,14 @@ export const processChangedFiles = async ({ filePatterns, allDiffFiles, inputs, - yamlFilePatterns + yamlFilePatterns, + workingDirectory }: { filePatterns: string[] allDiffFiles: ChangedFiles inputs: Inputs yamlFilePatterns: Record + workingDirectory?: string }): Promise => { if (filePatterns.length > 0) { core.startGroup('changed-files-patterns') @@ -44,7 +46,8 @@ export const processChangedFiles = async ({ allDiffFiles, allFilteredDiffFiles, inputs, - filePatterns + filePatterns, + workingDirectory }) core.info('All Done!') core.endGroup() @@ -71,7 +74,8 @@ export const processChangedFiles = async ({ allFilteredDiffFiles, inputs, filePatterns: yamlFilePatterns[key], - outputPrefix: key + outputPrefix: key, + workingDirectory }) if (anyModified) { modifiedKeys.push(key) @@ -106,7 +110,8 @@ export const processChangedFiles = async ({ await setOutputsAndGetModifiedAndChangedFilesStatus({ allDiffFiles, allFilteredDiffFiles: allDiffFiles, - inputs + inputs, + workingDirectory }) core.info('All Done!') core.endGroup() diff --git a/src/changedFilesOutput.ts b/src/changedFilesOutput.ts index 8905fc9d..016c259f 100644 --- a/src/changedFilesOutput.ts +++ b/src/changedFilesOutput.ts @@ -1,4 +1,5 @@ import * as core from '@actions/core' +import path from 'path' import { ChangedFiles, ChangeTypeEnum, @@ -6,7 +7,7 @@ import { getChangeTypeFiles } from './changedFiles' import {Inputs} from './inputs' -import {getOutputKey, setArrayOutput, setOutput} from './utils' +import {getOutputKey, setArrayOutput, setOutput, exists} from './utils' const getArrayFromPaths = ( paths: string | string[], @@ -20,13 +21,15 @@ export const setOutputsAndGetModifiedAndChangedFilesStatus = async ({ allFilteredDiffFiles, inputs, filePatterns = [], - outputPrefix = '' + outputPrefix = '', + workingDirectory }: { allDiffFiles: ChangedFiles allFilteredDiffFiles: ChangedFiles inputs: Inputs filePatterns?: string[] outputPrefix?: string + workingDirectory?: string }): Promise<{anyModified: boolean; anyChanged: boolean}> => { const addedFiles = await getChangeTypeFiles({ inputs, @@ -388,6 +391,22 @@ export const setOutputsAndGetModifiedAndChangedFilesStatus = async ({ changeTypes: [ChangeTypeEnum.Deleted] }) core.debug(`Deleted files: ${JSON.stringify(deletedFiles)}`) + + if ( + inputs.dirNamesDeletedFilesIncludeOnlyDeletedDirs && + inputs.dirNames && + workingDirectory + ) { + const deletedFilesPaths: string[] = [] + for (const deletedPath of getArrayFromPaths(deletedFiles.paths, inputs)) { + if (!(await exists(path.join(workingDirectory, deletedPath)))) { + deletedFilesPaths.push(deletedPath) + } + } + deletedFiles.paths = deletedFilesPaths + deletedFiles.count = deletedFilesPaths.length.toString() + } + await setOutput({ key: getOutputKey('deleted_files', outputPrefix), value: deletedFiles.paths, diff --git a/src/inputs.ts b/src/inputs.ts index 3c29eedd..fbda124f 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -31,6 +31,7 @@ export type Inputs = { dirNamesExcludeCurrentDir: boolean dirNamesIncludeFiles: string dirNamesIncludeFilesSeparator: string + dirNamesDeletedFilesIncludeOnlyDeletedDirs: boolean json: boolean escapeJson: boolean fetchDepth?: number @@ -210,6 +211,12 @@ export const getInputs = (): Inputs => { required: false } ) + const dirNamesDeletedFilesIncludeOnlyDeletedDirs = core.getBooleanInput( + 'dir_names_deleted_files_include_only_deleted_dirs', + { + required: false + } + ) const inputs: Inputs = { files, @@ -254,6 +261,7 @@ export const getInputs = (): Inputs => { dirNamesExcludeCurrentDir, dirNamesIncludeFiles, dirNamesIncludeFilesSeparator, + dirNamesDeletedFilesIncludeOnlyDeletedDirs, json, escapeJson, writeOutputFiles, diff --git a/src/main.ts b/src/main.ts index c323f498..a2b114c3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -152,7 +152,8 @@ const getChangedFilesFromLocalGitHistory = async ({ filePatterns, allDiffFiles, inputs, - yamlFilePatterns + yamlFilePatterns, + workingDirectory }) if (inputs.includeAllOldNewRenamedFiles) { diff --git a/src/utils.ts b/src/utils.ts index 65975e0e..f330cd92 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -139,7 +139,7 @@ export const verifyMinimumGitVersion = async (): Promise => { * @param filePath - path to check * @returns path exists */ -const exists = async (filePath: string): Promise => { +export const exists = async (filePath: string): Promise => { try { await fs.access(filePath) return true @@ -233,6 +233,36 @@ export const updateGitGlobalConfig = async ({ } } +/** + * Get tracked git directories + * @param cwd - working directory + */ +export const getGitTrackedDirectories = async ({ + cwd +}: { + cwd: string +}): Promise => { + const {exitCode, stdout, stderr} = await exec.getExecOutput( + 'git', + ['ls-tree', '-d', '-r', '--name-only', 'HEAD'], + { + cwd, + ignoreReturnCode: true, + silent: !core.isDebug() + } + ) + + if (exitCode !== 0) { + core.warning(stderr || "Couldn't list tracked directories") + return [] + } + + return stdout + .trim() + .split('\n') + .map((line: string) => normalizeSeparators(line.trim())) +} + /** * Checks if a git repository is shallow * @param cwd - working directory