mirror of
https://github.com/tj-actions/changed-files
synced 2024-12-17 03:47:20 +00:00
feat: add support for restricting the deleted files output to only deleted directories
This commit is contained in:
parent
0b947ed818
commit
e6ce728d79
5 changed files with 71 additions and 8 deletions
|
@ -24,12 +24,14 @@ export const processChangedFiles = async ({
|
|||
filePatterns,
|
||||
allDiffFiles,
|
||||
inputs,
|
||||
yamlFilePatterns
|
||||
yamlFilePatterns,
|
||||
workingDirectory
|
||||
}: {
|
||||
filePatterns: string[]
|
||||
allDiffFiles: ChangedFiles
|
||||
inputs: Inputs
|
||||
yamlFilePatterns: Record<string, string[]>
|
||||
workingDirectory?: string
|
||||
}): Promise<void> => {
|
||||
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()
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -152,7 +152,8 @@ const getChangedFilesFromLocalGitHistory = async ({
|
|||
filePatterns,
|
||||
allDiffFiles,
|
||||
inputs,
|
||||
yamlFilePatterns
|
||||
yamlFilePatterns,
|
||||
workingDirectory
|
||||
})
|
||||
|
||||
if (inputs.includeAllOldNewRenamedFiles) {
|
||||
|
|
32
src/utils.ts
32
src/utils.ts
|
@ -139,7 +139,7 @@ export const verifyMinimumGitVersion = async (): Promise<void> => {
|
|||
* @param filePath - path to check
|
||||
* @returns path exists
|
||||
*/
|
||||
const exists = async (filePath: string): Promise<boolean> => {
|
||||
export const exists = async (filePath: string): Promise<boolean> => {
|
||||
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<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
|
||||
* @param cwd - working directory
|
||||
|
|
Loading…
Reference in a new issue