reposync/issue_sync.py

159 lines
6.0 KiB
Python

# coding: utf-8
"""
Issue同步脚本
实现Issue在不同平台间的同步功能
"""
import asyncio
import json
import time
from datetime import datetime
from typing import Dict, List, Optional
class IssueSyncManager:
"""Issue同步管理器"""
def __init__(self):
self.sync_jobs = []
self.sync_logs = []
async def sync_issue_from_github_to_gitee(self, project_name: str, github_token: str, gitee_token: str):
"""从GitHub同步Issue到Gitee"""
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 开始同步项目 {project_name} 的Issue从GitHub到Gitee")
try:
# 模拟从GitHub获取Issue列表
github_issues = await self._fetch_github_issues(project_name, github_token)
print(f"从GitHub获取到 {len(github_issues)} 个Issue")
# 模拟同步到Gitee
for issue in github_issues:
await self._sync_single_issue_to_gitee(issue, gitee_token)
await asyncio.sleep(1) # 避免请求过于频繁
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 项目 {project_name} 的Issue同步完成")
except Exception as e:
print(f"同步失败: {str(e)}")
self._log_sync_error(project_name, str(e))
async def sync_issue_from_gitee_to_github(self, project_name: str, gitee_token: str, github_token: str):
"""从Gitee同步Issue到GitHub"""
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 开始同步项目 {project_name} 的Issue从Gitee到GitHub")
try:
# 模拟从Gitee获取Issue列表
gitee_issues = await self._fetch_gitee_issues(project_name, gitee_token)
print(f"从Gitee获取到 {len(gitee_issues)} 个Issue")
# 模拟同步到GitHub
for issue in gitee_issues:
await self._sync_single_issue_to_github(issue, github_token)
await asyncio.sleep(1) # 避免请求过于频繁
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 项目 {project_name} 的Issue同步完成")
except Exception as e:
print(f"同步失败: {str(e)}")
self._log_sync_error(project_name, str(e))
async def _fetch_github_issues(self, project_name: str, token: str) -> List[Dict]:
"""模拟从GitHub获取Issue列表"""
# 这里应该实现真实的GitHub API调用
# 目前返回模拟数据
return [
{
"id": 1,
"title": "Bug: 登录功能异常",
"description": "用户登录时出现500错误",
"state": "open",
"labels": ["bug", "high-priority"],
"assignee": "developer1",
"author": "user1"
},
{
"id": 2,
"title": "Feature: 添加用户管理功能",
"description": "需要添加用户增删改查功能",
"state": "open",
"labels": ["enhancement"],
"assignee": "developer2",
"author": "user2"
}
]
async def _fetch_gitee_issues(self, project_name: str, token: str) -> List[Dict]:
"""模拟从Gitee获取Issue列表"""
# 这里应该实现真实的Gitee API调用
# 目前返回模拟数据
return [
{
"id": 101,
"title": "Bug: 数据导出功能异常",
"description": "导出Excel时格式错误",
"state": "open",
"labels": ["bug"],
"assignee": "developer3",
"author": "user3"
}
]
async def _sync_single_issue_to_gitee(self, issue: Dict, token: str):
"""同步单个Issue到Gitee"""
print(f"正在同步Issue '{issue['title']}' 到Gitee...")
# 这里应该实现真实的Gitee API调用
await asyncio.sleep(0.5) # 模拟API调用时间
print(f"Issue '{issue['title']}' 同步到Gitee成功")
async def _sync_single_issue_to_github(self, issue: Dict, token: str):
"""同步单个Issue到GitHub"""
print(f"正在同步Issue '{issue['title']}' 到GitHub...")
# 这里应该实现真实的GitHub API调用
await asyncio.sleep(0.5) # 模拟API调用时间
print(f"Issue '{issue['title']}' 同步到GitHub成功")
def _log_sync_error(self, project_name: str, error_message: str):
"""记录同步错误日志"""
log_entry = {
"timestamp": datetime.now().isoformat(),
"project": project_name,
"type": "error",
"message": error_message
}
self.sync_logs.append(log_entry)
print(f"错误日志已记录: {log_entry}")
def get_sync_logs(self) -> List[Dict]:
"""获取同步日志"""
return self.sync_logs
async def main():
"""主函数"""
print("=== Issue同步工具启动 ===")
# 创建同步管理器
sync_manager = IssueSyncManager()
# 配置同步参数
project_name = "test-project"
github_token = "your_github_token"
gitee_token = "your_gitee_token"
# 执行同步任务
print("1. 从GitHub同步到Gitee")
await sync_manager.sync_issue_from_github_to_gitee(project_name, github_token, gitee_token)
print("\n2. 从Gitee同步到GitHub")
await sync_manager.sync_issue_from_gitee_to_github(project_name, gitee_token, github_token)
# 显示同步日志
print("\n=== 同步日志 ===")
logs = sync_manager.get_sync_logs()
for log in logs:
print(f"[{log['timestamp']}] {log['type'].upper()}: {log['message']}")
print("\n=== Issue同步工具运行完成 ===")
if __name__ == "__main__":
# 运行异步主函数
asyncio.run(main())