wony617
commited on
Commit
Β·
b05265a
1
Parent(s):
a5bcbae
refactor(log): separate logging into GitHubLogger
Browse filesDecouple logging from GitHubPRAgent. Introduce logger/github_logger.py for appending JSONL logs to a dedicated repo/branch/file via env config. Wire workflow to use the new logger.
- agent/workflow.py +3 -2
- logger/github_logger.py +75 -0
agent/workflow.py
CHANGED
@@ -204,11 +204,12 @@ def generate_github_pr(
|
|
204 |
else:
|
205 |
toctree_status = f"\nπ **Toctree Update Failed:** β {toctree_result['message']}"
|
206 |
|
207 |
-
# Append full result JSON to GitHub
|
208 |
try:
|
209 |
import json
|
|
|
210 |
log_entry = json.dumps(result, ensure_ascii=False) + "\n"
|
211 |
-
log_res =
|
212 |
print(f"π Log append result: {log_res}")
|
213 |
except Exception as e:
|
214 |
print(f"β Failed to append PR log via GitHub API: {e}")
|
|
|
204 |
else:
|
205 |
toctree_status = f"\nπ **Toctree Update Failed:** β {toctree_result['message']}"
|
206 |
|
207 |
+
# Append full result JSON to dedicated GitHub logging repository (always)
|
208 |
try:
|
209 |
import json
|
210 |
+
from logger.github_logger import GitHubLogger
|
211 |
log_entry = json.dumps(result, ensure_ascii=False) + "\n"
|
212 |
+
log_res = GitHubLogger().append_jsonl(log_entry)
|
213 |
print(f"π Log append result: {log_res}")
|
214 |
except Exception as e:
|
215 |
print(f"β Failed to append PR log via GitHub API: {e}")
|
logger/github_logger.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
from typing import Optional
|
4 |
+
|
5 |
+
try:
|
6 |
+
from github import Github, GithubException
|
7 |
+
LIBS_OK = True
|
8 |
+
except ImportError:
|
9 |
+
LIBS_OK = False
|
10 |
+
|
11 |
+
class GitHubLogger:
|
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 or LOG_REPO_OWNER + LOG_REPO_NAME
|
17 |
+
- LOG_BRANCH (default: 'log_event')
|
18 |
+
- LOG_FILE_PATH (default: 'pr_success.log')
|
19 |
+
"""
|
20 |
+
|
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")
|
30 |
+
if repo_spec and "/" in repo_spec:
|
31 |
+
self.owner, self.repo_name = repo_spec.split("/", 1)
|
32 |
+
else:
|
33 |
+
self.owner = os.environ.get("LOG_REPO_OWNER")
|
34 |
+
self.repo_name = os.environ.get("LOG_REPO_NAME")
|
35 |
+
if not self.owner or not self.repo_name:
|
36 |
+
raise ValueError("Missing LOG_REPO or LOG_REPO_OWNER/LOG_REPO_NAME")
|
37 |
+
|
38 |
+
self.branch = os.environ.get("LOG_BRANCH", "log_event")
|
39 |
+
self.path = os.environ.get("LOG_FILE_PATH", "pr_success.log")
|
40 |
+
|
41 |
+
def _ensure_branch(self, repo):
|
42 |
+
try:
|
43 |
+
repo.get_branch(self.branch)
|
44 |
+
except GithubException as e:
|
45 |
+
if e.status == 404:
|
46 |
+
base = repo.get_branch(repo.default_branch)
|
47 |
+
repo.create_git_ref(ref=f"refs/heads/{self.branch}", sha=base.commit.sha)
|
48 |
+
else:
|
49 |
+
raise
|
50 |
+
|
51 |
+
def append_jsonl(self, jsonl_line: str, commit_message: str = "chore(log): append entry") -> str:
|
52 |
+
repo = self._client.get_repo(f"{self.owner}/{self.repo_name}")
|
53 |
+
self._ensure_branch(repo)
|
54 |
+
try:
|
55 |
+
existing = repo.get_contents(self.path, ref=self.branch)
|
56 |
+
existing_content = base64.b64decode(existing.content).decode("utf-8")
|
57 |
+
new_content = existing_content + jsonl_line
|
58 |
+
repo.update_file(
|
59 |
+
path=self.path,
|
60 |
+
message=commit_message,
|
61 |
+
content=new_content,
|
62 |
+
sha=existing.sha,
|
63 |
+
branch=self.branch,
|
64 |
+
)
|
65 |
+
return "SUCCESS: Log appended"
|
66 |
+
except GithubException as e:
|
67 |
+
if e.status == 404:
|
68 |
+
repo.create_file(
|
69 |
+
path=self.path,
|
70 |
+
message=commit_message,
|
71 |
+
content=jsonl_line,
|
72 |
+
branch=self.branch,
|
73 |
+
)
|
74 |
+
return "SUCCESS: Log file created and first entry appended"
|
75 |
+
raise
|