3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2024-12-17 13:47:20 +00:00

feat: add support for restricting the deleted files output to only deleted directories

This commit is contained in:
Tonye Jack 2023-09-21 20:47:12 -06:00
parent 0b947ed818
commit e6ce728d79
5 changed files with 71 additions and 8 deletions

View file

@ -24,12 +24,14 @@ export const processChangedFiles = async ({
filePatterns, filePatterns,
allDiffFiles, allDiffFiles,
inputs, inputs,
yamlFilePatterns yamlFilePatterns,
workingDirectory
}: { }: {
filePatterns: string[] filePatterns: string[]
allDiffFiles: ChangedFiles allDiffFiles: ChangedFiles
inputs: Inputs inputs: Inputs
yamlFilePatterns: Record<string, string[]> yamlFilePatterns: Record<string, string[]>
workingDirectory?: string
}): Promise<void> => { }): Promise<void> => {
if (filePatterns.length > 0) { if (filePatterns.length > 0) {
core.startGroup('changed-files-patterns') core.startGroup('changed-files-patterns')
@ -44,7 +46,8 @@ export const processChangedFiles = async ({
allDiffFiles, allDiffFiles,
allFilteredDiffFiles, allFilteredDiffFiles,
inputs, inputs,
filePatterns filePatterns,
workingDirectory
}) })
core.info('All Done!') core.info('All Done!')
core.endGroup() core.endGroup()
@ -71,7 +74,8 @@ export const processChangedFiles = async ({
allFilteredDiffFiles, allFilteredDiffFiles,
inputs, inputs,
filePatterns: yamlFilePatterns[key], filePatterns: yamlFilePatterns[key],
outputPrefix: key outputPrefix: key,
workingDirectory
}) })
if (anyModified) { if (anyModified) {
modifiedKeys.push(key) modifiedKeys.push(key)
@ -106,7 +110,8 @@ export const processChangedFiles = async ({
await setOutputsAndGetModifiedAndChangedFilesStatus({ await setOutputsAndGetModifiedAndChangedFilesStatus({
allDiffFiles, allDiffFiles,
allFilteredDiffFiles: allDiffFiles, allFilteredDiffFiles: allDiffFiles,
inputs inputs,
workingDirectory
}) })
core.info('All Done!') core.info('All Done!')
core.endGroup() core.endGroup()

View file

@ -1,4 +1,5 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import path from 'path'
import { import {
ChangedFiles, ChangedFiles,
ChangeTypeEnum, ChangeTypeEnum,
@ -6,7 +7,7 @@ import {
getChangeTypeFiles getChangeTypeFiles
} from './changedFiles' } from './changedFiles'
import {Inputs} from './inputs' import {Inputs} from './inputs'
import {getOutputKey, setArrayOutput, setOutput} from './utils' import {getOutputKey, setArrayOutput, setOutput, exists} from './utils'
const getArrayFromPaths = ( const getArrayFromPaths = (
paths: string | string[], paths: string | string[],
@ -20,13 +21,15 @@ export const setOutputsAndGetModifiedAndChangedFilesStatus = async ({
allFilteredDiffFiles, allFilteredDiffFiles,
inputs, inputs,
filePatterns = [], filePatterns = [],
outputPrefix = '' outputPrefix = '',
workingDirectory
}: { }: {
allDiffFiles: ChangedFiles allDiffFiles: ChangedFiles
allFilteredDiffFiles: ChangedFiles allFilteredDiffFiles: ChangedFiles
inputs: Inputs inputs: Inputs
filePatterns?: string[] filePatterns?: string[]
outputPrefix?: string outputPrefix?: string
workingDirectory?: string
}): Promise<{anyModified: boolean; anyChanged: boolean}> => { }): Promise<{anyModified: boolean; anyChanged: boolean}> => {
const addedFiles = await getChangeTypeFiles({ const addedFiles = await getChangeTypeFiles({
inputs, inputs,
@ -388,6 +391,22 @@ export const setOutputsAndGetModifiedAndChangedFilesStatus = async ({
changeTypes: [ChangeTypeEnum.Deleted] changeTypes: [ChangeTypeEnum.Deleted]
}) })
core.debug(`Deleted files: ${JSON.stringify(deletedFiles)}`) 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({ await setOutput({
key: getOutputKey('deleted_files', outputPrefix), key: getOutputKey('deleted_files', outputPrefix),
value: deletedFiles.paths, value: deletedFiles.paths,

View file

@ -31,6 +31,7 @@ export type Inputs = {
dirNamesExcludeCurrentDir: boolean dirNamesExcludeCurrentDir: boolean
dirNamesIncludeFiles: string dirNamesIncludeFiles: string
dirNamesIncludeFilesSeparator: string dirNamesIncludeFilesSeparator: string
dirNamesDeletedFilesIncludeOnlyDeletedDirs: boolean
json: boolean json: boolean
escapeJson: boolean escapeJson: boolean
fetchDepth?: number fetchDepth?: number
@ -210,6 +211,12 @@ export const getInputs = (): Inputs => {
required: false required: false
} }
) )
const dirNamesDeletedFilesIncludeOnlyDeletedDirs = core.getBooleanInput(
'dir_names_deleted_files_include_only_deleted_dirs',
{
required: false
}
)
const inputs: Inputs = { const inputs: Inputs = {
files, files,
@ -254,6 +261,7 @@ export const getInputs = (): Inputs => {
dirNamesExcludeCurrentDir, dirNamesExcludeCurrentDir,
dirNamesIncludeFiles, dirNamesIncludeFiles,
dirNamesIncludeFilesSeparator, dirNamesIncludeFilesSeparator,
dirNamesDeletedFilesIncludeOnlyDeletedDirs,
json, json,
escapeJson, escapeJson,
writeOutputFiles, writeOutputFiles,

View file

@ -152,7 +152,8 @@ const getChangedFilesFromLocalGitHistory = async ({
filePatterns, filePatterns,
allDiffFiles, allDiffFiles,
inputs, inputs,
yamlFilePatterns yamlFilePatterns,
workingDirectory
}) })
if (inputs.includeAllOldNewRenamedFiles) { if (inputs.includeAllOldNewRenamedFiles) {

View file

@ -139,7 +139,7 @@ export const verifyMinimumGitVersion = async (): Promise<void> => {
* @param filePath - path to check * @param filePath - path to check
* @returns path exists * @returns path exists
*/ */
const exists = async (filePath: string): Promise<boolean> => { export const exists = async (filePath: string): Promise<boolean> => {
try { try {
await fs.access(filePath) await fs.access(filePath)
return true 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<string[]> => {
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 * Checks if a git repository is shallow
* @param cwd - working directory * @param cwd - working directory