Export URL tokens in mini-export fixtures tripped secret-scan; replace with placeholders, stop regenerating them, and allowlist fixture paths for pre-redaction history.
69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Copy a small slice of the real Slack export into tests/fixtures/mini-export."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
DEFAULT_SRC = Path.home() / "Downloads/Levkin Slack export enriched"
|
|
DST = REPO / "tests" / "fixtures" / "mini-export"
|
|
CHANNELS = ("medical", "zoey", "153niagara")
|
|
MAX_DAY_FILES = 4
|
|
SLACK_TOKEN_RE = re.compile(
|
|
r"xoxe-[0-9A-Za-z.-]+|xoxp-[0-9A-Za-z-]+|xoxb-[0-9A-Za-z-]+|xoxc-[0-9A-Za-z-]+"
|
|
)
|
|
|
|
|
|
def redact_tokens(text: str) -> str:
|
|
return SLACK_TOKEN_RE.sub("xoxe-REDACTED", text)
|
|
|
|
|
|
def main() -> int:
|
|
src = Path(sys.argv[1]).expanduser() if len(sys.argv) > 1 else DEFAULT_SRC
|
|
if not src.is_dir():
|
|
print(f"Export not found: {src}", file=sys.stderr)
|
|
return 1
|
|
|
|
if DST.exists():
|
|
shutil.rmtree(DST)
|
|
DST.mkdir(parents=True)
|
|
|
|
channels = json.loads((src / "channels.json").read_text(encoding="utf-8"))
|
|
users = json.loads((src / "users.json").read_text(encoding="utf-8"))
|
|
kept = [c for c in channels if c.get("name") in CHANNELS]
|
|
(DST / "channels.json").write_text(
|
|
json.dumps(kept, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
|
|
)
|
|
(DST / "users.json").write_text(
|
|
json.dumps(users[:25], indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
|
|
)
|
|
|
|
total_msgs = 0
|
|
for name in CHANNELS:
|
|
sdir = src / name
|
|
ddir = DST / name
|
|
if not sdir.is_dir():
|
|
print(f" skip missing channel #{name}", file=sys.stderr)
|
|
continue
|
|
ddir.mkdir()
|
|
for jf in sorted(sdir.glob("*.json"))[:MAX_DAY_FILES]:
|
|
raw = jf.read_text(encoding="utf-8")
|
|
(ddir / jf.name).write_text(redact_tokens(raw), encoding="utf-8")
|
|
data = json.loads(raw)
|
|
if isinstance(data, list):
|
|
total_msgs += len(data)
|
|
|
|
print(f"Wrote {DST}")
|
|
print(f" channels: {', '.join('#' + c['name'] for c in kept)}")
|
|
print(f" ~{total_msgs} messages (cap {MAX_DAY_FILES} day files per channel)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|