diff --git a/entrypoint.sh b/entrypoint.sh index 717d4f6..27f15c3 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -149,15 +149,13 @@ create_pull_request() { else echo "CREATE PULL REQUEST" - if [ -n "$INPUT_PULL_REQUEST_BODY" ]; then - BODY=",\"body\":\"${INPUT_PULL_REQUEST_BODY//$'\n'/\\n}\"" - fi - + # The body is passed as data, never as part of the jq program. A literal "\n" still means a new line PULL_RESPONSE_DATA=$(jq -n --arg pr_title "${INPUT_PULL_REQUEST_TITLE}" \ --arg base_branch "${BASE_BRANCH}" \ --arg branch "${BRANCH}" \ - --arg body "${BODY}" \ - "{title: \$pr_title, base: \$base_branch, head: \$branch ${BODY}}") + --arg body "${INPUT_PULL_REQUEST_BODY}" \ + '{title: $pr_title, base: $base_branch, head: $branch} + + if $body == "" then {} else {body: ($body | split("\\n") | join("\n"))} end') # create pull request PULL_RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${PULL_RESPONSE_DATA}" "${PULLS_URL}") diff --git a/tests/pull-request-payload.sh b/tests/pull-request-payload.sh new file mode 100644 index 0000000..f167189 --- /dev/null +++ b/tests/pull-request-payload.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# Run with Bash and jq. Pass an entrypoint path to check a different revision. +set -eo pipefail + +entrypoint=${1:-"$(dirname "$0")/../entrypoint.sh"} +tmp_dir=$(mktemp -d) +trap 'rm -rf "$tmp_dir"' EXIT + +# Load the production function without starting the action. +sed -n '/^create_pull_request() {$/,/^}$/p' "$entrypoint" > "$tmp_dir/create-pull-request.sh" +source "$tmp_dir/create-pull-request.sh" + +curl() { + while [ "$#" -gt 0 ]; do + case "$1" in + --write-out) printf '200'; return ;; + -X) if [ "$2" = GET ]; then printf '[]200'; return; fi ;; + --data) + printf '%s\n' "$2" > "$tmp_dir/payload.json" + printf '{"html_url":"https://example.invalid/pull/1","number":1}' + return + ;; + esac + shift + done + return 1 +} + +view_debug_output() { :; } + +GITHUB_OUTPUT="$tmp_dir/outputs" +GITHUB_REPOSITORY=example/translations +export GITHUB_TOKEN=test +unset GH_TOKEN +INPUT_PULL_REQUEST_TITLE='Update translations' +INPUT_PULL_REQUEST_BASE_BRANCH_NAME=main +INPUT_PULL_REQUEST_LABELS= +INPUT_PULL_REQUEST_ASSIGNEES= +INPUT_PULL_REQUEST_REVIEWERS= +INPUT_PULL_REQUEST_TEAM_REVIEWERS= + +failed=0 + +check_payload() { + local name=$1 input=$2 expected=$3 + rm -f "$tmp_dir/payload.json" + if ( + INPUT_PULL_REQUEST_BODY=$input + create_pull_request translations + ) > "$tmp_dir/log" 2>&1 && jq -e --arg expected "$expected" ' + {title: "Update translations", base: "main", head: "translations"} + + (if $expected == "" then {} else {body: $expected} end) == . + ' "$tmp_dir/payload.json" > /dev/null; then + printf 'PASS %s\n' "$name" + else + printf 'FAIL %s\n' "$name" >&2 + cat "$tmp_dir/log" >&2 + failed=1 + fi +} + +check_payload 'double quotes' 'Update "strings"' 'Update "strings"' +check_payload 'backslash' 'translations\fr' 'translations\fr' +check_payload 'literal placeholder' 'Update ${locale}' 'Update ${locale}' +check_payload 'literal newline escape' 'first\nsecond' $'first\nsecond' +check_payload 'actual newline' $'first\nsecond' $'first\nsecond' +check_payload 'literal escaped quote and tab' 'Update \"strings\"\t' 'Update \"strings\"\t' +check_payload 'empty body' '' '' + +exit "$failed"