ci(actions): use action notify webhook
This commit is contained in:
@@ -1,8 +1,3 @@
|
|||||||
curl -X POST https://flow.banlv-ai.com/requirements/REQ-XXXXXXXX/development-complete \
|
curl -X POST "https://flow.banlv-ai.com/webhooks/action-notify?chat_id=oc_xxx" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{
|
-d '{"message":"Gitea 内容操作完成,已同步。"}'
|
||||||
"operator": "gitea",
|
|
||||||
"result": "ready_for_test",
|
|
||||||
"summary": "Gitea 内容操作已完成:这里写他完成了什么、提交/分支/链接",
|
|
||||||
"source": "gitea"
|
|
||||||
}'
|
|
||||||
@@ -5,36 +5,26 @@ warn() {
|
|||||||
printf 'Notification warning: %s\n' "$*" >&2
|
printf 'Notification warning: %s\n' "$*" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve_commit_message() {
|
generate_chat_id() {
|
||||||
if [ -n "${ACTION_NOTIFY_COMMIT_MESSAGE:-}" ]; then
|
if [ -n "${ACTION_NOTIFY_CHAT_ID:-}" ]; then
|
||||||
printf '%s\n' "$ACTION_NOTIFY_COMMIT_MESSAGE"
|
printf '%s' "$ACTION_NOTIFY_CHAT_ID"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if command -v git >/dev/null 2>&1; then
|
if command -v node >/dev/null 2>&1; then
|
||||||
git log -1 --pretty=%B 2>/dev/null || true
|
node <<'NODE'
|
||||||
|
const { randomUUID } = require("node:crypto");
|
||||||
|
process.stdout.write(`oc_${randomUUID().replaceAll("-", "")}`);
|
||||||
|
NODE
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
extract_requirement_id() {
|
if command -v openssl >/dev/null 2>&1; then
|
||||||
printf '%s\n' "$1" | grep -Eo 'REQ-[A-Za-z0-9_-]+' | head -n 1 || true
|
printf 'oc_%s' "$(openssl rand -hex 16)"
|
||||||
}
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
normalize_job_result() {
|
printf 'oc_%s%s' "$(date +%s)" "$$"
|
||||||
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:-}}"
|
branch_name="${BRANCH_NAME:-${GITHUB_REF_NAME:-}}"
|
||||||
@@ -53,23 +43,15 @@ if [ -z "$short_sha" ]; then
|
|||||||
short_sha="unknown"
|
short_sha="unknown"
|
||||||
fi
|
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="${FLOW_NOTIFY_BASE_URL:-https://flow.banlv-ai.com}"
|
||||||
base_url="${base_url%/}"
|
base_url="${base_url%/}"
|
||||||
notify_url="$base_url/requirements/$requirement_id/development-complete"
|
chat_id="$(generate_chat_id)"
|
||||||
|
|
||||||
deploy_env="${DEPLOY_ENV:-unknown}"
|
deploy_env="${DEPLOY_ENV:-unknown}"
|
||||||
image_tag="${IMAGE_VERSION_TAG:-unknown}"
|
image_tag="${IMAGE_VERSION_TAG:-unknown}"
|
||||||
job_status="${ACTION_JOB_STATUS:-unknown}"
|
job_status="${ACTION_JOB_STATUS:-unknown}"
|
||||||
workflow_name="${GITHUB_WORKFLOW:-Docker Image}"
|
workflow_name="${GITHUB_WORKFLOW:-Docker Image}"
|
||||||
repository="${GITHUB_REPOSITORY:-cozsweet-frontend-nextjs}"
|
repository="${GITHUB_REPOSITORY:-cozsweet-frontend-nextjs}"
|
||||||
result="$(normalize_job_result "$job_status")"
|
|
||||||
|
|
||||||
run_url=""
|
run_url=""
|
||||||
if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
|
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}"
|
run_url="${GITHUB_SERVER_URL:-unknown}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export FLOW_NOTIFY_OPERATOR="gitea"
|
export ACTION_NOTIFY_MESSAGE="$(cat <<EOF
|
||||||
export FLOW_NOTIFY_SOURCE="gitea"
|
|
||||||
export FLOW_NOTIFY_RESULT="$result"
|
|
||||||
export FLOW_NOTIFY_SUMMARY="$(cat <<EOF
|
|
||||||
Gitea workflow finished.
|
Gitea workflow finished.
|
||||||
|
|
||||||
Workflow: $workflow_name
|
Workflow: $workflow_name
|
||||||
@@ -98,19 +77,21 @@ EOF
|
|||||||
|
|
||||||
payload="$(node <<'NODE'
|
payload="$(node <<'NODE'
|
||||||
const payload = {
|
const payload = {
|
||||||
operator: process.env.FLOW_NOTIFY_OPERATOR,
|
message: process.env.ACTION_NOTIFY_MESSAGE,
|
||||||
result: process.env.FLOW_NOTIFY_RESULT,
|
|
||||||
summary: process.env.FLOW_NOTIFY_SUMMARY,
|
|
||||||
source: process.env.FLOW_NOTIFY_SOURCE,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
process.stdout.write(JSON.stringify(payload));
|
process.stdout.write(JSON.stringify(payload));
|
||||||
NODE
|
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
|
if [ "${FLOW_NOTIFY_DRY_RUN:-}" = "1" ]; then
|
||||||
printf 'Flow notification dry run URL: %s\n' "$notify_url"
|
printf 'Action notification dry run URL: %s\n' "$notify_url"
|
||||||
printf 'Flow notification dry run payload: %s\n' "$payload"
|
printf 'Action notification dry run payload: %s\n' "$payload"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -118,8 +99,8 @@ if ! curl -fsS \
|
|||||||
-X POST "$notify_url" \
|
-X POST "$notify_url" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "$payload" >/dev/null; then
|
-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
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Sent flow notification for $requirement_id."
|
echo "Sent action notification for $chat_id."
|
||||||
|
|||||||
Reference in New Issue
Block a user