Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle cases when BASE SHA isn't known on push events :basecamp: #196

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ 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
if [[ ${UNIT_TESTS:-1} -ne 0 ]]; then
INPUT_PUSH_EVENT_BASE=$(git rev-list -n 1 "HEAD~1")
else
echo "first commit on new branch detected"
fi
fi

export BASE=${INPUT_PUSH_EVENT_BASE:-}
export HEAD=${INPUT_PUSH_EVENT_HEAD:-}
[[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
Expand Down
19 changes: 19 additions & 0 deletions test/pick_base_and_head_hash.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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}\""

Check warning

Code scanning / shellcheck

TRUE_BASE is referenced but not assigned.

TRUE_BASE is referenced but not assigned.
# 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"

Expand Down