From 5e2a2f192377df7d67537b0e788e1b53e8a76f12 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 5 Sep 2021 08:22:47 -0400 Subject: [PATCH] Added support for detecting deleted files. (#188) --- .github/workflows/test.yml | 71 +++++++++++++++++++++++++++++++++++--- README.md | 17 ++++++++- action.yml | 11 +++++- entrypoint.sh | 40 ++++++++++++++++----- 4 files changed, 125 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5843ea9d..b484baa3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -172,7 +172,7 @@ jobs: echo '${{ toJSON(steps.changed-files-specific.outputs) }}' shell: bash - - name: Verify any_changed files + - name: Verify any_changed for specific files if: "!contains(steps.changed-files-specific.outputs.all_modified_files, 'action.yml') && !contains(steps.changed-files-specific.outputs.all_modified_files, '.github/workflows/test.yml')" run: | if [[ "${{ steps.changed-files-specific.outputs.any_changed }}" != "false" ]]; then @@ -181,6 +181,15 @@ jobs: fi shell: bash + - name: Verify any_deleted for specific files + if: "!contains(steps.changed-files-specific.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific.outputs.deleted_files, '.github/workflows/test.yml')" + run: | + if [[ "${{ steps.changed-files-specific.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific.outputs.any_deleted }})" + exit 1 + fi + shell: + bash - name: Run changed-files with specific files comma check duplicates id: changed-files-specific-duplicate-output uses: ./ @@ -222,6 +231,15 @@ jobs: fi shell: bash + - name: Verify any_deleted files comma separator + if: "!contains(steps.changed-files-specific-comma.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific-comma.outputs.deleted_files, '.github/workflows/test.yml')" + run: | + if [[ "${{ steps.changed-files-specific-comma.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-comma.outputs.any_deleted }})" + exit 1 + fi + shell: + bash - name: Run changed-files with specific files pipe separator id: changed-files-specific-pipe uses: ./ @@ -244,6 +262,15 @@ jobs: fi shell: bash + - name: Verify any_deleted files comma separator + if: "!contains(steps.changed-files-specific-pipe.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific-pipe.outputs.deleted_files, '.github/workflows/test.yml')" + run: | + if [[ "${{ steps.changed-files-specific-pipe.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-pipe.outputs.any_deleted }})" + exit 1 + fi + shell: + bash - name: Run changed-files with specific files forward slash separator id: changed-files-specific-forward-slash uses: ./ @@ -266,6 +293,15 @@ jobs: fi shell: bash + - name: Verify any_deleted files comma separator + if: "!contains(steps.changed-files-specific-forward-slash.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific-forward-slash.outputs.deleted_files, '.github/workflows/test.yml')" + run: | + if [[ "${{ steps.changed-files-specific.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-forward-slash.outputs.any_deleted }})" + exit 1 + fi + shell: + bash - name: Run changed-files with specific files from a source file id: changed-files-specific-source-file uses: ./ @@ -275,7 +311,7 @@ jobs: test/changed-files-list.txt files: | .github/workflows/rebase.yml - - name: Verify any_changed files + - name: Verify any_changed from source files if: "!contains(steps.changed-files-specific-source-file.outputs.all_modified_files, 'action.yml') && !contains(steps.changed-files-specific-source-file.outputs.all_modified_files, '.github/workflows/test.yml')" run: | if [[ "${{ steps.changed-files-specific-source-file.outputs.any_changed }}" != "false" ]]; then @@ -284,6 +320,15 @@ jobs: fi shell: bash + - name: Verify any_deleted from source files + if: "!contains(steps.changed-files-specific-source-file.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific-source-file.outputs.deleted_files, '.github/workflows/test.yml')" + run: | + if [[ "${{ steps.changed-files-specific-source-file.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-source-file.outputs.any_deleted }})" + exit 1 + fi + shell: + bash - name: Show output run: | echo '${{ toJSON(steps.changed-files-specific-source-file.outputs) }}' @@ -296,6 +341,11 @@ jobs: files_from_source_file: | test/changed-files-list.txt separator: "," + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-specific-comma-source-file.outputs) }}' + shell: + bash - name: Verify any_changed files comma separator if: "!contains(steps.changed-files-specific-comma-source-file.outputs.all_modified_files, 'action.yml') && !contains(steps.changed-files-specific-comma-source-file.outputs.all_modified_files, '.github/workflows/test.yml')" run: | @@ -305,9 +355,13 @@ jobs: fi shell: bash - - name: Show output + - name: Verify any_deleted files with comma separator + if: "!contains(steps.changed-files-specific-comma-source-file.outputs.deleted_files, 'action.yml') && !contains(steps.changed-files-specific-comma-source-file.outputs.deleted_files, '.github/workflows/test.yml')" run: | - echo '${{ toJSON(steps.changed-files-specific-comma-source-file.outputs) }}' + if [[ "${{ steps.changed-files-specific-comma-source-file.outputs.any_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-comma-source-file.outputs.any_deleted }})" + exit 1 + fi shell: bash - name: Run changed-files with custom sha @@ -354,6 +408,15 @@ jobs: fi shell: bash + - name: Verify only_deleted files + if: steps.changed-files-specific-only-changed.outputs.other_deleted_files != '' + run: | + if [[ "${{ steps.changed-files-specific-only-changed.outputs.only_deleted }}" != "false" ]]; then + echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific-only-changed.outputs.only_deleted }})" + exit 1 + fi + shell: + bash - name: Show output run: | echo '${{ toJSON(steps.changed-files-specific-only-changed.outputs) }}' diff --git a/README.md b/README.md index a2779f3c..dc8cd010 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ jobs: |:--------------------:|:------------:|:----------------------------------:|:----------------------------------------:| | any_changed | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has changed.
i.e. *using a combination of all added,
copied, modified and renamed files (ACMR).* | | only_changed | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input have changed. | +| any_deleted | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has been deleted. | +| only_deleted | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has been deleted. | | other_changed_files | `string` | `'new.txt path/to/file.png ...'` | Select all other changed files
not listed in the files input
i.e. *a combination of all added,
copied and modified files (ACMR).* | | all_modified_files | `string` | `'new.txt path/to/file.png ...'` | Select all modified files
i.e. *a combination of all added,
copied, modified and renamed files (ACMR).* | | all_changed_and_modified_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed
and modified files
i.e. *a combination of (ACMRDTUX).* | @@ -164,6 +166,20 @@ jobs: run: | echo "Only files listed above have changed." + - name: Run step if any of the listed files above is deleted + if: steps.changed-files.outputs.any_deleted == "true" + run: | + for file in "${{ steps.changed-files.outputs.deleted_files }}"; do + echo "$file was deleted" + done + + - name: Run step if all listed files above have been deleted + if: steps.changed-files.outputs.only_deleted == "true" + run: | + for file in "${{ steps.changed-files.outputs.deleted_files }}"; do + echo "$file was deleted" + done + - name: Use a source file or list of file(s) to populate to files input. id: changed-files-specific-source-file uses: tj-actions/changed-files@v1.1.0 @@ -203,7 +219,6 @@ jobs: uses: tj-actions/changed-files@v1.1.0 with: path: subfolder - ``` ### Running [pre-commit](https://pre-commit.com/) on all modified files diff --git a/action.yml b/action.yml index dcabfa3c..59c2a7fe 100644 --- a/action.yml +++ b/action.yml @@ -64,11 +64,20 @@ outputs: description: Return true only when any files provided using the files input have changed. value: ${{ steps.changed-files.outputs.any_changed }} only_changed: - description: Return true only when only files provided using the files input have changed. + description: Return true when all files provided using the files input have changed. value: ${{ steps.changed-files.outputs.only_changed }} other_changed_files: description: Return list of changed files not listed in the files input. value: ${{ steps.changed-files.outputs.other_changed_files }} + any_deleted: + description: Return true only when any files provided using the files input have been deleted. + value: ${{ steps.changed-files.outputs.any_deleted }} + only_deleted: + description: Return true when all files provided using the files input have been deleted. + value: ${{ steps.changed-files.outputs.only_deleted }} + other_deleted_files: + description: Return list of deleted files not listed in the files input. + value: ${{ steps.changed-files.outputs.other_deleted_files }} runs: using: 'composite' diff --git a/entrypoint.sh b/entrypoint.sh index afc73d2a..06ac2fb6 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -125,11 +125,12 @@ else ALL_CHANGED_AND_MODIFIED=$(echo "${ALL_CHANGED_ARRAY[*]}" | tr " " "\n" | sort -u | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') ALL_MODIFIED_FILES=$(echo "${ALL_MODIFIED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') - ALL_OTHER_CHANGED_FILES=$(git diff --diff-filter="ACMR" --name-only "$PREVIOUS_SHA" "$CURRENT_SHA") - IFS=" " read -r -a UNIQUE_ALL_MODIFIED_FILES_ARRAY <<< "$(echo "${ALL_MODIFIED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | tr "\n" " ")" - IFS=" " read -r -a OTHER_CHANGED_FILES_ARRAY <<< "$(echo "${ALL_OTHER_CHANGED_FILES[@]}" "${UNIQUE_ALL_MODIFIED_FILES[@]}" | tr " " "\n" | sort | uniq -u | tr "\n" " ")" + ALL_OTHER_MODIFIED_FILES=$(git diff --diff-filter="ACMR" --name-only "$PREVIOUS_SHA" "$CURRENT_SHA") - OTHER_CHANGED_FILES=$(echo "${OTHER_CHANGED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') + IFS=" " read -r -a UNIQUE_ALL_MODIFIED_FILES_ARRAY <<< "$(echo "${ALL_MODIFIED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | tr "\n" " ")" + IFS=" " read -r -a OTHER_MODIFIED_FILES_ARRAY <<< "$(echo "${ALL_OTHER_MODIFIED_FILES[@]}" "${UNIQUE_ALL_MODIFIED_FILES_ARRAY[@]}" | tr " " "\n" | sort | uniq -u | tr "\n" " ")" + + OTHER_MODIFIED_FILES=$(echo "${OTHER_MODIFIED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') echo "Matching modified files: ${UNIQUE_ALL_MODIFIED_FILES_ARRAY[*]}" @@ -139,13 +140,36 @@ else echo "::set-output name=any_changed::false" fi - if [[ -n "${OTHER_CHANGED_FILES_ARRAY[*]}" ]]; then - echo "Non Matching modified files: ${OTHER_CHANGED_FILES_ARRAY[*]}" + if [[ -n "${OTHER_MODIFIED_FILES_ARRAY[*]}" ]]; then + echo "Non Matching modified files: ${OTHER_MODIFIED_FILES_ARRAY[*]}" echo "::set-output name=only_changed::false" - echo "::set-output name=other_changed_files::$OTHER_CHANGED_FILES" - else + echo "::set-output name=other_changed_files::$OTHER_MODIFIED_FILES" + elif [[ -n "${UNIQUE_ALL_MODIFIED_FILES_ARRAY[*]}" ]]; then echo "::set-output name=only_changed::true" fi + + OTHER_DELETED_FILES=$(git diff --diff-filter=D --name-only "$PREVIOUS_SHA" "$CURRENT_SHA") + + IFS=" " read -r -a UNIQUE_DELETED_FILES_ARRAY <<< "$(echo "${DELETED_ARRAY[*]}" | tr " " "\n" | sort -u | tr "\n" " ")" + IFS=" " read -r -a OTHER_DELETED_FILES_ARRAY <<< "$(echo "${OTHER_DELETED_FILES[@]}" "${UNIQUE_DELETED_FILES_ARRAY[@]}" | tr " " "\n" | sort | uniq -u | tr "\n" " ")" + + OTHER_DELETED_FILES=$(echo "${OTHER_DELETED_FILES_ARRAY[*]}" | tr " " "\n" | sort -u | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') + + echo "Matching modified files: ${UNIQUE_DELETED_FILES_ARRAY[*]}" + + if [[ -n "${UNIQUE_DELETED_FILES_ARRAY[*]}" ]]; then + echo "::set-output name=any_deleted::true" + else + echo "::set-output name=any_deleted::false" + fi + + if [[ -n "${OTHER_DELETED_FILES_ARRAY[*]}" ]]; then + echo "Non Matching modified files: ${OTHER_DELETED_FILES_ARRAY[*]}" + echo "::set-output name=only_deleted::false" + echo "::set-output name=other_deleted_files::$OTHER_DELETED_FILES" + elif [[ -n "${UNIQUE_DELETED_FILES_ARRAY[*]}" ]]; then + echo "::set-output name=only_deleted::true" + fi fi echo "Added files: $ADDED"