mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
@coderabbitai ignore ## Summary - **Email notifications via SendGrid** — replaces Pushbullet as the primary notification channel for the GDrive validator cloud function. Pushbullet kept as secondary fallback. - **Post-job trigger** — the backend orchestrator now triggers GDrive validation after each job that uploads to the public share, catching duplicates within minutes instead of up to 24 hours. - **Manual CLI** — `python scripts/check_public_share.py` for on-demand validation checks with formatted output and `--json` flag. - **Comprehensive docs** — `docs/GDRIVE-VALIDATOR.md` with architecture, configuration, troubleshooting, and operational reference. Motivated by the NOMAD-1271 duplicate brand code incident where daily-only checks left a duplicate undetected for ~24 hours. ## Changes | File | Change | |------|--------| | `infrastructure/functions/gdrive_validator/main.py` | Add `send_email_notification()` via SendGrid, unified `send_notification()` sends both email + Pushbullet | | `infrastructure/functions/gdrive_validator/requirements.txt` | Add `sendgrid>=6.0.0` | | `infrastructure/functions/gdrive_validator/deploy.sh` | Add SendGrid secret + email env vars | | `infrastructure/__main__.py` | Add SendGrid secret to function, backend SA invoker IAM (Cloud Functions + Cloud Run) | | `infrastructure/modules/secrets.py` | Add `gdrive-validator-url` secret | | `.github/workflows/ci.yml` | Add `GDRIVE_VALIDATOR_URL` to Cloud Run deploy secrets | | `backend/services/gdrive_validator_client.py` | **New**: HTTP client with OIDC auth, fire-and-forget pattern | | `backend/workers/video_worker_orchestrator.py` | Add Stage 6: post-distribution GDrive validation (only when `gdrive_folder_id` set) | | `scripts/check_public_share.py` | **New**: CLI tool for manual validation checks | | `docs/GDRIVE-VALIDATOR.md` | **New**: Comprehensive validator documentation | | `pyproject.toml` | Version bump to 0.119.9 | ## Already deployed - Pulumi infrastructure applied (IAM bindings, secrets, function config) - Cloud Function redeployed with SendGrid support - `gdrive-validator-url` secret set in Secret Manager - Tested via `check_public_share.py` — validated 3,608 files, email sent successfully ## Test plan - [x] `make test` — 170 passed, 3 skipped (1 pre-existing failure in credit-enforcement unrelated to this PR) - [x] `pulumi preview` — clean, 3 creates + 1 update - [x] `pulumi up` — applied successfully - [x] Cloud Function deployed and tested end-to-end - [x] `python scripts/check_public_share.py` — formatted output works - [x] `python scripts/check_public_share.py --json` — JSON output works - [x] Email received at gen@nomadkaraoke.com (notification_sent: true) - [ ] Post-job trigger: will verify on next job completion (backend needs redeploy via CI merge) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
153 lines
4.6 KiB
Python
Executable File
153 lines
4.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Check the public Google Drive share for issues.
|
|
|
|
Invokes the gdrive-validator Cloud Function and displays results.
|
|
|
|
Usage:
|
|
python scripts/check_public_share.py # Formatted output
|
|
python scripts/check_public_share.py --json # Raw JSON output
|
|
|
|
Exit codes:
|
|
0 = all clear
|
|
1 = issues found
|
|
2 = error
|
|
"""
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
PROJECT_ID = "nomadkaraoke"
|
|
REGION = "us-central1"
|
|
FUNCTION_NAME = "gdrive-validator"
|
|
|
|
|
|
def call_cloud_function() -> dict:
|
|
"""Call the GDrive validator Cloud Function via gcloud."""
|
|
result = subprocess.run(
|
|
[
|
|
"gcloud", "functions", "call", FUNCTION_NAME,
|
|
f"--region={REGION}",
|
|
f"--project={PROJECT_ID}",
|
|
"--format=json",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=120,
|
|
)
|
|
|
|
if result.returncode != 0:
|
|
raise RuntimeError(f"gcloud call failed: {result.stderr.strip()}")
|
|
|
|
# gcloud functions call --format=json returns the response as a double-encoded JSON string
|
|
parsed = json.loads(result.stdout)
|
|
|
|
# If parsed is a string, it's the double-encoded function response
|
|
if isinstance(parsed, str):
|
|
return json.loads(parsed)
|
|
|
|
# If it's a dict with a "result" field (older gcloud versions), unwrap it
|
|
if isinstance(parsed, dict) and "result" in parsed:
|
|
inner = parsed["result"]
|
|
return json.loads(inner) if isinstance(inner, str) else inner
|
|
|
|
return parsed
|
|
|
|
|
|
def print_formatted(data: dict):
|
|
"""Print validation results in a human-readable format."""
|
|
status = data.get("status", "unknown")
|
|
|
|
if status == "ok":
|
|
summary = data.get("summary", {})
|
|
print("✅ All clear - no issues found\n")
|
|
print(f" MP4: {summary.get('mp4', 0):,} files")
|
|
print(f" MP4-720p:{summary.get('mp4_720p', 0):,} files")
|
|
print(f" CDG: {summary.get('cdg', 0):,} files")
|
|
print(f" Total: {summary.get('total', 0):,} files")
|
|
return
|
|
|
|
if status == "issues_found":
|
|
issues = data.get("issues", {})
|
|
|
|
# Duplicates
|
|
dupes = issues.get("duplicates", {})
|
|
if dupes:
|
|
print("⚠️ DUPLICATES:")
|
|
for folder, seq_map in dupes.items():
|
|
for seq_num, filenames in seq_map.items():
|
|
print(f" {folder}: NOMAD-{int(seq_num):04d} appears {len(filenames)} times")
|
|
for f in filenames[:3]:
|
|
print(f" - {f}")
|
|
if len(filenames) > 3:
|
|
print(f" - ...and {len(filenames) - 3} more")
|
|
print()
|
|
|
|
# Invalid filenames
|
|
invalid = issues.get("invalid_filenames", {})
|
|
if invalid:
|
|
print("⚠️ INVALID FILENAMES:")
|
|
for folder, filenames in invalid.items():
|
|
for f in filenames[:5]:
|
|
print(f" {folder}: {f}")
|
|
if len(filenames) > 5:
|
|
print(f" ...and {len(filenames) - 5} more in {folder}")
|
|
print()
|
|
|
|
# Gaps
|
|
gaps = issues.get("gaps", {})
|
|
if gaps:
|
|
print("⚠️ SEQUENCE GAPS:")
|
|
for folder, missing in gaps.items():
|
|
if len(missing) <= 10:
|
|
print(f" {folder}: missing {', '.join(map(str, missing))}")
|
|
else:
|
|
print(f" {folder}: {len(missing)} gaps (first 5: {', '.join(map(str, missing[:5]))}...)")
|
|
print()
|
|
|
|
# Summary
|
|
summary = issues.get("summary", {})
|
|
print(f"Checked: {summary.get('mp4', 0)} MP4, {summary.get('mp4_720p', 0)} 720p, {summary.get('cdg', 0)} CDG files")
|
|
return
|
|
|
|
if status == "warning":
|
|
print(f"⚠️ {data.get('message', 'Unknown warning')}")
|
|
return
|
|
|
|
print(f"❌ Unexpected status: {status}")
|
|
print(f" {data.get('message', '')}")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Check public GDrive share for issues")
|
|
parser.add_argument("--json", action="store_true", help="Output raw JSON")
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
data = call_cloud_function()
|
|
except Exception as e:
|
|
if args.json:
|
|
print(json.dumps({"status": "error", "message": str(e)}))
|
|
else:
|
|
print(f"❌ Error: {e}")
|
|
sys.exit(2)
|
|
|
|
if args.json:
|
|
print(json.dumps(data, indent=2))
|
|
else:
|
|
print_formatted(data)
|
|
|
|
status = data.get("status", "error")
|
|
if status == "ok":
|
|
sys.exit(0)
|
|
elif status == "issues_found":
|
|
sys.exit(1)
|
|
else:
|
|
sys.exit(2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|