From 1f907ee3bb9623c6a98b8c9585d2d4cb1fb9d707 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 13:07:33 -0600 Subject: [PATCH 01/10] feat: make since_last_remote the default for push events --- action.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 28a4e1a7cee..70c902b743a 100644 --- a/action.yml +++ b/action.yml @@ -50,10 +50,6 @@ inputs: base_sha: description: "Specify a base commit SHA on used for comparing changes" required: false - since_last_remote_commit: - description: "Use the last commit on the remote branch as the base_sha for push event." - required: false - default: "false" since: description: "Get changed files for commits whose timestamp is older than the given time" required: false @@ -160,7 +156,7 @@ runs: echo "::set-output name=base_sha::$BASE_SHA" elif [[ -n "${{ inputs.base_sha }}" ]]; then echo "::set-output name=base_sha::${{ inputs.base_sha }}" - elif [[ "${{ inputs.since_last_remote_commit }}" == "true" ]]; then + else LAST_REMOTE_COMMIT="${{ github.event.before }}" if [[ -z "$LAST_REMOTE_COMMIT" || "$LAST_REMOTE_COMMIT" == "0000000000000000000000000000000000000000" ]]; then LAST_REMOTE_COMMIT=$(git rev-parse $(git branch -r --sort=-committerdate | head -1)) From 26361016d977cbec6629ee49f763d41da14746f9 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 17:49:47 -0600 Subject: [PATCH 02/10] Fixed test. --- action.yml | 51 +++++++++++++++++++------------------------------ get-base-sha.sh | 34 +++++++++++++++++++++++++++++++++ get-sha.sh | 24 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 get-base-sha.sh create mode 100644 get-sha.sh diff --git a/action.yml b/action.yml index 70c902b743a..00bac98a62d 100644 --- a/action.yml +++ b/action.yml @@ -147,40 +147,31 @@ runs: using: "composite" steps: - run: | - # "Set base sha..." - if [[ -n "${{ inputs.since }}" ]]; then - BASE_SHA=$(git log --format="%H" --date=local --since="${{ inputs.since }}" --reverse | head -n 1) - if [[ -z "$BASE_SHA" ]]; then - echo "::warning::The BASE_SHA for date '${{ inputs.since }}' couldn't be determined." - fi - echo "::set-output name=base_sha::$BASE_SHA" - elif [[ -n "${{ inputs.base_sha }}" ]]; then - echo "::set-output name=base_sha::${{ inputs.base_sha }}" - else - LAST_REMOTE_COMMIT="${{ github.event.before }}" - if [[ -z "$LAST_REMOTE_COMMIT" || "$LAST_REMOTE_COMMIT" == "0000000000000000000000000000000000000000" ]]; then - LAST_REMOTE_COMMIT=$(git rev-parse $(git branch -r --sort=-committerdate | head -1)) - fi - if [[ "${{ inputs.sha }}" == "$LAST_REMOTE_COMMIT" ]]; then - LAST_REMOTE_COMMIT=$(git rev-parse "${{ inputs.sha }}^1") - fi - echo "::set-output name=base_sha::$LAST_REMOTE_COMMIT" - fi + # "Calculate the base sha..." + bash $GITHUB_ACTION_PATH/get-base-sha.sh id: base-sha shell: bash + env: + GITHUB_WORKSPACE: ${{ github.workspace }} + GITHUB_EVENT_BEFORE: ${{ github.event.before }} + # INPUT_ is not available in Composite run steps + # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs + INPUT_SINCE: ${{ inputs.since }} + INPUT_BASE_SHA: ${{ inputs.base_sha }} + INPUT_SHA: ${{ inputs.sha }} + INPUT_PATH: ${{ inputs.path }} - run: | - # "Set the sha..." - if [[ -n "${{ inputs.until }}" ]]; then - SHA=$(git log -1 --format="%H" --date=local --until="${{ inputs.until }}") - if [[ -z "$SHA" ]]; then - echo "::warning::The SHA for date '${{ inputs.until }}' couldn't be determined, falling back to the current sha." - fi - echo "::set-output name=sha::$SHA" - else - echo "::set-output name=sha::${{ inputs.sha }}" - fi + # "Calculate the sha..." + bash $GITHUB_ACTION_PATH/get-sha.sh id: sha shell: bash + env: + GITHUB_WORKSPACE: ${{ github.workspace }} + # INPUT_ is not available in Composite run steps + # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs + INPUT_UNTIL: ${{ inputs.until }} + INPUT_SHA: ${{ inputs.sha }} + INPUT_PATH: ${{ inputs.path }} - run: | # "Calculating the previous and current SHA..." bash $GITHUB_ACTION_PATH/diff-sha.sh @@ -191,7 +182,6 @@ runs: GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_BASE_REF: ${{ github.base_ref }} GITHUB_HEAD_REF: ${{ github.head_ref }} - GITHUB_ACTION_PATH: ${{ github.action_path }} GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} # INPUT_ is not available in Composite run steps @@ -220,7 +210,6 @@ runs: id: changed-files shell: bash env: - GITHUB_ACTION_PATH: ${{ github.action_path }} GITHUB_WORKSPACE: ${{ github.workspace }} # INPUT_ is not available in Composite run steps # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs diff --git a/get-base-sha.sh b/get-base-sha.sh new file mode 100644 index 00000000000..9cd65e77a41 --- /dev/null +++ b/get-base-sha.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +set -eu + +if [[ -n $INPUT_PATH ]]; then + REPO_DIR="$GITHUB_WORKSPACE/$INPUT_PATH" + + echo "Resolving repository path: $REPO_DIR" + if [[ ! -d "$REPO_DIR" ]]; then + echo "::error::Invalid repository path: $REPO_DIR" + exit 1 + fi + cd "$REPO_DIR" +fi + +if [[ -n "$INPUT_SINCE" ]]; then + BASE_SHA=$(git log --format="%H" --date=local --since="$INPUT_SINCE" --reverse | head -n 1) + if [[ -z "$BASE_SHA" ]]; then + echo "::warning::The BASE_SHA for date '$INPUT_SINCE' couldn't be determined." + fi + echo "::set-output name=base_sha::$BASE_SHA" +elif [[ -n "$INPUT_BASE_SHA" ]]; then + echo "::set-output name=base_sha::$INPUT_BASE_SHA" +else + LAST_REMOTE_COMMIT="$GITHUB_EVENT_BEFORE" + + if [[ -z "$LAST_REMOTE_COMMIT" || "$LAST_REMOTE_COMMIT" == "0000000000000000000000000000000000000000" ]]; then + LAST_REMOTE_COMMIT=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1)") + fi + if [[ "$INPUT_SHA" == "$LAST_REMOTE_COMMIT" ]]; then + LAST_REMOTE_COMMIT=$(git rev-parse "$INPUT_SHA^1") + fi + echo "::set-output name=base_sha::$LAST_REMOTE_COMMIT" +fi \ No newline at end of file diff --git a/get-sha.sh b/get-sha.sh new file mode 100644 index 00000000000..e94a9d79ce6 --- /dev/null +++ b/get-sha.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -eu + +if [[ -n $INPUT_PATH ]]; then + REPO_DIR="$GITHUB_WORKSPACE/$INPUT_PATH" + + echo "Resolving repository path: $REPO_DIR" + if [[ ! -d "$REPO_DIR" ]]; then + echo "::error::Invalid repository path: $REPO_DIR" + exit 1 + fi + cd "$REPO_DIR" +fi + +if [[ -n "$INPUT_UNTIL" ]]; then + SHA=$(git log -1 --format="%H" --date=local --until="$INPUT_UNTIL") + if [[ -z "$SHA" ]]; then + echo "::warning::The SHA for date '$INPUT_UNTIL' couldn't be determined, falling back to the current sha." + fi + echo "::set-output name=sha::$SHA" +else + echo "::set-output name=sha::$INPUT_SHA" +fi \ No newline at end of file From 068970bff87f5381f963ac40771d21b5c38cd46d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 18:12:07 -0600 Subject: [PATCH 03/10] reorder inputs --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 00bac98a62d..bef048b2b7d 100644 --- a/action.yml +++ b/action.yml @@ -195,11 +195,11 @@ runs: with: files: ${{ inputs.files }} files-separator: ${{ inputs.files_separator }} - escape-paths: true excluded-files: ${{ inputs.files_ignore }} excluded-files-separator: ${{ inputs.files_ignore_separator }} files-from-source-file: ${{ inputs.files_from_source_file }} excluded-files-from-source-file: ${{ inputs.files_ignore_from_source_file}} + escape-paths: true working-directory: ${{ inputs.path }} base-sha: ${{ steps.changed-files-diff-sha.outputs.previous_sha }} sha: ${{ steps.changed-files-diff-sha.outputs.current_sha }} From 0ec94592d4b5ce8d6553940302e21833bed39dc9 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 18:30:52 -0600 Subject: [PATCH 04/10] Renamed entrypoint.sh -> get-changed-paths.sh --- .github/workflows/test.yml | 8 ++++---- action.yml | 2 +- entrypoint.sh => get-changed-paths.sh | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename entrypoint.sh => get-changed-paths.sh (100%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f0a49b529b7..93a6b334468 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -522,10 +522,10 @@ jobs: uses: ./ with: files: | - entrypoint.sh + get-changed-paths.sh *.sh - name: Verify all_changed_files files has no duplicates - if: contains(steps.changed-files-specific-duplicate-output.outputs.all_changed_files, 'entrypoint.sh') + if: contains(steps.changed-files-specific-duplicate-output.outputs.all_changed_files, 'get-changed-paths.sh') run: | ALL_CHANGED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_changed_files }}) UNIQUE_ALL_CHANGED_FILES=$(echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | sort -u | xargs) @@ -537,7 +537,7 @@ jobs: shell: bash - name: Verify all_changed_and_modified_files files has no duplicates - if: contains(steps.changed-files-specific-duplicate-output.outputs.all_changed_and_modified_files, 'entrypoint.sh') + if: contains(steps.changed-files-specific-duplicate-output.outputs.all_changed_and_modified_files, 'get-changed-paths.sh') run: | ALL_CHANGED_AND_MODIFIED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_changed_and_modified_files }}) UNIQUE_ALL_CHANGED_AND_MODIFIED_FILES=$(echo "$ALL_CHANGED_AND_MODIFIED_FILES" | tr ' ' '\n' | sort -u | xargs) @@ -549,7 +549,7 @@ jobs: shell: bash - name: Verify all_modified_files files has no duplicates - if: contains(steps.changed-files-specific-duplicate-output.outputs.all_modified_files, 'entrypoint.sh') + if: contains(steps.changed-files-specific-duplicate-output.outputs.all_modified_files, 'get-changed-paths.sh') run: | ALL_MODIFIED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_modified_files }}) UNIQUE_ALL_MODIFIED_FILES=$(echo "$ALL_MODIFIED_FILES" | tr ' ' '\n' | sort -u | xargs) diff --git a/action.yml b/action.yml index bef048b2b7d..92c21e97d86 100644 --- a/action.yml +++ b/action.yml @@ -206,7 +206,7 @@ runs: include-deleted-files: true separator: "|" - run: | - bash $GITHUB_ACTION_PATH/entrypoint.sh + bash $GITHUB_ACTION_PATH/get-changed-paths.sh id: changed-files shell: bash env: diff --git a/entrypoint.sh b/get-changed-paths.sh similarity index 100% rename from entrypoint.sh rename to get-changed-paths.sh From b6b72a44c83eb61e43b7096bc24175f820d75829 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 18:35:01 -0600 Subject: [PATCH 05/10] Updated to use INPUT_HAS_CUSTOM_PATTERNS. --- action.yml | 1 + get-changed-paths.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 92c21e97d86..4c548769416 100644 --- a/action.yml +++ b/action.yml @@ -227,6 +227,7 @@ runs: INPUT_DIFF_RELATIVE: ${{ inputs.diff_relative }} INPUT_DIR_NAMES: ${{ inputs.dir_names }} INPUT_JSON: ${{ inputs.json }} + INPUT_HAS_CUSTOM_PATTERNS: ${{ steps.glob.outputs.has-custom-patterns }} branding: icon: file-text diff --git a/get-changed-paths.sh b/get-changed-paths.sh index 6866f6321e7..fe63604b187 100755 --- a/get-changed-paths.sh +++ b/get-changed-paths.sh @@ -81,7 +81,7 @@ echo "Retrieving changes between $INPUT_PREVIOUS_SHA ($INPUT_TARGET_BRANCH) → echo "Getting diff..." -if [[ -z "$INPUT_FILES_PATTERN_FILE" ]]; then +if [[ "$INPUT_HAS_CUSTOM_PATTERNS" == "false" ]]; then if [[ "$INPUT_JSON" == "false" ]]; then ADDED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" A | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') COPIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" C | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') From 2c4c729f2a74ebf2b1360bb395d3ce43d826bb84 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 18:59:22 -0600 Subject: [PATCH 06/10] revert removing since-last-remote-commit input --- action.yml | 5 +++++ get-base-sha.sh | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 4c548769416..b1824a0f55f 100644 --- a/action.yml +++ b/action.yml @@ -50,6 +50,10 @@ inputs: base_sha: description: "Specify a base commit SHA on used for comparing changes" required: false + since_last_remote_commit: + description: "Use the last commit on the remote branch as the base_sha for push event." + required: false + default: "false" since: description: "Get changed files for commits whose timestamp is older than the given time" required: false @@ -160,6 +164,7 @@ runs: INPUT_BASE_SHA: ${{ inputs.base_sha }} INPUT_SHA: ${{ inputs.sha }} INPUT_PATH: ${{ inputs.path }} + INPUT_SINCE_LAST_REMOTE_COMMIT: ${{ inputs.since_last_remote_commit }} - run: | # "Calculate the sha..." bash $GITHUB_ACTION_PATH/get-sha.sh diff --git a/get-base-sha.sh b/get-base-sha.sh index 9cd65e77a41..e94240431d3 100644 --- a/get-base-sha.sh +++ b/get-base-sha.sh @@ -21,7 +21,7 @@ if [[ -n "$INPUT_SINCE" ]]; then echo "::set-output name=base_sha::$BASE_SHA" elif [[ -n "$INPUT_BASE_SHA" ]]; then echo "::set-output name=base_sha::$INPUT_BASE_SHA" -else +elif [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then LAST_REMOTE_COMMIT="$GITHUB_EVENT_BEFORE" if [[ -z "$LAST_REMOTE_COMMIT" || "$LAST_REMOTE_COMMIT" == "0000000000000000000000000000000000000000" ]]; then From 5485a02af8ec905520937e24e653b443047da143 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 19:08:58 -0600 Subject: [PATCH 07/10] Updated to use the last remote commit by default. --- action.yml | 1 + diff-sha.sh | 24 +++++++++--------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/action.yml b/action.yml index b1824a0f55f..47f6744c7fe 100644 --- a/action.yml +++ b/action.yml @@ -189,6 +189,7 @@ runs: GITHUB_HEAD_REF: ${{ github.head_ref }} GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} + GITHUB_EVENT_BEFORE: ${{ github.event.before }} # INPUT_ is not available in Composite run steps # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs INPUT_SHA: ${{ steps.sha.outputs.sha }} diff --git a/diff-sha.sh b/diff-sha.sh index 9116f3ba3fd..2269749302d 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -57,30 +57,24 @@ else fi if [[ -z $GITHUB_BASE_REF ]]; then - TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} - CURRENT_BRANCH=$TARGET_BRANCH - - echo "::debug::GITHUB_BASE_REF unset using $TARGET_BRANCH..." + TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$? + CURRENT_BRANCH=$TARGET_BRANCH && exit_status=$? || exit_status=$? if [[ -z $INPUT_BASE_SHA ]]; then - git fetch --no-tags -u --progress origin --depth=2 "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$? - - if [[ $(git rev-list --count "HEAD") -gt 1 ]]; then - PREVIOUS_SHA=$(git rev-parse "@~1" 2>&1) && exit_status=$? || exit_status=$? - echo "::debug::Previous SHA: $PREVIOUS_SHA" - else - PREVIOUS_SHA=$CURRENT_SHA; exit_status=$? + PREVIOUS_SHA=$GITHUB_EVENT_BEFORE + if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then INITIAL_COMMIT="true" echo "::debug::Initial commit detected" - echo "::debug::Previous SHA: $PREVIOUS_SHA" fi else - PREVIOUS_SHA=$INPUT_BASE_SHA; exit_status=$? + PREVIOUS_SHA=$INPUT_BASE_SHA TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$? - echo "::debug::Previous SHA: $PREVIOUS_SHA" - echo "::debug::Target branch: $TARGET_BRANCH" + CURRENT_BRANCH=$TARGET_BRANCH fi + echo "::debug::Target branch $TARGET_BRANCH..." + echo "::debug::Current branch $CURRENT_BRANCH..." + echo "::debug::Verifying the previous commit SHA: $PREVIOUS_SHA" git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$? From de772eb038d30fb955fda775c66e2773009d53c8 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 19:11:18 -0600 Subject: [PATCH 08/10] added empty line --- get-base-sha.sh | 2 +- get-sha.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/get-base-sha.sh b/get-base-sha.sh index e94240431d3..c2fcee3c283 100644 --- a/get-base-sha.sh +++ b/get-base-sha.sh @@ -31,4 +31,4 @@ elif [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then LAST_REMOTE_COMMIT=$(git rev-parse "$INPUT_SHA^1") fi echo "::set-output name=base_sha::$LAST_REMOTE_COMMIT" -fi \ No newline at end of file +fi diff --git a/get-sha.sh b/get-sha.sh index e94a9d79ce6..ede5529c090 100644 --- a/get-sha.sh +++ b/get-sha.sh @@ -21,4 +21,4 @@ if [[ -n "$INPUT_UNTIL" ]]; then echo "::set-output name=sha::$SHA" else echo "::set-output name=sha::$INPUT_SHA" -fi \ No newline at end of file +fi From ef1134dca7db98126adfe9ad0a47dde38e56c6af Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 19:33:05 -0600 Subject: [PATCH 09/10] update to handle the default event.before --- diff-sha.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/diff-sha.sh b/diff-sha.sh index 2269749302d..988c598a1f1 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -62,6 +62,14 @@ if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z $INPUT_BASE_SHA ]]; then PREVIOUS_SHA=$GITHUB_EVENT_BEFORE + + if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then + PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1)") + fi + if [[ "$INPUT_SHA" == "$PREVIOUS_SHA" ]]; then + PREVIOUS_SHA=$(git rev-parse "$INPUT_SHA^1") + fi + if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then INITIAL_COMMIT="true" echo "::debug::Initial commit detected" From 05f9744e418ca2abb86747d06d248d5416fff147 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sat, 24 Sep 2022 19:33:47 -0600 Subject: [PATCH 10/10] remove unused code --- diff-sha.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 988c598a1f1..434f2899c6b 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -66,9 +66,6 @@ if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1)") fi - if [[ "$INPUT_SHA" == "$PREVIOUS_SHA" ]]; then - PREVIOUS_SHA=$(git rev-parse "$INPUT_SHA^1") - fi if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then INITIAL_COMMIT="true"