reposync/issue_sync/examples/gitlink_gitee_pr_sync_examp...

223 lines
7.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GitLink-Gitee PR同步使用示例
演示如何使用GitLink-Gitee PR同步功能
"""
import sys
import os
import json
from datetime import datetime
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from issue_sync.service.gitlink_gitee_pr_sync_service import GitLinkGiteePRSyncService
def example_basic_sync():
"""
基本同步示例
"""
print("=== GitLink-Gitee PR同步基本示例 ===")
# 配置信息
config = {
'gitlink_owner': 'your_gitlink_owner',
'gitlink_repo': 'your_gitlink_repo',
'gitlink_cookie': 'autologin_trustie=your_cookie_here',
'gitee_owner': 'your_gitee_owner',
'gitee_repo': 'your_gitee_repo',
'gitee_token': 'your_gitee_token_here',
'sync_direction': 'bidirectional', # 双向同步
'sync_comments': True # 同步评论
}
try:
# 创建同步服务
sync_service = GitLinkGiteePRSyncService(config)
# 执行同步
sync_service.sync_pull_requests()
# 获取同步状态
status = sync_service.get_sync_status()
print(f"同步状态: {json.dumps(status, ensure_ascii=False, indent=2)}")
except Exception as e:
print(f"同步失败: {str(e)}")
def example_unidirectional_sync():
"""
单向同步示例
"""
print("\n=== GitLink-Gitee PR单向同步示例 ===")
# 从GitLink同步到Gitee
config_gitlink_to_gitee = {
'gitlink_owner': 'your_gitlink_owner',
'gitlink_repo': 'your_gitlink_repo',
'gitlink_cookie': 'autologin_trustie=your_cookie_here',
'gitee_owner': 'your_gitee_owner',
'gitee_repo': 'your_gitee_repo',
'gitee_token': 'your_gitee_token_here',
'sync_direction': 'gitlink_to_gitee', # 只从GitLink同步到Gitee
'sync_comments': True
}
try:
sync_service = GitLinkGiteePRSyncService(config_gitlink_to_gitee)
sync_service.sync_pull_requests()
print("GitLink到Gitee同步完成")
except Exception as e:
print(f"GitLink到Gitee同步失败: {str(e)}")
# 从Gitee同步到GitLink
config_gitee_to_gitlink = {
'gitlink_owner': 'your_gitlink_owner',
'gitlink_repo': 'your_gitlink_repo',
'gitlink_cookie': 'autologin_trustie=your_cookie_here',
'gitee_owner': 'your_gitee_owner',
'gitee_repo': 'your_gitee_repo',
'gitee_token': 'your_gitee_token_here',
'sync_direction': 'gitee_to_gitlink', # 只从Gitee同步到GitLink
'sync_comments': False # 不同步评论
}
try:
sync_service = GitLinkGiteePRSyncService(config_gitee_to_gitlink)
sync_service.sync_pull_requests()
print("Gitee到GitLink同步完成")
except Exception as e:
print(f"Gitee到GitLink同步失败: {str(e)}")
def example_with_error_handling():
"""
带错误处理的同步示例
"""
print("\n=== GitLink-Gitee PR同步错误处理示例 ===")
config = {
'gitlink_owner': 'invalid_owner',
'gitlink_repo': 'invalid_repo',
'gitlink_cookie': 'invalid_cookie',
'gitee_owner': 'invalid_owner',
'gitee_repo': 'invalid_repo',
'gitee_token': 'invalid_token',
'sync_direction': 'bidirectional',
'sync_comments': True
}
try:
sync_service = GitLinkGiteePRSyncService(config)
sync_service.sync_pull_requests()
except Exception as e:
print(f"预期的错误: {str(e)}")
print("错误处理正常工作")
def example_test_connection():
"""
测试连接示例
"""
print("\n=== GitLink-Gitee PR连接测试示例 ===")
config = {
'gitlink_owner': 'your_gitlink_owner',
'gitlink_repo': 'your_gitlink_repo',
'gitlink_cookie': 'autologin_trustie=your_cookie_here',
'gitee_owner': 'your_gitee_owner',
'gitee_repo': 'your_gitee_repo',
'gitee_token': 'your_gitee_token_here',
'sync_direction': 'bidirectional',
'sync_comments': True
}
try:
sync_service = GitLinkGiteePRSyncService(config)
# 测试GitLink连接
print("测试GitLink连接...")
gitlink_prs = sync_service.gitlink_api.fetch_pull_requests()
if gitlink_prs is not None:
print(f"GitLink连接成功找到 {len(gitlink_prs)} 个PR")
else:
print("GitLink连接失败")
# 测试Gitee连接
print("测试Gitee连接...")
gitee_prs = sync_service.gitee_api.fetch_pull_requests()
if gitee_prs is not None:
print(f"Gitee连接成功找到 {len(gitee_prs)} 个PR")
else:
print("Gitee连接失败")
except Exception as e:
print(f"连接测试失败: {str(e)}")
def example_custom_config():
"""
自定义配置示例
"""
print("\n=== GitLink-Gitee PR自定义配置示例 ===")
# 不同的同步配置
configs = [
{
'name': '开发环境同步',
'config': {
'gitlink_owner': 'dev_owner',
'gitlink_repo': 'dev_repo',
'gitlink_cookie': 'autologin_trustie=dev_cookie',
'gitee_owner': 'dev_owner',
'gitee_repo': 'dev_repo',
'gitee_token': 'dev_token',
'sync_direction': 'bidirectional',
'sync_comments': True
}
},
{
'name': '生产环境同步',
'config': {
'gitlink_owner': 'prod_owner',
'gitlink_repo': 'prod_repo',
'gitlink_cookie': 'autologin_trustie=prod_cookie',
'gitee_owner': 'prod_owner',
'gitee_repo': 'prod_repo',
'gitee_token': 'prod_token',
'sync_direction': 'gitlink_to_gitee', # 只从GitLink同步到Gitee
'sync_comments': False # 生产环境不同步评论
}
}
]
for config_info in configs:
print(f"\n执行 {config_info['name']} 同步...")
try:
sync_service = GitLinkGiteePRSyncService(config_info['config'])
sync_service.sync_pull_requests()
print(f"{config_info['name']} 同步完成")
except Exception as e:
print(f"{config_info['name']} 同步失败: {str(e)}")
def main():
"""
主函数
"""
print("GitLink-Gitee PR同步功能演示")
print("=" * 50)
# 运行各种示例
example_basic_sync()
example_unidirectional_sync()
example_with_error_handling()
example_test_connection()
example_custom_config()
print("\n" + "=" * 50)
print("演示完成")
if __name__ == "__main__":
main()