From 3ef3d14e6dc10488d92a04ca1f01d33c506acacd Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 4 Dec 2021 06:11:27 -0500 Subject: [PATCH] Improve error handling (#263) * 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 --- .github/workflows/test.yml | 63 +++++++++++++++++++++++++++++++++++--- action.yml | 2 +- entrypoint.sh | 24 ++++++++++----- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index efb3b783..74ad784e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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 - continue-on-error: true 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 diff --git a/action.yml b/action.yml index 119859da..556be076 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/entrypoint.sh b/entrypoint.sh index 59e700af..cb1f0c15 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -e +set -eu echo "::group::changed-files" @@ -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 @@ -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 @@ -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 @@ -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}') @@ -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}')