Spaces:
Runtime error
Runtime error
| import os | |
| import logging | |
| from github import Github | |
| log = logging.getLogger("core") | |
| class GitHubSyncer: | |
| def __init__(self, repo_name: str, access_token: str): | |
| self.repo_name = repo_name | |
| self.access_token = access_token | |
| if self.access_token: | |
| self.gh = Github(self.access_token) | |
| try: | |
| self.repo = self.gh.get_repo(self.repo_name) | |
| log.info(f"Connected to GitHub repository: {self.repo_name}") | |
| except Exception as e: | |
| log.error(f"Failed to connect to GitHub repo {self.repo_name}: {e}") | |
| self.repo = None | |
| else: | |
| log.warning("GITHUB_TOKEN not provided. GitHub sync will be disabled.") | |
| self.repo = None | |
| def sync_file(self, file_path: str, content: str, commit_message: str = "Auto-update by AI Agent"): | |
| if not self.repo: | |
| log.warning(f"Skipping GitHub sync for {file_path}: No repository connection.") | |
| return | |
| # Windows yo'lakchalarini GitHub uchun to'g'rilash (/) | |
| git_path = file_path.replace("\\", "/") | |
| try: | |
| # Fayl allaqachon GitHub-da bormi yoki yo'qligini tekshirish | |
| try: | |
| contents = self.repo.get_contents(git_path, ref="main") | |
| # Fayl bor bo'lsa, yangilaymiz (update) | |
| self.repo.update_file( | |
| path=git_path, | |
| message=commit_message, | |
| content=content, | |
| sha=contents.sha, | |
| branch="main" | |
| ) | |
| log.info(f"Updated existing file on GitHub: {git_path}") | |
| except Exception: | |
| # Fayl topilmasa, yangi yaratamiz (create) | |
| self.repo.create_file( | |
| path=git_path, | |
| message=commit_message, | |
| content=content, | |
| branch="main" | |
| ) | |
| log.info(f"Created new file on GitHub: {git_path}") | |
| except Exception as e: | |
| log.error(f"Failed to sync file {git_path} to GitHub: {e}") | |
| raise e |