diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 488135c8..eaac4a7d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -119,6 +119,34 @@ jobs: shell: bash + test-unset-github-output-env: + name: Test unset GITHUB_OUTPUT env + runs-on: ${{ matrix.platform }} + strategy: + fail-fast: false + max-parallel: 4 + matrix: + platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022] + + steps: + - name: Checkout to branch + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Run changed-files with unset GITHUB_OUTPUT env + id: changed-files + continue-on-error: true + uses: ./ + env: + GITHUB_OUTPUT: "" + + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files.outputs) }}' + shell: + bash + test-single-commit-history: name: Test changed-files single commit history runs-on: ${{ matrix.platform }} @@ -335,6 +363,7 @@ jobs: continue-on-error: true with: since: "2022-08-19" + target_branch_fetch_depth: "20000" - name: Show output run: | echo '${{ toJSON(steps.changed-files-since.outputs) }}' @@ -346,6 +375,7 @@ jobs: continue-on-error: true with: until: "2022-08-20" + target_branch_fetch_depth: "20000" - name: Show output run: | echo '${{ toJSON(steps.changed-files-until.outputs) }}' diff --git a/action.yml b/action.yml index 0501b614..9c47d21d 100644 --- a/action.yml +++ b/action.yml @@ -150,33 +150,6 @@ outputs: runs: using: "composite" steps: - - run: | - # "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 }} - GITHUB_EVENT_FORCED : ${{ github.event.forced }} - # 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: | - # "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 @@ -193,8 +166,10 @@ runs: GITHUB_EVENT_FORCED: ${{ github.event.forced }} # 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 }} - INPUT_BASE_SHA: ${{ steps.base-sha.outputs.base_sha }} + INPUT_SHA: ${{ inputs.sha }} + INPUT_BASE_SHA: ${{ inputs.base_sha }} + INPUT_SINCE: ${{ inputs.since }} + INPUT_UNTIL: ${{ inputs.until }} INPUT_PATH: ${{ inputs.path }} INPUT_TARGET_BRANCH_FETCH_DEPTH: ${{ inputs.target_branch_fetch_depth }} - name: Glob match diff --git a/diff-sha.sh b/diff-sha.sh index f6157dde..f7831edb 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -3,6 +3,7 @@ set -eu INITIAL_COMMIT="false" +GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} echo "::group::changed-files-diff-sha" @@ -40,7 +41,16 @@ fi echo "::debug::Getting HEAD SHA..." if [[ -z $INPUT_SHA ]]; then - CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1) && exit_status=$? || exit_status=$? + if [[ -n "$INPUT_UNTIL" ]]; then + CURRENT_SHA=$(git log -1 --format="%H" --date=local --until="$INPUT_UNTIL") && exit_status=$? || exit_status=$? + + if [[ $exit_status -ne 0 ]]; then + echo "::error::Invalid until date: $INPUT_UNTIL" + exit 1 + fi + else + CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1) && exit_status=$? || exit_status=$? + fi else CURRENT_SHA=$INPUT_SHA; exit_status=$? fi @@ -63,22 +73,37 @@ if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z $INPUT_BASE_SHA ]]; then git fetch --no-tags -u --progress origin --depth="$INPUT_TARGET_BRANCH_FETCH_DEPTH" "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$? - PREVIOUS_SHA="" - if [[ "$GITHUB_EVENT_FORCED" == "false" ]]; then - PREVIOUS_SHA=$GITHUB_EVENT_BEFORE - fi + if [[ -n "$INPUT_SINCE" ]]; then + PREVIOUS_SHA=$(git log --format="%H" --date=local --since="$INPUT_SINCE" --reverse | head -n 1) - if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then - PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1 | xargs)") - fi + if [[ -z "$PREVIOUS_SHA" ]]; then + echo "::error::Unable to locate a previous commit for the specified date: $INPUT_SINCE" + exit 1 + fi + else + PREVIOUS_SHA="" - if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then - PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA^1") + if [[ "$GITHUB_EVENT_FORCED" == "false" ]]; then + PREVIOUS_SHA=$GITHUB_EVENT_BEFORE + fi + + if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then + PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1 | xargs)") + fi if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then - INITIAL_COMMIT="true" - echo "::debug::Initial commit detected" + PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA^1") + + if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then + INITIAL_COMMIT="true" + echo "::debug::Initial commit detected" + fi + fi + + if [[ -z "$PREVIOUS_SHA" ]]; then + echo "::error::Unable to locate a previous commit" + exit 1 fi fi else @@ -132,11 +157,18 @@ if [[ -n "$PREVIOUS_SHA" && -n "$CURRENT_SHA" && "$PREVIOUS_SHA" == "$CURRENT_SH exit 1 fi -cat <> "$GITHUB_OUTPUT" +if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=target_branch::$TARGET_BRANCH" + echo "::set-output name=current_branch::$CURRENT_BRANCH" + echo "::set-output name=previous_sha::$PREVIOUS_SHA" + echo "::set-output name=current_sha::$CURRENT_SHA" +else + cat <> "$GITHUB_OUTPUT" target_branch=$TARGET_BRANCH current_branch=$CURRENT_BRANCH previous_sha=$PREVIOUS_SHA current_sha=$CURRENT_SHA EOF +fi echo "::endgroup::" diff --git a/get-base-sha.sh b/get-base-sha.sh deleted file mode 100644 index 09e8703c..00000000 --- a/get-base-sha.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/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 "base_sha=$BASE_SHA" >> "$GITHUB_OUTPUT" -elif [[ -n "$INPUT_BASE_SHA" ]]; then - echo "base_sha=$INPUT_BASE_SHA" >> "$GITHUB_OUTPUT" -fi diff --git a/get-changed-paths.sh b/get-changed-paths.sh index 388474ce..e597d256 100755 --- a/get-changed-paths.sh +++ b/get-changed-paths.sh @@ -7,6 +7,8 @@ INPUT_SEPARATOR="${INPUT_SEPARATOR//'.'/'%2E'}" INPUT_SEPARATOR="${INPUT_SEPARATOR//$'\n'/'%0A'}" INPUT_SEPARATOR="${INPUT_SEPARATOR//$'\r'/'%0D'}" +GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} + if [[ $INPUT_QUOTEPATH == "false" ]]; then git config --global core.quotepath off else @@ -131,9 +133,17 @@ else if [[ -n "${ALL_CHANGED}" ]]; then echo "::debug::Matching changed files: ${ALL_CHANGED}" - echo "any_changed=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_changed::true" + else + echo "any_changed=true" >> "$GITHUB_OUTPUT" + fi else - echo "any_changed=false" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_changed::false" + else + echo "any_changed=false" >> "$GITHUB_OUTPUT" + fi fi OTHER_CHANGED="" @@ -154,19 +164,38 @@ else if [[ -n "${OTHER_CHANGED}" && "${OTHER_CHANGED}" != "[]" ]]; then echo "::debug::Non Matching changed files: ${OTHER_CHANGED}" - echo "only_changed=false" >> "$GITHUB_OUTPUT" - echo "other_changed_files=$OTHER_CHANGED" >> "$GITHUB_OUTPUT" + + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_changed::false" + echo "::set-output name=other_changed_files::$OTHER_CHANGED" + else + echo "only_changed=false" >> "$GITHUB_OUTPUT" + echo "other_changed_files=$OTHER_CHANGED" >> "$GITHUB_OUTPUT" + fi + elif [[ -n "${ALL_CHANGED}" ]]; then - echo "only_changed=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_changed::true" + else + echo "only_changed=true" >> "$GITHUB_OUTPUT" + fi fi ALL_OTHER_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMRD" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') if [[ -n "${ALL_MODIFIED}" ]]; then echo "::debug::Matching modified files: ${ALL_MODIFIED}" - echo "any_modified=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_modified::true" + else + echo "any_modified=true" >> "$GITHUB_OUTPUT" + fi else - echo "any_modified=false" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_modified::false" + else + echo "any_modified=false" >> "$GITHUB_OUTPUT" + fi fi OTHER_MODIFIED="" @@ -187,19 +216,37 @@ else if [[ -n "${OTHER_MODIFIED}" && "$OTHER_MODIFIED" != "[]" ]]; then echo "::debug::Non Matching modified files: ${OTHER_MODIFIED}" - echo "only_modified=false" >> "$GITHUB_OUTPUT" - echo "other_modified_files=$OTHER_MODIFIED" >> "$GITHUB_OUTPUT" + + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_modified::false" + echo "::set-output name=other_modified_files::$OTHER_MODIFIED" + else + echo "only_modified=false" >> "$GITHUB_OUTPUT" + echo "other_modified_files=$OTHER_MODIFIED" >> "$GITHUB_OUTPUT" + fi elif [[ -n "${ALL_MODIFIED}" ]]; then - echo "only_modified=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_modified::true" + else + echo "only_modified=true" >> "$GITHUB_OUTPUT" + fi fi ALL_OTHER_DELETED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" D | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') if [[ -n "${DELETED}" ]]; then echo "::debug::Matching deleted files: ${DELETED}" - echo "any_deleted=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_deleted::true" + else + echo "any_deleted=true" >> "$GITHUB_OUTPUT" + fi else - echo "any_deleted=false" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=any_deleted::false" + else + echo "any_deleted=false" >> "$GITHUB_OUTPUT" + fi fi OTHER_DELETED="" @@ -220,10 +267,19 @@ else if [[ -n "${OTHER_DELETED}" && "${OTHER_DELETED}" != "[]" ]]; then echo "::debug::Non Matching deleted files: ${OTHER_DELETED}" - echo "only_deleted=false" >> "$GITHUB_OUTPUT" - echo "other_deleted_files=$OTHER_DELETED" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_deleted::false" + echo "::set-output name=other_deleted_files::$OTHER_DELETED" + else + echo "only_deleted=false" >> "$GITHUB_OUTPUT" + echo "other_deleted_files=$OTHER_DELETED" >> "$GITHUB_OUTPUT" + fi elif [[ -n "${DELETED}" ]]; then - echo "only_deleted=true" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=only_deleted::true" + else + echo "only_deleted=true" >> "$GITHUB_OUTPUT" + fi fi if [[ "$INPUT_JSON" == "false" ]]; then ADDED=$(echo "${ADDED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') @@ -267,7 +323,20 @@ if [[ $INPUT_INCLUDE_ALL_OLD_NEW_RENAMED_FILES == "true" ]]; then echo "::debug::All old & new renamed files: $ALL_OLD_NEW_RENAMED" fi -cat <> "$GITHUB_OUTPUT" +if [[ -z "$GITHUB_OUTPUT" ]]; then + 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=type_changed_files::$TYPE_CHANGED" + echo "::set-output name=unmerged_files::$UNMERGED" + echo "::set-output name=unknown_files::$UNKNOWN" + echo "::set-output name=all_changed_and_modified_files::$ALL_CHANGED_AND_MODIFIED" + echo "::set-output name=all_changed_files::$ALL_CHANGED" + echo "::set-output name=all_modified_files::$ALL_MODIFIED" +else + cat <> "$GITHUB_OUTPUT" added_files=$ADDED copied_files=$COPIED deleted_files=$DELETED @@ -280,8 +349,14 @@ all_changed_and_modified_files=$ALL_CHANGED_AND_MODIFIED all_changed_files=$ALL_CHANGED all_modified_files=$ALL_MODIFIED EOF +fi + if [[ $INPUT_INCLUDE_ALL_OLD_NEW_RENAMED_FILES == "true" ]]; then - echo "all_old_new_renamed_files=$ALL_OLD_NEW_RENAMED" >> "$GITHUB_OUTPUT" + if [[ -z "$GITHUB_OUTPUT" ]]; then + echo "::set-output name=all_old_new_renamed_files::$ALL_OLD_NEW_RENAMED" + else + echo "all_old_new_renamed_files=$ALL_OLD_NEW_RENAMED" >> "$GITHUB_OUTPUT" + fi fi echo "::endgroup::" diff --git a/get-sha.sh b/get-sha.sh deleted file mode 100644 index 8ffdfc11..00000000 --- a/get-sha.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/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 "sha=$SHA" >> "$GITHUB_OUTPUT" -else - echo "sha=$INPUT_SHA" >> "$GITHUB_OUTPUT" -fi