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:
@@ -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(
|
||||
|
||||
@@ -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 ||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
11
src/main.ts
11
src/main.ts
@@ -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 =
|
||||
|
||||
16
src/utils.ts
16
src/utils.ts
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user