From 5f386c80c596cdbe40c4c512f47e3cd5e03604e6 Mon Sep 17 00:00:00 2001 From: shuaiplus <2327005759@qq.com> Date: Fri, 6 Mar 2026 03:08:38 +0800 Subject: [PATCH] feat: refactor kv sync logic to use regex for R2 block replacement --- .github/workflows/sync-upstream.yml | 35 ++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index 7923b51..5c352bc 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -40,24 +40,29 @@ jobs: git checkout -B kv origin/main python - <<'PY' -from pathlib import Path + import re + from pathlib import Path -path = Path("wrangler.toml") -text = path.read_text(encoding="utf-8") -old = """[[r2_buckets]] -binding = "ATTACHMENTS" -bucket_name = "nodewarden-attachments" -""" -new = """[[kv_namespaces]] -binding = "ATTACHMENTS_KV" -id = "REPLACE_WITH_KV_NAMESPACE_ID" -""" + path = Path("wrangler.toml") + text = path.read_text(encoding="utf-8") -if old not in text: - raise SystemExit("Expected R2 block not found in wrangler.toml") + pattern = ( + r"\[\[r2_buckets\]\]\s*" + r'binding\s*=\s*"ATTACHMENTS"\s*' + r'bucket_name\s*=\s*"nodewarden-attachments"\s*' + ) + replacement = ( + '[[kv_namespaces]]\n' + 'binding = "ATTACHMENTS_KV"\n' + 'id = "REPLACE_WITH_KV_NAMESPACE_ID"\n' + ) -path.write_text(text.replace(old, new, 1), encoding="utf-8") -PY + new_text, count = re.subn(pattern, replacement, text, count=1) + if count == 0: + raise SystemExit("Expected R2 block not found in wrangler.toml") + + path.write_text(new_text, encoding="utf-8") + PY git add wrangler.toml git commit -m "chore(kv): sync from main" || echo "No changes"