3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-01-29 13:34:51 +00:00

feat: add support for outputting renamed files as deleted and added (#1260)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tonye Jack 2023-06-14 13:59:31 -06:00 committed by GitHub
parent c648759d89
commit 90ef0b1b22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 16 deletions

View file

@ -869,6 +869,41 @@ jobs:
exit 1
shell:
bash
- name: Run changed-files for old new filenames test rename 2 output as deleted and added
id: changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added
uses: ./
with:
base_sha: 4d04215
sha: fe238e6
fetch_depth: 60000
include_all_old_new_renamed_files: true
output_renamed_files_as_deleted_and_added: true
- name: Show output
run: |
echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs) }}'
shell:
bash
- name: Check all_old_new_renamed_files output
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files, 'test/test rename 2.txt,test/test rename-2.txt')"
run: |
echo "Invalid output: Expected to include (test/test rename 2.txt test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files }})"
exit 1
shell:
bash
- name: Check deleted_files output
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files, 'test/test rename 2.txt')"
run: |
echo "Invalid output: Expected to include (test/test rename 2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files }})"
exit 1
shell:
bash
- name: Check added_files output
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files, 'test/test rename-2.txt')"
run: |
echo "Invalid output: Expected to include (test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files }})"
exit 1
shell:
bash
- name: Run changed-files with specific files
id: changed-files-specific
uses: ./

View file

@ -117,6 +117,10 @@ inputs:
description: "Directory to store output files."
required: false
default: ".github/outputs"
output_renamed_files_as_deleted_and_added:
description: "Output renamed files as deleted and added files."
required: false
default: "false"
outputs:
added_files:

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

View file

@ -88,18 +88,21 @@ export const getAllDiffFiles = async ({
workingDirectory,
hasSubmodule,
diffResult,
submodulePaths
submodulePaths,
outputRenamedFilesAsDeletedAndAdded
}: {
workingDirectory: string
hasSubmodule: boolean
diffResult: DiffResult
submodulePaths: string[]
outputRenamedFilesAsDeletedAndAdded: boolean
}): Promise<ChangedFiles> => {
const files = await getAllChangedFiles({
cwd: workingDirectory,
sha1: diffResult.previousSha,
sha2: diffResult.currentSha,
diff: diffResult.diff
diff: diffResult.diff,
outputRenamedFilesAsDeletedAndAdded
})
if (hasSubmodule) {
@ -124,7 +127,8 @@ export const getAllDiffFiles = async ({
sha2: submoduleShaResult.currentSha,
diff: diffResult.diff,
isSubmodule: true,
parentDir: submodulePath
parentDir: submodulePath,
outputRenamedFilesAsDeletedAndAdded
})
for (const changeType of Object.keys(

View file

@ -370,12 +370,7 @@ export const getSHAForPullRequestEvent = async (
if (!previousSha) {
if (inputs.sinceLastRemoteCommit) {
previousSha =
env.GITHUB_EVENT_BEFORE ||
(await getRemoteBranchHeadSha({
cwd: workingDirectory,
branch: currentBranch
}))
previousSha = env.GITHUB_EVENT_BEFORE
if (
!previousSha ||

View file

@ -30,6 +30,7 @@ export type Inputs = {
sinceLastRemoteCommit: boolean
writeOutputFiles: boolean
outputDir: string
outputRenamedFilesAsDeletedAndAdded: boolean
}
export const getInputs = (): Inputs => {
@ -111,6 +112,10 @@ export const getInputs = (): Inputs => {
required: false
})
const outputDir = core.getInput('output_dir', {required: false})
const outputRenamedFilesAsDeletedAndAdded = core.getBooleanInput(
'output_renamed_files_as_deleted_and_added',
{required: false}
)
const inputs: Inputs = {
files,
@ -139,7 +144,8 @@ export const getInputs = (): Inputs => {
escapeJson,
sinceLastRemoteCommit,
writeOutputFiles,
outputDir
outputDir,
outputRenamedFilesAsDeletedAndAdded
}
if (fetchDepth) {

View file

@ -61,6 +61,8 @@ export async function run(): Promise<void> {
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
const outputRenamedFilesAsDeletedAndAdded =
inputs.outputRenamedFilesAsDeletedAndAdded
let submodulePaths: string[] = []
if (hasSubmodule) {
@ -118,7 +120,8 @@ export async function run(): Promise<void> {
workingDirectory,
hasSubmodule,
diffResult,
submodulePaths
submodulePaths,
outputRenamedFilesAsDeletedAndAdded
})
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
@ -261,7 +264,8 @@ export async function run(): Promise<void> {
const otherChangedFiles = allOtherChangedFiles
.split(inputs.separator)
.filter(
filePath => !allChangedFiles.split(inputs.separator).includes(filePath)
(filePath: string) =>
!allChangedFiles.split(inputs.separator).includes(filePath)
)
const onlyChanged =
@ -320,7 +324,8 @@ export async function run(): Promise<void> {
const otherModifiedFiles = allOtherModifiedFiles
.split(inputs.separator)
.filter(
filePath => !allModifiedFiles.split(inputs.separator).includes(filePath)
(filePath: string) =>
!allModifiedFiles.split(inputs.separator).includes(filePath)
)
const onlyModified =

View file

@ -410,7 +410,8 @@ export const getAllChangedFiles = async ({
sha2,
diff,
isSubmodule = false,
parentDir = ''
parentDir = '',
outputRenamedFilesAsDeletedAndAdded = false
}: {
cwd: string
sha1: string
@ -418,6 +419,7 @@ export const getAllChangedFiles = async ({
diff: string
isSubmodule?: boolean
parentDir?: string
outputRenamedFilesAsDeletedAndAdded?: boolean
}): Promise<ChangedFiles> => {
const {exitCode, stdout, stderr} = await exec.getExecOutput(
'git',
@ -466,13 +468,21 @@ export const getAllChangedFiles = async ({
const lines = stdout.split('\n').filter(Boolean)
for (const line of lines) {
const [changeType, filePath] = line.split('\t')
const [changeType, filePath, newPath = ''] = line.split('\t')
const normalizedFilePath = isSubmodule
? normalizePath(path.join(parentDir, filePath))
: normalizePath(filePath)
const normalizedNewPath = isSubmodule
? normalizePath(path.join(parentDir, newPath))
: normalizePath(newPath)
if (changeType.startsWith('R')) {
changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath)
if (outputRenamedFilesAsDeletedAndAdded) {
changedFiles[ChangeTypeEnum.Deleted].push(normalizedFilePath)
changedFiles[ChangeTypeEnum.Added].push(normalizedNewPath)
} else {
changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath)
}
} else {
changedFiles[changeType as ChangeTypeEnum].push(normalizedFilePath)
}