From 36055f1ab0d6d6fc0347ff3035cc932c74708a3b Mon Sep 17 00:00:00 2001 From: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com> Date: Sat, 6 Jun 2026 01:28:06 +0200 Subject: [PATCH] fix: normalize inputs --- entrypoint.sh | 23 +++ tests/unit/test_optional_inputs_defaults.sh | 166 ++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100755 tests/unit/test_optional_inputs_defaults.sh diff --git a/entrypoint.sh b/entrypoint.sh index a4ad5ed..1e95954 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -160,6 +160,29 @@ trim_whitespace() { printf '%s' "${value}" } +# GitHub Actions usually exports every input env var, even when empty. Direct +# docker execution does not, so normalize optional inputs before `set -u` +# references later in the script. +: "${INPUT_REPOSITORY:=}" +: "${INPUT_REPOSITORY_PATH:=.}" +: "${INPUT_SOURCE_BRANCH:=}" +: "${INPUT_TARGET_BRANCH:=master}" +: "${INPUT_TITLE:=}" +: "${INPUT_TEMPLATE:=}" +: "${INPUT_BODY:=}" +: "${INPUT_REVIEWER:=}" +: "${INPUT_ASSIGNEE:=}" +: "${INPUT_LABEL:=}" +: "${INPUT_MILESTONE:=}" +: "${INPUT_DRAFT:=false}" +: "${INPUT_OLD_STRING:=}" +: "${INPUT_NEW_STRING:=}" +: "${INPUT_GET_DIFF:=false}" +: "${INPUT_IGNORE_USERS:=dependabot}" +: "${INPUT_ALLOW_NO_DIFF:=false}" +: "${INPUT_MAX_BODY_BYTES:=65000}" +: "${INPUT_MAX_DIFF_LINES:=0}" + append_csv_arg() { local flag="$1" local csv_value="$2" diff --git a/tests/unit/test_optional_inputs_defaults.sh b/tests/unit/test_optional_inputs_defaults.sh new file mode 100755 index 0000000..9f231cf --- /dev/null +++ b/tests/unit/test_optional_inputs_defaults.sh @@ -0,0 +1,166 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" +SCRIPT_PATH="${SCRIPT_DIR}/../../entrypoint.sh" +TMP_DIR="$(mktemp -d)" +trap 'rm -rf "${TMP_DIR}"' EXIT + +assert_contains() { + local file_path="$1" + local expected="$2" + if ! grep -Fq -- "${expected}" "${file_path}"; then + echo "Assertion failed. Expected to find: ${expected}" >&2 + echo "----- FILE CONTENT -----" >&2 + cat "${file_path}" >&2 + exit 1 + fi +} + +mkdir -p "${TMP_DIR}/bin" +mkdir -p "${TMP_DIR}/repo" + +cat > "${TMP_DIR}/bin/git" <<'EOF' +#!/usr/bin/env bash +set -Eeuo pipefail + +args=("$@") +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "-C" ]]; then + args=("${args[@]:2}") +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "config" && "${args[1]}" == "--global" ]]; then + exit 0 +fi + +if [[ "${#args[@]}" -ge 1 && "${args[0]}" == "config" ]]; then + exit 0 +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "remote" && "${args[1]}" == "set-url" ]]; then + exit 0 +fi + +if [[ "${#args[@]}" -ge 1 && "${args[0]}" == "fetch" ]]; then + exit 0 +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "rev-parse" && "${args[1]}" == "--is-inside-work-tree" ]]; then + echo "true" + exit 0 +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "show-ref" ]]; then + last_arg="${args[$((${#args[@]} - 1))]}" + if [[ "${last_arg}" == "refs/remotes/origin/test-branch" || "${last_arg}" == "refs/remotes/origin/master" ]]; then + exit 0 + fi + exit 1 +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "rev-parse" ]]; then + last_arg="${args[$((${#args[@]} - 1))]}" + if [[ "${last_arg}" == "origin/test-branch" ]]; then + echo "bbb222" + exit 0 + fi + if [[ "${last_arg}" == "origin/master" ]]; then + echo "aaa111" + exit 0 + fi +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "diff" && "${args[1]}" == "--quiet" ]]; then + exit 1 +fi + +if [[ "${#args[@]}" -ge 1 && "${args[0]}" == "diff" ]]; then + echo "M README.md" + exit 0 +fi + +if [[ "${#args[@]}" -ge 1 && "${args[0]}" == "log" ]]; then + echo "stub log" + exit 0 +fi + +if [[ "${#args[@]}" -ge 2 && "${args[0]}" == "symbolic-ref" ]]; then + echo "test-branch" + exit 0 +fi + +echo "Unsupported git call: $*" >&2 +exit 1 +EOF + +cat > "${TMP_DIR}/bin/gh" <<'EOF' +#!/usr/bin/env bash +set -Eeuo pipefail + +cmd="$*" + +if [[ "$#" -ge 1 && "$1" == "api" ]]; then + if [[ "${cmd}" == *"repos/owner/repo/pulls?state=open&base=master"* ]]; then + echo "[]" + exit 0 + fi + if [[ "${cmd}" == *"repos/owner/repo/pulls/"* ]]; then + echo "https://example.test/pr/123" + exit 0 + fi +fi + +if [[ "$#" -ge 2 && "$1" == "pr" && "$2" == "create" ]]; then + echo "https://example.test/pr/123" + exit 0 +fi + +if [[ "$#" -ge 2 && "$1" == "pr" && "$2" == "view" ]]; then + echo "123" + exit 0 +fi + +echo "Unsupported gh call: $*" >&2 +exit 1 +EOF + +chmod +x "${TMP_DIR}/bin/git" "${TMP_DIR}/bin/gh" + +cat > "${TMP_DIR}/template.md" <<'EOF' +Automated end-to-end test. + + + + +EOF + +LOG_FILE="${TMP_DIR}/run.log" +set +e +PATH="${TMP_DIR}/bin:${PATH}" \ +GITHUB_ACTOR="ci-user" \ +GITHUB_TOKEN="token" \ +GITHUB_REPOSITORY="owner/repo" \ +GITHUB_WORKSPACE="${TMP_DIR}" \ +GITHUB_OUTPUT="${TMP_DIR}/output.txt" \ +INPUT_GITHUB_TOKEN="token" \ +INPUT_SOURCE_BRANCH="test-branch" \ +INPUT_TITLE="test(pull-request): draft PR with diff - safe to close" \ +INPUT_BODY="$(cat "${TMP_DIR}/template.md")" \ +INPUT_GET_DIFF="true" \ +INPUT_DRAFT="true" \ +bash "${SCRIPT_PATH}" >"${LOG_FILE}" 2>&1 +STATUS="$?" +set -e + +if [[ "${STATUS}" != "0" ]]; then + echo "Expected successful execution with omitted optional inputs" >&2 + cat "${LOG_FILE}" >&2 + exit 1 +fi + +assert_contains "${LOG_FILE}" "template: " +assert_contains "${LOG_FILE}" "Creating pull request" +assert_contains "${TMP_DIR}/output.txt" "pr_number=123" + +echo "Optional input defaults test passed."