SQL-Eval / utils /common.py
Jun-Min Lee
init
7b45c3b
Raw
History Blame Contribute Delete
1.74 kB
import os
import json
from github import Github
from dotenv import load_dotenv
LABELS_DIR = "labels"
def _get_repo():
load_dotenv(override=True)
repo_name = os.getenv("GITHUB_REPO")
if not repo_name:
raise RuntimeError("GITHUB_REPO env var is not set")
g = Github(os.getenv("GITHUB_TOKEN"))
return g.get_repo(repo_name)
def list_completed_keys() -> set:
"""Session keys ("{pid}_{sid}") that already have a submitted label file
in the GitHub repo's labels/ directory. Returns an empty set (rather than
raising) if the repo/labels dir isn't reachable yet, so the app still
works before it's configured or if the directory doesn't exist yet."""
try:
repo = _get_repo()
contents = repo.get_contents(LABELS_DIR, ref="main")
return {os.path.splitext(c.name)[0] for c in contents}
except Exception:
return set()
def upload_label(key: str, payload: dict):
"""Create labels/{key}.json in the GitHub repo. Uses create (not update)
so a second submission for an already-completed session fails loudly
instead of silently overwriting someone else's labels."""
path = f"{LABELS_DIR}/{key}.json"
content = json.dumps(payload, ensure_ascii=False, indent=2)
try:
repo = _get_repo()
try:
repo.get_contents(path, ref="main")
return False, "⚠️ 이미 λ‹€λ₯Έ μ‚¬λžŒμ΄ 이 μ„Έμ…˜μ„ μ™„λ£Œν–ˆμŠ΅λ‹ˆλ‹€. μƒˆ μ„Έμ…˜μ„ λ°›μ•„μ£Όμ„Έμš”."
except Exception:
pass
repo.create_file(path, f"feat: add labels for {key}", content, branch="main")
return True, "βœ… 제좜 μ™„λ£Œ! κ°μ‚¬ν•©λ‹ˆλ‹€."
except Exception as e:
return False, f"❌ μ—…λ‘œλ“œ μ‹€νŒ¨: {e}"