Submit code using git workflow: write -> commit -> push -> PULL in codespace -> open -> submit
(self, code_content: str, filename: Optional[str] = None)
| 589 | # ==================== 原有的单个提交方法(保留兼容性) ==================== |
| 590 | |
| 591 | async def submit_code(self, code_content: str, filename: Optional[str] = None) -> Dict[str, Any]: |
| 592 | """Submit code using git workflow: write -> commit -> push -> PULL in codespace -> open -> submit""" |
| 593 | |
| 594 | target_file = filename or "code.py" |
| 595 | logger.info(f"| 🔍 Preparing evaluation for {target_file}...") |
| 596 | |
| 597 | # 1. Write code to file in repo (Local) |
| 598 | file_path = os.path.join(self.repo_path, target_file) |
| 599 | |
| 600 | logger.info(f"| 🔍 Writing code to {file_path}...") |
| 601 | os.makedirs(os.path.dirname(file_path), exist_ok=True) |
| 602 | with open(file_path, 'w', encoding='utf-8') as f: |
| 603 | f.write(code_content) |
| 604 | |
| 605 | # 2. Git add, commit, and push (Local) |
| 606 | logger.info("| 🔍 Committing and pushing to GitHub...") |
| 607 | try: |
| 608 | # Configure git user (required for commit) |
| 609 | await self._run_git_command(['config', 'user.name', self.username], self.repo_path) |
| 610 | await self._run_git_command(['config', 'user.email', f'{self.username}@users.noreply.github.com'], self.repo_path) |
| 611 | |
| 612 | # Git add |
| 613 | await self._run_git_command(['add', target_file], self.repo_path) |
| 614 | |
| 615 | # Check if there are changes to commit |
| 616 | status_out = await self._run_git_command(['status', '--porcelain'], self.repo_path) |
| 617 | |
| 618 | if status_out.strip(): |
| 619 | # Git commit |
| 620 | await self._run_git_command(['commit', '-m', f'Add solution: {target_file}'], self.repo_path) |
| 621 | |
| 622 | # Git push |
| 623 | await self._run_git_command(['push'], self.repo_path) |
| 624 | logger.info("| 🔍 Successfully pushed to GitHub") |
| 625 | else: |
| 626 | logger.info("| 🔍 No changes to commit (file unchanged)") |
| 627 | except Exception as e: |
| 628 | logger.warning(f"| ⚠️ Git operation failed: {str(e)}") |
| 629 | # Continue anyway, file might already be in repo or push might not be needed |
| 630 | |
| 631 | # 3. Wait a bit for GitHub to sync |
| 632 | await asyncio.sleep(3) |
| 633 | |
| 634 | # 4. [修改] Pull latest code inside Codespace FIRST |
| 635 | # 只有先 Pull 把文件拉下来,后面的 Open 才能找到文件 |
| 636 | await self._sync_codespace_repo() |
| 637 | |
| 638 | # 5. [修改] Open file in Codespace |
| 639 | await self._open_editor_file(target_file) |
| 640 | await asyncio.sleep(2) |
| 641 | |
| 642 | # 6. Trigger LeetCode submission |
| 643 | logger.info("| 🔍 Triggering LeetCode submission...") |
| 644 | await self.page.keyboard.press("Control+Shift+P") |
| 645 | await asyncio.sleep(1) |
| 646 | await self.page.keyboard.type("LeetCode: Submit to LeetCode") |
| 647 | await asyncio.sleep(1) |
| 648 | await self.page.keyboard.press("Enter") |
nothing calls this directly
no test coverage detected