Merge pull request #644 from tj-actions/feat/make-since-last-remote-the-default

This commit is contained in:
Tonye Jack 2022-09-24 19:45:29 -06:00 committed by GitHub
commit f038bbbed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 101 additions and 52 deletions

View File

@ -522,10 +522,10 @@ jobs:
uses: ./ uses: ./
with: with:
files: | files: |
entrypoint.sh get-changed-paths.sh
*.sh *.sh
- name: Verify all_changed_files files has no duplicates - 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: | run: |
ALL_CHANGED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_changed_files }}) 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) UNIQUE_ALL_CHANGED_FILES=$(echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | sort -u | xargs)
@ -537,7 +537,7 @@ jobs:
shell: shell:
bash bash
- name: Verify all_changed_and_modified_files files has no duplicates - 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: | run: |
ALL_CHANGED_AND_MODIFIED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_changed_and_modified_files }}) 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) UNIQUE_ALL_CHANGED_AND_MODIFIED_FILES=$(echo "$ALL_CHANGED_AND_MODIFIED_FILES" | tr ' ' '\n' | sort -u | xargs)
@ -549,7 +549,7 @@ jobs:
shell: shell:
bash bash
- name: Verify all_modified_files files has no duplicates - 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: | run: |
ALL_MODIFIED_FILES=(${{ steps.changed-files-specific-duplicate-output.outputs.all_modified_files }}) 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) UNIQUE_ALL_MODIFIED_FILES=$(echo "$ALL_MODIFIED_FILES" | tr ' ' '\n' | sort -u | xargs)

View File

@ -151,40 +151,32 @@ runs:
using: "composite" using: "composite"
steps: steps:
- run: | - run: |
# "Set base sha..." # "Calculate the base sha..."
if [[ -n "${{ inputs.since }}" ]]; then bash $GITHUB_ACTION_PATH/get-base-sha.sh
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
id: base-sha id: base-sha
shell: bash 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: | - run: |
# "Set the sha..." # "Calculate the sha..."
if [[ -n "${{ inputs.until }}" ]]; then bash $GITHUB_ACTION_PATH/get-sha.sh
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
id: sha id: sha
shell: bash 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: | - run: |
# "Calculating the previous and current SHA..." # "Calculating the previous and current SHA..."
bash $GITHUB_ACTION_PATH/diff-sha.sh bash $GITHUB_ACTION_PATH/diff-sha.sh
@ -195,9 +187,9 @@ runs:
GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_BASE_REF: ${{ github.base_ref }} GITHUB_BASE_REF: ${{ github.base_ref }}
GITHUB_HEAD_REF: ${{ github.head_ref }} GITHUB_HEAD_REF: ${{ github.head_ref }}
GITHUB_ACTION_PATH: ${{ github.action_path }}
GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_WORKSPACE: ${{ github.workspace }}
GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} 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 # 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 # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
INPUT_SHA: ${{ steps.sha.outputs.sha }} INPUT_SHA: ${{ steps.sha.outputs.sha }}
@ -209,22 +201,21 @@ runs:
with: with:
files: ${{ inputs.files }} files: ${{ inputs.files }}
files-separator: ${{ inputs.files_separator }} files-separator: ${{ inputs.files_separator }}
escape-paths: true
excluded-files: ${{ inputs.files_ignore }} excluded-files: ${{ inputs.files_ignore }}
excluded-files-separator: ${{ inputs.files_ignore_separator }} excluded-files-separator: ${{ inputs.files_ignore_separator }}
files-from-source-file: ${{ inputs.files_from_source_file }} files-from-source-file: ${{ inputs.files_from_source_file }}
excluded-files-from-source-file: ${{ inputs.files_ignore_from_source_file}} excluded-files-from-source-file: ${{ inputs.files_ignore_from_source_file}}
escape-paths: true
working-directory: ${{ inputs.path }} working-directory: ${{ inputs.path }}
base-sha: ${{ steps.changed-files-diff-sha.outputs.previous_sha }} base-sha: ${{ steps.changed-files-diff-sha.outputs.previous_sha }}
sha: ${{ steps.changed-files-diff-sha.outputs.current_sha }} sha: ${{ steps.changed-files-diff-sha.outputs.current_sha }}
include-deleted-files: true include-deleted-files: true
separator: "|" separator: "|"
- run: | - run: |
bash $GITHUB_ACTION_PATH/entrypoint.sh bash $GITHUB_ACTION_PATH/get-changed-paths.sh
id: changed-files id: changed-files
shell: bash shell: bash
env: env:
GITHUB_ACTION_PATH: ${{ github.action_path }}
GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_WORKSPACE: ${{ github.workspace }}
# INPUT_<VARIABLE_NAME> is not available in Composite run steps # 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 # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
@ -242,6 +233,7 @@ runs:
INPUT_DIFF_RELATIVE: ${{ inputs.diff_relative }} INPUT_DIFF_RELATIVE: ${{ inputs.diff_relative }}
INPUT_DIR_NAMES: ${{ inputs.dir_names }} INPUT_DIR_NAMES: ${{ inputs.dir_names }}
INPUT_JSON: ${{ inputs.json }} INPUT_JSON: ${{ inputs.json }}
INPUT_HAS_CUSTOM_PATTERNS: ${{ steps.glob.outputs.has-custom-patterns }}
branding: branding:
icon: file-text icon: file-text

View File

@ -57,30 +57,29 @@ else
fi fi
if [[ -z $GITHUB_BASE_REF ]]; then if [[ -z $GITHUB_BASE_REF ]]; then
TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$?
CURRENT_BRANCH=$TARGET_BRANCH CURRENT_BRANCH=$TARGET_BRANCH && exit_status=$? || exit_status=$?
echo "::debug::GITHUB_BASE_REF unset using $TARGET_BRANCH..."
if [[ -z $INPUT_BASE_SHA ]]; then 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 [[ $(git rev-list --count "HEAD") -gt 1 ]]; then if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then
PREVIOUS_SHA=$(git rev-parse "@~1" 2>&1) && exit_status=$? || exit_status=$? PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1)")
echo "::debug::Previous SHA: $PREVIOUS_SHA" fi
else
PREVIOUS_SHA=$CURRENT_SHA; exit_status=$? if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then
INITIAL_COMMIT="true" INITIAL_COMMIT="true"
echo "::debug::Initial commit detected" echo "::debug::Initial commit detected"
echo "::debug::Previous SHA: $PREVIOUS_SHA"
fi fi
else 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=$? TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA" CURRENT_BRANCH=$TARGET_BRANCH
echo "::debug::Target branch: $TARGET_BRANCH"
fi fi
echo "::debug::Target branch $TARGET_BRANCH..."
echo "::debug::Current branch $CURRENT_BRANCH..."
echo "::debug::Verifying the previous commit SHA: $PREVIOUS_SHA" 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=$? git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?

34
get-base-sha.sh Normal file
View File

@ -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

View File

@ -81,7 +81,7 @@ echo "Retrieving changes between $INPUT_PREVIOUS_SHA ($INPUT_TARGET_BRANCH) →
echo "Getting diff..." echo "Getting diff..."
if [[ -z "$INPUT_FILES_PATTERN_FILE" ]]; then if [[ "$INPUT_HAS_CUSTOM_PATTERNS" == "false" ]]; then
if [[ "$INPUT_JSON" == "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}') 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}') 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}')

24
get-sha.sh Normal file
View File

@ -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