Files
cozsweet-frontend-nextjs/scripts/ci/notify-actions.sh
T

126 lines
3.0 KiB
Bash

#!/usr/bin/env bash
set -u
warn() {
printf 'Notification warning: %s\n' "$*" >&2
}
resolve_commit_message() {
if [ -n "${ACTION_NOTIFY_COMMIT_MESSAGE:-}" ]; then
printf '%s\n' "$ACTION_NOTIFY_COMMIT_MESSAGE"
return 0
fi
if command -v git >/dev/null 2>&1; then
git log -1 --pretty=%B 2>/dev/null || true
fi
}
extract_requirement_id() {
printf '%s\n' "$1" | grep -Eo 'REQ-[A-Za-z0-9_-]+' | head -n 1 || true
}
normalize_job_result() {
case "$1" in
success)
printf 'ready_for_test'
;;
cancelled | canceled)
printf 'cancelled'
;;
failure | failed)
printf 'failed'
;;
*)
printf 'failed'
;;
esac
}
branch_name="${BRANCH_NAME:-${GITHUB_REF_NAME:-}}"
if [ -z "$branch_name" ] && [ -n "${GITHUB_REF:-}" ]; then
branch_name="${GITHUB_REF#refs/heads/}"
fi
if [ -z "$branch_name" ]; then
branch_name="unknown"
fi
short_sha="${SHORT_SHA:-}"
if [ -z "$short_sha" ] && command -v git >/dev/null 2>&1; then
short_sha="$(git rev-parse --short HEAD 2>/dev/null || true)"
fi
if [ -z "$short_sha" ]; then
short_sha="unknown"
fi
commit_message="$(resolve_commit_message)"
requirement_id="$(extract_requirement_id "$commit_message")"
if [ -z "$requirement_id" ]; then
warn "no REQ-* requirement id found in latest commit message, skip flow notification"
exit 0
fi
base_url="${FLOW_NOTIFY_BASE_URL:-https://flow.banlv-ai.com}"
base_url="${base_url%/}"
notify_url="$base_url/requirements/$requirement_id/development-complete"
deploy_env="${DEPLOY_ENV:-unknown}"
image_tag="${IMAGE_VERSION_TAG:-unknown}"
job_status="${ACTION_JOB_STATUS:-unknown}"
workflow_name="${GITHUB_WORKFLOW:-Docker Image}"
repository="${GITHUB_REPOSITORY:-cozsweet-frontend-nextjs}"
result="$(normalize_job_result "$job_status")"
run_url=""
if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
if [ -z "$run_url" ]; then
run_url="${GITHUB_SERVER_URL:-unknown}"
fi
export FLOW_NOTIFY_OPERATOR="gitea"
export FLOW_NOTIFY_SOURCE="gitea"
export FLOW_NOTIFY_RESULT="$result"
export FLOW_NOTIFY_SUMMARY="$(cat <<EOF
Gitea workflow finished.
Workflow: $workflow_name
Repository: $repository
Branch: $branch_name
Environment: $deploy_env
Commit: $short_sha
Image: $image_tag
Status: $job_status
Run: $run_url
EOF
)"
payload="$(node <<'NODE'
const payload = {
operator: process.env.FLOW_NOTIFY_OPERATOR,
result: process.env.FLOW_NOTIFY_RESULT,
summary: process.env.FLOW_NOTIFY_SUMMARY,
source: process.env.FLOW_NOTIFY_SOURCE,
};
process.stdout.write(JSON.stringify(payload));
NODE
)"
if [ "${FLOW_NOTIFY_DRY_RUN:-}" = "1" ]; then
printf 'Flow notification dry run URL: %s\n' "$notify_url"
printf 'Flow notification dry run payload: %s\n' "$payload"
exit 0
fi
if ! curl -fsS \
-X POST "$notify_url" \
-H "Content-Type: application/json" \
-d "$payload" >/dev/null; then
warn "failed to send flow notification for $requirement_id"
exit 0
fi
echo "Sent flow notification for $requirement_id."