Spaces:
Paused
Paused
| 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}" | |