wony617
commited on
Commit
·
859119b
1
Parent(s):
7e4dd83
Add github token for rate limit handling
Browse files- example.env +1 -0
- logger/github_logger.py +3 -3
- translator/retriever.py +9 -3
example.env
CHANGED
@@ -13,5 +13,6 @@ HF_SPACE_NAME=
|
|
13 |
|
14 |
# Secrets for logging to Github
|
15 |
LOG_REPO=
|
|
|
16 |
LOG_BRANCH=
|
17 |
LOG_FILE_PATH=
|
|
|
13 |
|
14 |
# Secrets for logging to Github
|
15 |
LOG_REPO=
|
16 |
+
LOG_GITHUB_TOKEN=
|
17 |
LOG_BRANCH=
|
18 |
LOG_FILE_PATH=
|
logger/github_logger.py
CHANGED
@@ -12,7 +12,7 @@ class GitHubLogger:
|
|
12 |
"""Dedicated logger that appends JSONL entries to a GitHub repo/branch/file.
|
13 |
|
14 |
Env vars:
|
15 |
-
- GITHUB_TOKEN
|
16 |
- LOG_REPO (format: owner/repo)
|
17 |
- LOG_BRANCH (default: 'log_event')
|
18 |
- LOG_FILE_PATH (default: 'pr_success.log')
|
@@ -21,9 +21,9 @@ class GitHubLogger:
|
|
21 |
def __init__(self):
|
22 |
if not LIBS_OK:
|
23 |
raise ImportError("PyGithub not installed. Please install PyGithub.")
|
24 |
-
token = os.environ.get("GITHUB_TOKEN")
|
25 |
if not token:
|
26 |
-
raise ValueError("Missing GITHUB_TOKEN for logging")
|
27 |
self._client = Github(token)
|
28 |
|
29 |
repo_spec = os.environ.get("LOG_REPO")
|
|
|
12 |
"""Dedicated logger that appends JSONL entries to a GitHub repo/branch/file.
|
13 |
|
14 |
Env vars:
|
15 |
+
- LOG_GITHUB_TOKEN (fallback: GITHUB_TOKEN)
|
16 |
- LOG_REPO (format: owner/repo)
|
17 |
- LOG_BRANCH (default: 'log_event')
|
18 |
- LOG_FILE_PATH (default: 'pr_success.log')
|
|
|
21 |
def __init__(self):
|
22 |
if not LIBS_OK:
|
23 |
raise ImportError("PyGithub not installed. Please install PyGithub.")
|
24 |
+
token = os.environ.get("LOG_GITHUB_TOKEN") or os.environ.get("GITHUB_TOKEN")
|
25 |
if not token:
|
26 |
+
raise ValueError("Missing LOG_GITHUB_TOKEN or GITHUB_TOKEN for logging")
|
27 |
self._client = Github(token)
|
28 |
|
29 |
repo_spec = os.environ.get("LOG_REPO")
|
translator/retriever.py
CHANGED
@@ -14,13 +14,17 @@ def get_github_repo_files(project: str = "transformers"):
|
|
14 |
"""
|
15 |
config = get_project_config(project)
|
16 |
|
17 |
-
# Add GitHub token if available to avoid rate limiting
|
18 |
headers = {}
|
19 |
github_token = os.environ.get("GITHUB_TOKEN")
|
20 |
if github_token:
|
21 |
headers["Authorization"] = f"token {github_token}"
|
22 |
|
23 |
response = requests.get(config.api_url, headers=headers)
|
|
|
|
|
|
|
|
|
24 |
|
25 |
data = response.json()
|
26 |
all_items = data.get("tree", [])
|
@@ -48,7 +52,7 @@ def get_github_issue_open_pr(project: str = "transformers", lang: str = "ko"):
|
|
48 |
"Accept": "application/vnd.github+json",
|
49 |
}
|
50 |
|
51 |
-
# Add GitHub token if available to avoid rate limiting
|
52 |
github_token = os.environ.get("GITHUB_TOKEN")
|
53 |
if github_token:
|
54 |
headers["Authorization"] = f"token {github_token}"
|
@@ -62,7 +66,9 @@ def get_github_issue_open_pr(project: str = "transformers", lang: str = "ko"):
|
|
62 |
url = f"https://api.github.com/repos/{repo_path}/pulls?state=open&page={page}&per_page={per_page}"
|
63 |
response = requests.get(url, headers=headers)
|
64 |
|
65 |
-
if response.status_code
|
|
|
|
|
66 |
raise Exception(f"GitHub API error: {response.status_code} {response.text}")
|
67 |
|
68 |
page_prs = response.json()
|
|
|
14 |
"""
|
15 |
config = get_project_config(project)
|
16 |
|
17 |
+
# Add GitHub token if available to avoid rate limiting (optional)
|
18 |
headers = {}
|
19 |
github_token = os.environ.get("GITHUB_TOKEN")
|
20 |
if github_token:
|
21 |
headers["Authorization"] = f"token {github_token}"
|
22 |
|
23 |
response = requests.get(config.api_url, headers=headers)
|
24 |
+
|
25 |
+
# Handle rate limit with helpful message
|
26 |
+
if response.status_code == 403 and "rate limit" in response.text.lower():
|
27 |
+
raise Exception(f"GitHub API rate limit exceeded. To avoid this, set GITHUB_TOKEN in your environment or provide a GitHub token in the UI. Details: {response.text}")
|
28 |
|
29 |
data = response.json()
|
30 |
all_items = data.get("tree", [])
|
|
|
52 |
"Accept": "application/vnd.github+json",
|
53 |
}
|
54 |
|
55 |
+
# Add GitHub token if available to avoid rate limiting (optional)
|
56 |
github_token = os.environ.get("GITHUB_TOKEN")
|
57 |
if github_token:
|
58 |
headers["Authorization"] = f"token {github_token}"
|
|
|
66 |
url = f"https://api.github.com/repos/{repo_path}/pulls?state=open&page={page}&per_page={per_page}"
|
67 |
response = requests.get(url, headers=headers)
|
68 |
|
69 |
+
if response.status_code == 403 and "rate limit" in response.text.lower():
|
70 |
+
raise Exception(f"GitHub API rate limit exceeded. To avoid this, set GITHUB_TOKEN in your environment or provide a GitHub token in the UI. Details: {response.text}")
|
71 |
+
elif response.status_code != 200:
|
72 |
raise Exception(f"GitHub API error: {response.status_code} {response.text}")
|
73 |
|
74 |
page_prs = response.json()
|