Skip to content

Commit

Permalink
Merge pull request #644 from tj-actions/feat/make-since-last-remote-t…
Browse files Browse the repository at this point in the history
…he-default
  • Loading branch information
jackton1 committed Sep 25, 2022
2 parents fc8e459 + 05f9744 commit f038bbb
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 52 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
58 changes: 25 additions & 33 deletions action.yml
Expand Up @@ -151,40 +151,32 @@ 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 }}"
elif [[ "${{ inputs.since_last_remote_commit }}" == "true" ]]; then
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_<VARIABLE_NAME> 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 }}
INPUT_SINCE_LAST_REMOTE_COMMIT: ${{ inputs.since_last_remote_commit }}
- 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_<VARIABLE_NAME> 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
Expand All @@ -195,9 +187,9 @@ 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 }}
GITHUB_EVENT_BEFORE: ${{ github.event.before }}
# INPUT_<VARIABLE_NAME> 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 }}
Expand All @@ -209,22 +201,21 @@ 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 }}
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:
GITHUB_ACTION_PATH: ${{ github.action_path }}
GITHUB_WORKSPACE: ${{ github.workspace }}
# INPUT_<VARIABLE_NAME> is not available in Composite run steps
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
Expand All @@ -242,6 +233,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
Expand Down
27 changes: 13 additions & 14 deletions diff-sha.sh
Expand Up @@ -57,30 +57,29 @@ 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=$?
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 [[ $(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=$?
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=$?

Expand Down
34 changes: 34 additions & 0 deletions 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"
elif [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
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
2 changes: 1 addition & 1 deletion entrypoint.sh → get-changed-paths.sh
Expand Up @@ -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}')
Expand Down
24 changes: 24 additions & 0 deletions 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

0 comments on commit f038bbb

Please sign in to comment.