Skip to content

Commit

Permalink
Improve error handling (#263)
Browse files Browse the repository at this point in the history
* Improve error handling

Fixes: #255

* Update entrypoint.sh

* Update entrypoint.sh

* Update error checking

* Update test.yml

* Update test.yml

* Update test.yml

* Update entrypoint.sh

* Update action.yml

* Update entrypoint.sh

* Update entrypoint.sh

* Update entrypoint.sh

* Update test.yml

* Update entrypoint.sh

* Update test.yml

* Update test.yml

* Update test.yml

* Update entrypoint.sh

* Update entrypoint.sh

* Update entrypoint.sh

* Update test.yml
  • Loading branch information
jackton1 committed Dec 4, 2021
1 parent 0a8c804 commit 3ef3d14
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
61 changes: 58 additions & 3 deletions .github/workflows/test.yml
Expand Up @@ -73,8 +73,8 @@ jobs:
shell:
bash

test-no-head-sha:
name: Test changed-files missing head sha
test-non-existent-base-sha:
name: Test changed-files non existent base sha
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
Expand All @@ -85,16 +85,71 @@ jobs:
- name: Checkout to branch
uses: actions/checkout@v2

- name: Run changed-files with defaults
- name: Run changed-files with non existent base sha
id: changed-files
uses: ./
continue-on-error: true
with:
base_sha: "4554456"

- name: Show output
run: |
echo "${{ toJSON(steps.changed-files.outputs) }}"
shell:
bash

- name: Run changed-files-specific with non existent base sha
id: changed-files-specific
uses: ./
continue-on-error: true
with:
files: action.yml
base_sha: "4554456"

- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific.outputs) }}"
shell:
bash

test-non-existent-sha:
name: Test changed-files non existent sha
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022, windows-2016]

steps:
- name: Checkout to branch
uses: actions/checkout@v2

- name: Run changed-files with non existent sha
id: changed-files
uses: ./
continue-on-error: true
with:
base_sha: "4554456"

- name: Show output
run: |
echo "${{ toJSON(steps.changed-files.outputs) }}"
shell:
bash

- name: Run changed-files-specific with non existent sha
id: changed-files-specific
uses: ./
continue-on-error: true
with:
files: action.yml
base_sha: "4554456"

- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-specific.outputs) }}"
shell:
bash

test:
name: Test changed-files
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Expand Up @@ -95,7 +95,7 @@ runs:
INPUT_FILES: ${{ inputs.files }}
INPUT_FILES_FROM_SOURCE_FILE: ${{ inputs.files_from_source_file }}
- run: |
# "Set up the bash sha..."
# "Set base sha..."
if [[ -n "${{ inputs.base_sha }}" ]]; then
echo "::set-output name=base_sha::${{ inputs.base_sha }}"
elif [[ "${{ inputs.since_last_remote_commit }}" == "true" ]]; then
Expand Down
24 changes: 16 additions & 8 deletions entrypoint.sh
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -eu

echo "::group::changed-files"

Expand Down Expand Up @@ -37,8 +37,12 @@ else
CURRENT_SHA=$INPUT_SHA && exit_status=$? || exit_status=$?
fi

git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?


if [[ $exit_status -ne 0 ]]; then
echo "::warning::Unable to determine the current head sha"
echo "::warning::Unable to locate the current sha: $CURRENT_SHA"
echo "::warning::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
git remote remove temp_changed_files
exit 1
fi
Expand All @@ -52,9 +56,11 @@ if [[ -z $GITHUB_BASE_REF ]]; then
else
PREVIOUS_SHA=$INPUT_BASE_SHA && exit_status=$? || exit_status=$?
fi

git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?

if [[ $exit_status -ne 0 ]]; then
echo "::warning::Unable to determine the previous commit sha"
echo "::warning::Unable to locate the previous sha: $PREVIOUS_SHA"
echo "::warning::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
git remote remove temp_changed_files
exit 1
Expand All @@ -64,15 +70,17 @@ else
CURRENT_BRANCH=$GITHUB_HEAD_REF

if [[ -z $INPUT_BASE_SHA ]]; then
git fetch --no-tags -u --progress --depth=1 temp_changed_files "${TARGET_BRANCH}":"${TARGET_BRANCH}"
git fetch --no-tags -u --progress --depth=1 temp_changed_files "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
PREVIOUS_SHA=$(git rev-parse "${TARGET_BRANCH}" 2>&1) && exit_status=$? || exit_status=$?
else
git fetch --no-tags -u --progress --depth=1 temp_changed_files "$INPUT_BASE_SHA"
git fetch --no-tags -u --progress --depth=1 temp_changed_files "$INPUT_BASE_SHA" && exit_status=$? || exit_status=$?
PREVIOUS_SHA=$INPUT_BASE_SHA
fi

git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?

if [[ $exit_status -ne 0 ]]; then
echo "::warning::Unable to determine the base ref sha for ${TARGET_BRANCH}"
echo "::warning::Unable to locate the previous sha: $PREVIOUS_SHA"
echo "::warning::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
git remote remove temp_changed_files
exit 1
Expand All @@ -81,8 +89,9 @@ fi

echo "Retrieving changes between $PREVIOUS_SHA ($TARGET_BRANCH) → $CURRENT_SHA ($CURRENT_BRANCH)"

echo "Getting diff..."

if [[ -z "${INPUT_FILES[*]}" ]]; then
echo "Getting diff..."
ADDED=$(git diff --diff-filter=A --name-only "$PREVIOUS_SHA" "$CURRENT_SHA" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
COPIED=$(git diff --diff-filter=C --name-only "$PREVIOUS_SHA" "$CURRENT_SHA" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
DELETED=$(git diff --diff-filter=D --name-only "$PREVIOUS_SHA" "$CURRENT_SHA" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
Expand All @@ -94,7 +103,6 @@ if [[ -z "${INPUT_FILES[*]}" ]]; then
ALL_CHANGED_AND_MODIFIED=$(git diff --diff-filter="*ACDMRTUX" --name-only "$PREVIOUS_SHA" "$CURRENT_SHA" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
ALL_MODIFIED=$(git diff --diff-filter="ACMR" --name-only "$PREVIOUS_SHA" "$CURRENT_SHA" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
else

echo "Input files: ${INPUT_FILES[*]}"

FILES=$(echo "${INPUT_FILES[*]}" | awk '{gsub(/ /,"\n"); print $0;}' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')
Expand Down

0 comments on commit 3ef3d14

Please sign in to comment.