diff --git a/repair_agent/agent/core.py b/repair_agent/agent/core.py index 3a08d6a..98b66d8 100644 --- a/repair_agent/agent/core.py +++ b/repair_agent/agent/core.py @@ -204,13 +204,18 @@ class RepairEngine: modified_files=modified_files, diff=diff, )) - # 自动提交并推送(仅在 Git 启用时) + # 自动提交、合并到 main 并推送(仅在 Git 启用时) if git_enabled and auto_commit and modified_files and git_manager: bug_ids = ", ".join([f"#{b.id}" for b in bugs]) git_manager.commit(f"fix: auto repair bugs {bug_ids}") logger.info("代码已提交") git_manager.push() - logger.info("代码已推送到远程") + logger.info("fix 分支已推送") + # 合并 fix 分支到 main 并推送,触发 CI/CD + if git_manager.merge_to_main_and_push(): + logger.info("已合并到 main 并推送") + else: + logger.warning("合并到 main 失败,请手动合并") elif not git_enabled and auto_commit: logger.info("未配置 GitHub 仓库,跳过自动提交") diff --git a/repair_agent/agent/git_manager.py b/repair_agent/agent/git_manager.py index 44d6d36..9f7d47a 100644 --- a/repair_agent/agent/git_manager.py +++ b/repair_agent/agent/git_manager.py @@ -172,6 +172,40 @@ class GitManager: logger.error(f"推送失败: {e}") return False + def merge_to_main_and_push(self) -> bool: + """将当前 fix 分支合并到 main 并推送,然后删除 fix 分支""" + if not self.repo: + return False + + try: + fix_branch = self.repo.active_branch.name + + # 切换到 main(或 master) + main_name = "main" if "main" in self.repo.heads else "master" + self.repo.heads[main_name].checkout() + + # 合并 fix 分支 + self.repo.git.merge(fix_branch) + logger.info(f"合并 {fix_branch} → {main_name}") + + # 推送 main + origin = self.repo.remotes.origin + origin.push(refspec=f"{main_name}:{main_name}") + logger.info(f"推送 {main_name} 成功") + + # 删除本地和远程 fix 分支 + self.repo.git.branch("-d", fix_branch) + try: + origin.push(refspec=f":{fix_branch}") + logger.info(f"删除远程分支 {fix_branch}") + except Exception: + pass # 远程分支可能不存在 + + return True + except Exception as e: + logger.error(f"合并到 main 失败: {e}") + return False + def reset_hard(self) -> bool: """重置所有更改""" if not self.repo: