File size: 1,741 Bytes
7b45c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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}"