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
9 changed files with 102 additions and 33 deletions

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)
}