Skip to content
Merged
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
23 changes: 23 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
166 changes: 166 additions & 0 deletions tests/unit/test_optional_inputs_defaults.sh
Original file line number Diff line number Diff line change
@@ -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.
<!-- Diff commits - START -->
<!-- Diff commits - END -->
<!-- Diff files - START -->
<!-- Diff files - END -->
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."