ops-recipes/scripts/comments-post-check.sh

49 lines
1.2 KiB
Bash
Executable file

#!/bin/sh
set -eu
BASE_URL=${BLOG_URL:-}
COOKIE_JAR=${COOKIE_JAR:-}
ARTICLE_ID=${ARTICLE_ID:-}
COMMENT_CONTENT=${COMMENT_CONTENT:-probe operativa}
ORIGIN=${ORIGIN:-}
if [ "${CONFIRM:-0}" != "1" ]; then
printf 'Impostare CONFIRM=1 per inviare un commento di prova\n' >&2
exit 2
fi
if [ -z "$BASE_URL" ] || [ -z "$COOKIE_JAR" ] || [ -z "$ARTICLE_ID" ]; then
printf 'BLOG_URL, COOKIE_JAR e ARTICLE_ID sono obbligatori\n' >&2
exit 2
fi
base=${BASE_URL%/}
if [ -z "$ORIGIN" ]; then
ORIGIN=$(printf '%s\n' "$base" | cut -d/ -f1,2,3)
fi
payload=$(jq -nc \
--arg articleId "$ARTICLE_ID" \
--arg author "probe" \
--arg content "$COMMENT_CONTENT" \
--arg email "probe@example.test" \
'{articleId:$articleId,author:$author,content:$content,email:$email}')
response=$(mktemp)
trap 'rm -f "$response"' EXIT HUP INT TERM
code=$(curl -sS -o "$response" -w '%{http_code}' \
-b "$COOKIE_JAR" \
-H "Origin: $ORIGIN" \
-H 'X-Comments-Form: 1' \
-H 'Content-Type: application/json' \
--data-binary "$payload" \
"$base/api/comments")
printf 'POST /api/comments -> HTTP %s\n' "$code"
if [ "$code" = "201" ]; then
jq '{status,prNumber,prUrl,branch,filePath}' "$response"
exit 0
fi
jq '{error,message}' "$response" 2>/dev/null || true
exit 1