diff --git a/action.yml b/action.yml index 70c902b7..00bac98a 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 00000000..9cd65e77 --- /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 00000000..e94a9d79 --- /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