diff --git a/src/functions.sh b/src/functions.sh index 723a3bdf..9ae4285f 100644 --- a/src/functions.sh +++ b/src/functions.sh @@ -41,10 +41,16 @@ is_strict_check_on_push_demanded () { # Function that picks values of BASE and HEAD commit based on triggrring event (INPUT_TRIGGERING_EVENT) # It sets BASE and HEAD for external use. +# https://github.com/tj-actions/changed-files/blob/main/diff-sha.sh#L116-L118 +# https://stackoverflow.com/a/61861763/10221282 # $? - return value - 0 on success pick_base_and_head_hash () { case ${INPUT_TRIGGERING_EVENT-${GITHUB_EVENT_NAME}} in "push") + if [[ -z "${INPUT_PUSH_EVENT_BASE}" || "${INPUT_PUSH_EVENT_BASE}" == "0000000000000000000000000000000000000000" ]]; then + INPUT_PUSH_EVENT_BASE=$(git rev-list -n 1 "HEAD~1") + fi + export BASE=${INPUT_PUSH_EVENT_BASE:-} export HEAD=${INPUT_PUSH_EVENT_HEAD:-} [[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\"" diff --git a/test/pick_base_and_head_hash.bats b/test/pick_base_and_head_hash.bats index bdb9bb2d..6d54d61a 100644 --- a/test/pick_base_and_head_hash.bats +++ b/test/pick_base_and_head_hash.bats @@ -40,6 +40,25 @@ setup () { # assert_equal "\"${HEAD}\"" "\"${INPUT_PUSH_EVENT_HEAD}\"" } +@test "pick_base_and_head_hash() - trigger event = push - first commit/new branch" { + source "${PROJECT_ROOT}/src/functions.sh" + + INPUT_TRIGGERING_EVENT="push" + + UNIT_TESTS=0 + INPUT_PUSH_EVENT_BASE="0000000000000000000000000000000000000000" + INPUT_PUSH_EVENT_HEAD="ghijkl789012" + + run pick_base_and_head_hash + assert_success + assert_output \ +"first commit on new branch detected +BASE:\"${TRUE_BASE}\" ; HEAD:\"${INPUT_PUSH_EVENT_HEAD}\"" + # TODO: Doesn't work, don't know why... + # assert_equal "\"${BASE}\"" "\"${TRUE_BASE}\"" + # assert_equal "\"${HEAD}\"" "\"${INPUT_PUSH_EVENT_HEAD}\"" +} + @test "pick_base_and_head_hash() - trigger event = pull_request" { source "${PROJECT_ROOT}/src/functions.sh"