ci(actions): use action notify webhook
This commit is contained in:
@@ -5,36 +5,26 @@ warn() {
|
||||
printf 'Notification warning: %s\n' "$*" >&2
|
||||
}
|
||||
|
||||
resolve_commit_message() {
|
||||
if [ -n "${ACTION_NOTIFY_COMMIT_MESSAGE:-}" ]; then
|
||||
printf '%s\n' "$ACTION_NOTIFY_COMMIT_MESSAGE"
|
||||
generate_chat_id() {
|
||||
if [ -n "${ACTION_NOTIFY_CHAT_ID:-}" ]; then
|
||||
printf '%s' "$ACTION_NOTIFY_CHAT_ID"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if command -v git >/dev/null 2>&1; then
|
||||
git log -1 --pretty=%B 2>/dev/null || true
|
||||
if command -v node >/dev/null 2>&1; then
|
||||
node <<'NODE'
|
||||
const { randomUUID } = require("node:crypto");
|
||||
process.stdout.write(`oc_${randomUUID().replaceAll("-", "")}`);
|
||||
NODE
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
extract_requirement_id() {
|
||||
printf '%s\n' "$1" | grep -Eo 'REQ-[A-Za-z0-9_-]+' | head -n 1 || true
|
||||
}
|
||||
if command -v openssl >/dev/null 2>&1; then
|
||||
printf 'oc_%s' "$(openssl rand -hex 16)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
normalize_job_result() {
|
||||
case "$1" in
|
||||
success)
|
||||
printf 'ready_for_test'
|
||||
;;
|
||||
cancelled | canceled)
|
||||
printf 'cancelled'
|
||||
;;
|
||||
failure | failed)
|
||||
printf 'failed'
|
||||
;;
|
||||
*)
|
||||
printf 'failed'
|
||||
;;
|
||||
esac
|
||||
printf 'oc_%s%s' "$(date +%s)" "$$"
|
||||
}
|
||||
|
||||
branch_name="${BRANCH_NAME:-${GITHUB_REF_NAME:-}}"
|
||||
@@ -53,23 +43,15 @@ 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"
|
||||
chat_id="$(generate_chat_id)"
|
||||
|
||||
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
|
||||
@@ -79,10 +61,7 @@ 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
|
||||
export ACTION_NOTIFY_MESSAGE="$(cat <<EOF
|
||||
Gitea workflow finished.
|
||||
|
||||
Workflow: $workflow_name
|
||||
@@ -98,19 +77,21 @@ 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,
|
||||
message: process.env.ACTION_NOTIFY_MESSAGE,
|
||||
};
|
||||
|
||||
process.stdout.write(JSON.stringify(payload));
|
||||
NODE
|
||||
)"
|
||||
|
||||
encoded_chat_id="$(
|
||||
node -e 'process.stdout.write(encodeURIComponent(process.argv[1] || ""))' "$chat_id"
|
||||
)"
|
||||
notify_url="$base_url/webhooks/action-notify?chat_id=$encoded_chat_id"
|
||||
|
||||
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"
|
||||
printf 'Action notification dry run URL: %s\n' "$notify_url"
|
||||
printf 'Action notification dry run payload: %s\n' "$payload"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -118,8 +99,8 @@ 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"
|
||||
warn "failed to send action notification for $chat_id"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "Sent flow notification for $requirement_id."
|
||||
echo "Sent action notification for $chat_id."
|
||||
|
||||
Reference in New Issue
Block a user