Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for retrieving the files input using a source file. #132

Merged
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ jobs:
fi
shell:
bash
- name: Run changed-files with specific files from a source file
id: changed-files-specific-source-file
uses: ./
with:
files-from-source-file: |
test/changed-files-list.txt
test/changed-files-list.txt
files: |
.github/workflows/rebase.yml
- name: Verify any_changed 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
echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific.outputs.any_changed }})"
exit 1
fi
shell:
bash
- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific.outputs) }}"
shell:
bash
- name: Run changed-files with specific files from a source file using a comma separator
id: changed-files-specific-comma-source-file
uses: ./
with:
files-from-source-file: |
test/changed-files-list.txt
separator: ","
- name: Verify any_changed files comma separator
if: "!contains(steps.changed-files-specific-comma.outputs.all_modified_files, 'action.yml') && !contains(steps.changed-files-specific-comma.outputs.all_modified_files, '.github/workflows/test.yml')"
run: |
if [[ "${{ steps.changed-files-specific.outputs.any_changed }}" != "false" ]]; then
echo "Invalid output: Expected (false) got (${{ steps.changed-files-specific.outputs.any_changed }})"
exit 1
fi
shell:
bash
- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific-comma.outputs) }}"
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
| separator | `string` | `true` | `' '` | Output string separator |
| files | `string` OR `string[]` | `false` | | Check for changes <br> using only these list of file(s) <br> (Defaults to the entire repo) |
| sha | `string` | `true` | `${{ github.sha }}` | Specify a different <br> commit SHA used for comparing changes |
| files-from-source-file | `string` | `false` | | Source file used populate <br> the files input. |

## Example

Expand Down Expand Up @@ -150,6 +151,22 @@ jobs:
if: steps.changed-files-specific.outputs.any_changed == 'true'
run: |
echo "One or more files listed above has changed."

- name: Use a source file or list of file(s) to populate to files input.
id: changed-files-specific-source-file
uses: ./
with:
files-from-source-file: |
test/changed-files-list.txt

- name: Use a source file or list of file(s) to populate to files input and optionally specify more files.
id: changed-files-specific-source-file-and-specify-files
uses: ./
with:
files-from-source-file: |
test/changed-files-list.txt
files: |
.github/workflows/rebase.yml

- name: Use a different commit SHA
id: changed-files-comma
Expand Down
22 changes: 21 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ inputs:
description: 'Split character for array output'
required: true
default: " "
files-from-source-file:
description: 'Source file to populate the files input'
required: false
default: ""
files:
description: 'Check for changes using only this list of files (Defaults to the entire repo)'
required: false
Expand Down Expand Up @@ -57,6 +61,22 @@ outputs:
runs:
using: 'composite'
steps:
- run: |
FILES=()

if [[ -n $INPUT_FILES_FROM_SOURCE_FILE ]]; then
for file in $INPUT_FILES_FROM_SOURCE_FILE
do
FILES+=$(cat $file | sort -u | tr "\n" " " )
done
fi

echo "::set-output name=files::$FILES"
id: source-input-files
shell: bash
env:
INPUT_FILES: ${{ inputs.files }}
INPUT_FILES_FROM_SOURCE_FILE: ${{ inputs.files-from-source-file }}
- run: |
bash $GITHUB_ACTION_PATH/entrypoint.sh
id: changed-files
Expand All @@ -68,7 +88,7 @@ runs:
# https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611
INPUT_SHA: ${{ inputs.sha }}
INPUT_TOKEN: ${{ inputs.token }}
INPUT_FILES: ${{ inputs.files }}
INPUT_FILES: ${{ join(format('{0} {1}', inputs.files, steps.source-input-files.outputs.files), ' ') }}
INPUT_SEPARATOR: ${{ inputs.separator }}

branding:
Expand Down
11 changes: 7 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ fi

echo "Retrieving changes between $PREV_SHA ($TARGET_BRANCH) → $CURR_SHA ($CURRENT_BRANCH)"

if [[ -z "$INPUT_FILES" ]]; then
UNIQUE_FILES=$(echo "$INPUT_FILES" | tr ' ' '\n' | sort -u | xargs)

if [[ -z "$UNIQUE_FILES" ]]; then
echo "Getting diff..."
ADDED=$(git diff --diff-filter=A --name-only "$PREV_SHA" "$CURR_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
COPIED=$(git diff --diff-filter=C --name-only "$PREV_SHA" "$CURR_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
Expand All @@ -66,7 +68,8 @@ else
UNKNOWN_ARRAY=()
ALL_CHANGED_ARRAY=()
ALL_MODIFIED_FILES_ARRAY=()
for path in ${INPUT_FILES}

for path in ${UNIQUE_FILES}
do
echo "Checking for file changes: \"${path}\"..."
IFS=" "
Expand Down Expand Up @@ -125,9 +128,9 @@ echo "Unknown files: $UNKNOWN"
echo "All changed files: $ALL_CHANGED"
echo "All modified files: $ALL_MODIFIED_FILES"

if [[ -n "$INPUT_FILES" ]]; then
if [[ -n "$UNIQUE_FILES" ]]; then
# shellcheck disable=SC2001
ALL_INPUT_FILES=$(echo "$INPUT_FILES" | tr "\n" " " | xargs)
ALL_INPUT_FILES=$(echo "$UNIQUE_FILES" | tr "\n" " " | xargs)

echo "Input files: ${ALL_INPUT_FILES[*]}"
echo "Matching modified files: ${ALL_MODIFIED_FILES[*]}"
Expand Down
4 changes: 4 additions & 0 deletions test/changed-files-list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.github/workflows/test.yml
action.yml
action.yml
test/changed-files-list.txt