diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6cbbcbdd..dbdebe79 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1029,7 +1029,8 @@ jobs: if: | ( !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test/test rename-1.txt') || - !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test') + !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test') || + steps.changed-files-dir-names-specific-include-files.outputs.any_changed == 'false' ) && runner.os != 'Windows' run: | echo "Invalid output: Expected to include (test/test rename-1.txt) and (test) got (${{ steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files }})" @@ -1040,13 +1041,41 @@ jobs: if: | ( !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test\\test rename-1.txt') || - !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test') + !contains(steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files, 'test') || + steps.changed-files-dir-names-specific-include-files.outputs.any_changed == 'false' ) && runner.os == 'Windows' run: | echo "Invalid output: Expected to include (test\\test rename-1.txt) and (test) got (${{ steps.changed-files-dir-names-specific-include-files.outputs.all_changed_files }})" exit 1 shell: bash + - name: Run changed-files with dir_names for specific files and an unmatched path + id: changed-files-dir-names-specific-unmatched-path + uses: ./ + with: + base_sha: d1c0ee4 + sha: 4d04215 + fetch_depth: 60000 + dir_names: true + files: unknown/** + json: true + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-dir-names-specific-unmatched-path.outputs) }}' + shell: + bash + - name: Check dir_names output + if: | + ( + steps.changed-files-dir-names-specific-unmatched-path.outputs.all_changed_files != '[]' || + steps.changed-files-dir-names-specific-unmatched-path.outputs.any_changed == 'true' || + steps.changed-files-dir-names-specific-unmatched-path.outputs.all_changed_files_count != '0' + ) + run: | + echo "Invalid output: Expected empty array and any_changed to be false got (${{ steps.changed-files-dir-names-specific-unmatched-path.outputs.all_changed_files }}) and (${{ steps.changed-files-dir-names-specific-unmatched-path.outputs.any_changed }})" + exit 1 + shell: + bash - name: Run changed-files with forward slash separator id: changed-files-forward-slash uses: ./ diff --git a/dist/index.js b/dist/index.js index aa0b834f..d09177fc 100644 Binary files a/dist/index.js and b/dist/index.js differ diff --git a/dist/index.js.map b/dist/index.js.map index 9d33af38..397008aa 100644 Binary files a/dist/index.js.map and b/dist/index.js.map differ diff --git a/src/changedFiles.ts b/src/changedFiles.ts index 68c10e14..0853c93c 100644 --- a/src/changedFiles.ts +++ b/src/changedFiles.ts @@ -268,20 +268,15 @@ export const getChangeTypeFiles = async ({ inputs: Inputs changedFiles: ChangedFiles changeTypes: ChangeTypeEnum[] -}): Promise<{paths: string; count: string}> => { +}): Promise<{paths: string[] | string; count: string}> => { const files = [ ...new Set(getChangeTypeFilesGenerator({inputs, changedFiles, changeTypes})) ].filter(Boolean) - if (inputs.json) { - return { - paths: jsonOutput({value: files, shouldEscape: inputs.escapeJson}), - count: files.length.toString() - } - } + const paths = inputs.json ? files : files.join(inputs.separator) return { - paths: files.join(inputs.separator), + paths, count: files.length.toString() } } @@ -317,20 +312,15 @@ export const getAllChangeTypeFiles = async ({ }: { inputs: Inputs changedFiles: ChangedFiles -}): Promise<{paths: string; count: string}> => { +}): Promise<{paths: string[] | string; count: string}> => { const files = [ ...new Set(getAllChangeTypeFilesGenerator({inputs, changedFiles})) ].filter(Boolean) - if (inputs.json) { - return { - paths: jsonOutput({value: files, shouldEscape: inputs.escapeJson}), - count: files.length.toString() - } - } + const paths = inputs.json ? files : files.join(inputs.separator) return { - paths: files.join(inputs.separator), + paths, count: files.length.toString() } } diff --git a/src/changedFilesOutput.ts b/src/changedFilesOutput.ts index 41c267e2..55de60d6 100644 --- a/src/changedFilesOutput.ts +++ b/src/changedFilesOutput.ts @@ -12,6 +12,13 @@ const getOutputKey = (key: string, outputPrefix: string): string => { return outputPrefix ? `${outputPrefix}_${key}` : key } +const getArrayFromPaths = ( + paths: string | string[], + inputs: Inputs +): string[] => { + return Array.isArray(paths) ? paths : paths.split(inputs.separator) +} + export const setChangedFilesOutput = async ({ allDiffFiles, allFilteredDiffFiles, @@ -34,12 +41,16 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('added_files', outputPrefix), value: addedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('added_files_count', outputPrefix), value: addedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const copiedFiles = await getChangeTypeFiles({ @@ -51,13 +62,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('copied_files', outputPrefix), value: copiedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('copied_files_count', outputPrefix), value: copiedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const modifiedFiles = await getChangeTypeFiles({ @@ -69,13 +84,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('modified_files', outputPrefix), value: modifiedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('modified_files_count', outputPrefix), value: modifiedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const renamedFiles = await getChangeTypeFiles({ @@ -87,13 +106,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('renamed_files', outputPrefix), value: renamedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('renamed_files_count', outputPrefix), value: renamedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const typeChangedFiles = await getChangeTypeFiles({ @@ -105,13 +128,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('type_changed_files', outputPrefix), value: typeChangedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('type_changed_files_count', outputPrefix), value: typeChangedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const unmergedFiles = await getChangeTypeFiles({ @@ -123,13 +150,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('unmerged_files', outputPrefix), value: unmergedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('unmerged_files_count', outputPrefix), value: unmergedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const unknownFiles = await getChangeTypeFiles({ @@ -141,13 +172,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('unknown_files', outputPrefix), value: unknownFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('unknown_files_count', outputPrefix), value: unknownFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const allChangedAndModifiedFiles = await getAllChangeTypeFiles({ @@ -162,13 +197,17 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('all_changed_and_modified_files', outputPrefix), value: allChangedAndModifiedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('all_changed_and_modified_files_count', outputPrefix), value: allChangedAndModifiedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const allChangedFiles = await getChangeTypeFiles({ @@ -185,19 +224,25 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('all_changed_files', outputPrefix), value: allChangedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('all_changed_files_count', outputPrefix), value: allChangedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) await setOutput({ key: getOutputKey('any_changed', outputPrefix), value: allChangedFiles.paths.length > 0 && filePatterns.length > 0, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) const allOtherChangedFiles = await getChangeTypeFiles({ @@ -212,12 +257,18 @@ export const setChangedFilesOutput = async ({ }) core.debug(`All other changed files: ${JSON.stringify(allOtherChangedFiles)}`) - const otherChangedFiles = allOtherChangedFiles.paths - .split(inputs.separator) - .filter( - (filePath: string) => - !allChangedFiles.paths.split(inputs.separator).includes(filePath) - ) + const allOtherChangedFilesPaths: string[] = getArrayFromPaths( + allOtherChangedFiles.paths, + inputs + ) + const allChangedFilesPaths: string[] = getArrayFromPaths( + allChangedFiles.paths, + inputs + ) + + const otherChangedFiles = allOtherChangedFilesPaths.filter( + (filePath: string) => !allChangedFilesPaths.includes(filePath) + ) const onlyChanged = otherChangedFiles.length === 0 && @@ -227,19 +278,27 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('only_changed', outputPrefix), value: onlyChanged, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) await setOutput({ key: getOutputKey('other_changed_files', outputPrefix), - value: otherChangedFiles.join(inputs.separator), - inputs + value: inputs.json + ? otherChangedFiles + : otherChangedFiles.join(inputs.separator), + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('other_changed_files_count', outputPrefix), value: otherChangedFiles.length.toString(), - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const allModifiedFiles = await getChangeTypeFiles({ @@ -257,19 +316,25 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('all_modified_files', outputPrefix), value: allModifiedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('all_modified_files_count', outputPrefix), value: allModifiedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) await setOutput({ key: getOutputKey('any_modified', outputPrefix), value: allModifiedFiles.paths.length > 0 && filePatterns.length > 0, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) const allOtherModifiedFiles = await getChangeTypeFiles({ @@ -284,12 +349,19 @@ export const setChangedFilesOutput = async ({ ] }) - const otherModifiedFiles = allOtherModifiedFiles.paths - .split(inputs.separator) - .filter( - (filePath: string) => - !allModifiedFiles.paths.split(inputs.separator).includes(filePath) - ) + const allOtherModifiedFilesPaths: string[] = getArrayFromPaths( + allOtherModifiedFiles.paths, + inputs + ) + + const allModifiedFilesPaths: string[] = getArrayFromPaths( + allModifiedFiles.paths, + inputs + ) + + const otherModifiedFiles = allOtherModifiedFilesPaths.filter( + (filePath: string) => !allModifiedFilesPaths.includes(filePath) + ) const onlyModified = otherModifiedFiles.length === 0 && @@ -299,19 +371,27 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('only_modified', outputPrefix), value: onlyModified, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) await setOutput({ key: getOutputKey('other_modified_files', outputPrefix), - value: otherModifiedFiles.join(inputs.separator), - inputs + value: inputs.json + ? otherModifiedFiles + : otherModifiedFiles.join(inputs.separator), + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('other_modified_files_count', outputPrefix), value: otherModifiedFiles.length.toString(), - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) const deletedFiles = await getChangeTypeFiles({ @@ -323,19 +403,25 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('deleted_files', outputPrefix), value: deletedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('deleted_files_count', outputPrefix), value: deletedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) await setOutput({ key: getOutputKey('any_deleted', outputPrefix), value: deletedFiles.paths.length > 0 && filePatterns.length > 0, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) const allOtherDeletedFiles = await getChangeTypeFiles({ @@ -344,11 +430,19 @@ export const setChangedFilesOutput = async ({ changeTypes: [ChangeTypeEnum.Deleted] }) - const otherDeletedFiles = allOtherDeletedFiles.paths - .split(inputs.separator) - .filter( - filePath => !deletedFiles.paths.split(inputs.separator).includes(filePath) - ) + const allOtherDeletedFilesPaths: string[] = getArrayFromPaths( + allOtherDeletedFiles.paths, + inputs + ) + + const deletedFilesPaths: string[] = getArrayFromPaths( + deletedFiles.paths, + inputs + ) + + const otherDeletedFiles = allOtherDeletedFilesPaths.filter( + filePath => !deletedFilesPaths.includes(filePath) + ) const onlyDeleted = otherDeletedFiles.length === 0 && @@ -358,18 +452,26 @@ export const setChangedFilesOutput = async ({ await setOutput({ key: getOutputKey('only_deleted', outputPrefix), value: onlyDeleted, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) await setOutput({ key: getOutputKey('other_deleted_files', outputPrefix), - value: otherDeletedFiles.join(inputs.separator), - inputs + value: inputs.json + ? otherDeletedFiles + : otherDeletedFiles.join(inputs.separator), + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json, + shouldEscape: inputs.escapeJson }) await setOutput({ key: getOutputKey('other_deleted_files_count', outputPrefix), value: otherDeletedFiles.length.toString(), - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir }) } diff --git a/src/main.ts b/src/main.ts index a22a9c60..634ee278 100644 --- a/src/main.ts +++ b/src/main.ts @@ -236,12 +236,16 @@ const getChangedFilesFromLocalGit = async ({ await setOutput({ key: 'all_old_new_renamed_files', value: allOldNewRenamedFiles.paths, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) await setOutput({ key: 'all_old_new_renamed_files_count', value: allOldNewRenamedFiles.count, - inputs + writeOutputFiles: inputs.writeOutputFiles, + outputDir: inputs.outputDir, + json: inputs.json }) core.info('All Done!') core.endGroup() diff --git a/src/utils.ts b/src/utils.ts index 7a07a1c2..a09d18e3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -892,7 +892,7 @@ export const jsonOutput = ({ value, shouldEscape }: { - value: string | string[] + value: string | string[] | boolean shouldEscape: boolean }): string => { const result = JSON.stringify(value) @@ -1226,18 +1226,29 @@ export const getRecoverFilePatterns = ({ export const setOutput = async ({ key, value, - inputs + writeOutputFiles, + outputDir, + json = false, + shouldEscape = false }: { key: string - value: string | boolean - inputs: Inputs + value: string | string[] | boolean + writeOutputFiles: boolean + outputDir: string + json?: boolean + shouldEscape?: boolean }): Promise => { - const cleanedValue = value.toString().trim() + let cleanedValue + if (json) { + cleanedValue = jsonOutput({value, shouldEscape}) + } else { + cleanedValue = value.toString().trim() + } + core.setOutput(key, cleanedValue) - if (inputs.writeOutputFiles) { - const outputDir = inputs.outputDir - const extension = inputs.json ? 'json' : 'txt' + if (writeOutputFiles) { + const extension = json ? 'json' : 'txt' const outputFilePath = path.join(outputDir, `${key}.${extension}`) if (!(await exists(outputDir))) {