Merge pull request #31 from tj-actions/add-support-for-files

This commit is contained in:
Tonye Jack 2021-05-01 12:11:02 -04:00 committed by GitHub
commit fcb2ab8c32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 23 deletions

View File

@ -40,3 +40,24 @@ jobs:
- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-comma.outputs) }}"
- name: Run changed-files with specific files
id: changed-files-specific
uses: ./
with:
files: |
action.yml
.github/workflows/test.yml
- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific.outputs) }}"
- name: Run changed-files with specific files comma separator
id: changed-files-specific-comma
uses: ./
with:
files: |
action.yml
.github/workflows/test.yml
separator: ","
- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific-comma.outputs) }}"

View File

@ -25,6 +25,7 @@ Using the default separator.
| Output | type | example | description |
|:-------------------:|:------------:|:------------------------------:|:----------------------------------------:|
| all_modified_files | `string` | 'new.txt other.png ...' | Select all modified files <br /> *i.e a combination of all added, <br />copied and modified files (ACM).* |
| has_changed | `string` | `true OR false` | Returns `true` only when the filenames provided using `files` input have all changed |
| all_changed_files | `string` | 'new.txt other.png ...' | Select all paths (*) <br /> *i.e a combination of all options below.* |
| added_files | `string` | 'new.txt other.png ...' | Select only files that are Added (A) |
| copied_files | `string` | 'new.txt other.png ...' | Select only files that are Copied (C) |
@ -41,6 +42,7 @@ Using the default separator.
| Input | type | required | default | description |
|:-------------:|:-----------:|:-------------:|:----------------------------:|:-------------:|
| separator | `string` | `true` | `' '` | Separator to return outputs |
| files | `string OR string[]` | `false` | | Restricted list of specific files to watch for changes |
## Usage

View File

@ -6,6 +6,10 @@ inputs:
description: 'Split character for array output'
required: true
default: " "
files:
description: 'Check for file changes for all files listed (Defaults to the entire repo)'
required: false
default: ""
outputs:
added_files:
@ -38,6 +42,9 @@ outputs:
all_modified_files:
description: List of all copied modified and added files
value: ${{ steps.changed_files.outputs.all_modified_files }}
has_changed:
description: Return true only when all files provided using the files input have all changed.
value: ${{ steps.changed_files.outputs.has_changed }}
runs:
using: 'composite'
@ -54,7 +61,12 @@ runs:
HEAD_SHA=$(git rev-parse ${TARGET_BRANCH} || true)
fi
INPUT_FILES="${{ inputs.files }}"
if [[ -z "$INPUT_FILES" ]]; then
echo "Getting diff..."
ADDED=$(git diff --diff-filter=A --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
COPIED=$(git diff --diff-filter=C --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
DELETED=$(git diff --diff-filter=D --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
@ -66,6 +78,66 @@ runs:
ALL_CHANGED=$(git diff --diff-filter='*ACDMRTUX' --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
ALL_MODIFIED_FILES=$(git diff --diff-filter='ACM' --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
echo "::set-output name=added_files::$ADDED"
echo "::set-output name=copied_files::$COPIED"
echo "::set-output name=deleted_files::$DELETED"
echo "::set-output name=modified_files::$MODIFIED"
echo "::set-output name=renamed_files::$RENAMED"
echo "::set-output name=changed_files::$CHANGED"
echo "::set-output name=unmerged_files::$UNMERGED"
echo "::set-output name=unknown_files::$UNKNOWN"
echo "::set-output name=all_changed_files::$ALL_CHANGED"
echo "::set-output name=all_modified_files::$ALL_MODIFIED_FILES"
else
ADDED=()
COPIED=()
DELETED=()
MODIFIED=()
RENAMED=()
CHANGED=()
UNMERGED=()
UNKNOWN=()
ALL_CHANGED=()
ALL_MODIFIED_FILES=()
for path in ${INPUT_FILES}
do
echo "Checking for file changes: \"${path}\"..."
ADDED+=$(git diff --diff-filter=A --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
COPIED+=$(git diff --diff-filter=C --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
DELETED+=$(git diff --diff-filter=D --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
MODIFIED+=$(git diff --diff-filter=M --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
RENAMED+=$(git diff --diff-filter=R --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
CHANGED+=$(git diff --diff-filter=T --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
UNMERGED+=$(git diff --diff-filter=U --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
UNKNOWN+=$(git diff --diff-filter=X --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
ALL_CHANGED+=$(git diff --diff-filter='*ACDMRTUX' --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
ALL_MODIFIED_FILES+=$(git diff --diff-filter='ACM' --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
done
ADDED=$(echo "$ADDED" | sed -E 's/(${{ inputs.separator }})$//')
COPIED=$(echo $COPIED | sed -E 's/(${{ inputs.separator }})$//')
DELETED=$(echo "$DELETED" | sed -E 's/(${{ inputs.separator }})$//')
MODIFIED=$(echo "$MODIFIED" | sed -E 's/(${{ inputs.separator }})$//')
RENAMED=$(echo "$RENAMED" | sed -E 's/(${{ inputs.separator }})$//')
CHANGED=$(echo "$CHANGED" | sed -E 's/(${{ inputs.separator }})$//')
UNMERGED=$(echo "$UNMERGED" | sed -E 's/(${{ inputs.separator }})$//')
UNKNOWN=$(echo "$UNKNOWN" | sed -E 's/(${{ inputs.separator }})$//')
ALL_CHANGED=$(echo "$ALL_CHANGED" | sed -E 's/(${{ inputs.separator }})$//')
ALL_MODIFIED_FILES=$(echo "$ALL_MODIFIED_FILES" | sed -E 's/(${{ inputs.separator }})$//')
OUTPUT_ALL_MODIFIED_FILES=$(echo $ALL_MODIFIED_FILES | sed "s/${{ inputs.separator }}/ /g")
ALL_INPUT_FILES=$(echo $INPUT_FILES | sed "s/\n/ /g")
IFS=$'\n' SORTED_INPUT_FILES=($(sort <<<"${ALL_INPUT_FILES[*]}"))
IFS=$'\n' SORTED_OUTPUT_ALL_MODIFIED_FILES=($(sort <<<"${OUTPUT_ALL_MODIFIED_FILES[*]}"))
if [[ "${SORTED_INPUT_FILES[*]}" == "${SORTED_OUTPUT_ALL_MODIFIED_FILES[*]}" ]]; then
echo "::set-output name=has_changed::true"
else
echo "::set-output name=has_changed::false"
fi
echo "::set-output name=added_files::$ADDED"
echo "::set-output name=copied_files::$COPIED"
@ -77,6 +149,7 @@ runs:
echo "::set-output name=unknown_files::$UNKNOWN"
echo "::set-output name=all_changed_files::$ALL_CHANGED"
echo "::set-output name=all_modified_files::$ALL_MODIFIED_FILES"
fi
shell: bash
branding: