forked from jkcl/reposync
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from service.issue_sync_service import IssueSyncService
|
|
import pymysql
|
|
|
|
def load_env_file(path):
|
|
"""从 .env 或 .ini 文件加载环境变量, 支持 'export KEY=VALUE' 格式"""
|
|
env_vars = {}
|
|
try:
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
if line.startswith('export '):
|
|
line = line[len('export '):]
|
|
|
|
parts = line.split('=', 1)
|
|
if len(parts) == 2:
|
|
key, value = parts[0].strip(), parts[1].strip()
|
|
# 去除值可能存在的引号
|
|
if (value.startswith("'") and value.endswith("'")) or \
|
|
(value.startswith('"') and value.endswith('"')):
|
|
value = value[1:-1]
|
|
env_vars[key] = value
|
|
except FileNotFoundError:
|
|
print(f"配置文件未找到: {path}")
|
|
except Exception as e:
|
|
print(f"读取配置文件时出错: {e}")
|
|
return env_vars
|
|
|
|
# 加载配置
|
|
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'env.ini')
|
|
db_config = load_env_file(config_path)
|
|
|
|
def get_db():
|
|
return pymysql.connect(
|
|
host=db_config.get('CEROBOT_MYSQL_HOST', 'localhost'),
|
|
user=db_config.get('CEROBOT_MYSQL_USER', 'root'),
|
|
password=db_config.get('CEROBOT_MYSQL_PWD', ''),
|
|
database='issue_sync',
|
|
charset="utf8mb4"
|
|
)
|
|
|
|
def get_all_enabled_sync_configs():
|
|
"""
|
|
从数据库读取所有启用的 issue 同步配置
|
|
"""
|
|
db = get_db()
|
|
cursor = db.cursor(pymysql.cursors.DictCursor)
|
|
cursor.execute("""
|
|
SELECT * FROM sync_config
|
|
WHERE enabled=1 AND sync_type='issue'
|
|
""")
|
|
configs = cursor.fetchall()
|
|
db.close()
|
|
return configs
|
|
|
|
def run_all_sync():
|
|
configs = get_all_enabled_sync_configs()
|
|
for config in configs:
|
|
print(f"开始同步: {config['source_platform']}->{config['target_platform']} {config['source_repo']}->{config['target_repo']}")
|
|
service = IssueSyncService(config)
|
|
|
|
# 根据配置决定同步方向
|
|
if config.get('sync_direction') == 'bidirectional':
|
|
print("执行双向同步...")
|
|
service.bidirectional_sync()
|
|
else:
|
|
print("执行单向同步...")
|
|
service.sync()
|
|
|
|
print(f"完成同步: {config['source_platform']}->{config['target_platform']} {config['source_repo']}->{config['target_repo']}")
|
|
|
|
if __name__ == "__main__":
|
|
run_all_sync()
|