reposync/test_gitlink_real_webhook.py

277 lines
9.5 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 webhook请求测试
使用实际的GitLink webhook数据进行测试
"""
import requests
import json
import time
# 测试配置
WEBHOOK_URL = "http://localhost:8001/webhook"
def test_real_gitlink_webhook():
"""测试真实的GitLink webhook请求"""
print("🔍 模拟真实的GitLink webhook请求...")
print("=" * 60)
# 真实的GitLink webhook headers
headers = {
"Content-Type": "application/json",
"X-Gitea-Delivery": "34756161-3ea3-429b-9474-6bd5dd0db6f1",
"X-Gitea-Event": "issue_comment",
"X-Gitea-Event-Type": "issue_comment",
"X-Gitea-Signature": "8a860b72da232f49b96d781f09eafbbf6f4a1a234c976aa258894b9dc4cd4cd8",
"X-Gogs-Delivery": "34756161-3ea3-429b-9474-6bd5dd0db6f1",
"X-Gogs-Event": "issue_comment",
"X-Gogs-Event-Type": "issue_comment",
"X-Gogs-Signature": "8a860b72da232f49b96d781f09eafbbf6f4a1a234c976aa258894b9dc4cd4cd8",
"X-Hub-Signature": "sha1=1cab338344264d0c674731051eac6015edcb1dbf",
"X-Hub-Signature-256": "sha256=8a860b72da232f49b96d781f09eafbbf6f4a1a234c976aa258894b9dc4cd4cd8",
"X-GitHub-Delivery": "34756161-3ea3-429b-9474-6bd5dd0db6f1",
"X-GitHub-Event": "issue_comment",
"X-GitHub-Event-Type": "issue_comment"
}
# 真实的GitLink webhook payload
payload = {
"action": "created",
"issue": {
"id": 131229,
"project_issues_index": 35,
"subject": "双向测试gitee到github的issue包括内容评论状态里程碑等",
"description": "我是秦薪淇",
"branch_name": None,
"start_date": None,
"due_date": None,
"created_at": "2025-07-09 14:56",
"updated_at": "2025-07-09 20:05",
"tags": [],
"status": {
"id": 1,
"name": "新增"
},
"priority": {
"id": 1,
"name": ""
},
"milestone": None,
"author": {
"id": 141380,
"login": "qinxinqi",
"name": "qinxinqi",
"email": "qinxinqi@example.org",
"image_url": "system/lets/letter_avatars/2/Q/223_120_140/120.png"
},
"assigners": [],
"participants": [
{
"id": 141380,
"login": "qinxinqi",
"name": "qinxinqi",
"email": "qinxinqi@example.org",
"image_url": "system/lets/letter_avatars/2/Q/223_120_140/120.png"
}
],
"comment_journals_count": 1,
"operate_journals_count": 1,
"attachments": []
},
"journal": {
"id": 430370,
"notes": "我秦薪淇实名上网",
"comments_count": 0
},
"project": {
"id": 1457398,
"identifier": "testdemo",
"name": "testdemo",
"description": "",
"visits": 71,
"praises_count": 0,
"watchers_count": 0,
"issues_count": 24,
"pull_requests_count": 1,
"forked_count": 0,
"is_public": True,
"mirror_url": "https://gitee.com/ttk00/testdemo",
"type": "mirror",
"created_at": "2025-06-13 20:52",
"updated_at": "2025-07-09 20:50",
"forked_from_project_id": None,
"platform": "forge",
"author": {
"name": "qinxinqi",
"type": "User",
"login": "qinxinqi",
"image_url": "system/lets/letter_avatars/2/Q/223_120_140/120.png"
},
"category": None,
"language": None
},
"password": "hzk200407140238" # 添加密码验证
}
print(f"📋 Issue信息:")
print(f" 标题: {payload['issue']['subject']}")
print(f" 编号: {payload['issue']['project_issues_index']}")
print(f" 动作: {payload['action']}")
print(f" 事件类型: issue_comment")
print(f" 用户: {payload['issue']['author']['name']} ({payload['issue']['author']['login']})")
print(f" 仓库: {payload['project']['identifier']}")
print(f" 评论内容: {payload['journal']['notes']}")
print()
print("📤 发送GitLink webhook请求...")
start_time = time.time()
try:
response = requests.post(
WEBHOOK_URL,
json=payload,
headers=headers,
timeout=60
)
end_time = time.time()
print(f"⏱️ 请求耗时: {end_time - start_time:.2f}")
if response.status_code == 200:
result = response.json()
print("✅ GitLink webhook处理成功:")
print(f" 成功状态: {result.get('success', False)}")
print(f" 处理消息: {result.get('message', '')}")
print(f" 识别平台: {result.get('platform', '')}")
if 'issue_info' in result:
issue_info = result['issue_info']
print(f" Issue标题: {issue_info.get('title', '')}")
print(f" Issue动作: {issue_info.get('action', '')}")
print(f" Issue用户: {issue_info.get('user', '')}")
print(f" 事件类型: {issue_info.get('event_type', '')}")
if 'sync_result' in result:
sync_result = result['sync_result']
print(f" 同步结果: {sync_result.get('success', False)}")
print(f" 同步消息: {sync_result.get('message', '')}")
if 'result' in sync_result:
sync_detail = sync_result['result']
print(f" 同步方向: {sync_detail.get('platform_direction', '')}")
print(f" 源仓库: {sync_detail.get('source', '')}")
print(f" 目标仓库: {sync_detail.get('target', '')}")
print(f" 时间戳: {result.get('timestamp', '')}")
return True
else:
print(f"❌ webhook处理失败: HTTP {response.status_code}")
print(f" 响应内容: {response.text}")
return False
except Exception as e:
print(f"❌ webhook请求异常: {str(e)}")
return False
def test_gitlink_issue_webhook():
"""测试GitLink Issue创建webhook非评论"""
print("\n🔍 模拟GitLink Issue创建webhook...")
print("=" * 60)
# 修改为Issue创建事件的headers
headers = {
"Content-Type": "application/json",
"X-Gitea-Event": "issues",
"X-Gitea-Event-Type": "issues",
"X-Gogs-Event": "issues",
"X-GitHub-Event": "issues"
}
# Issue创建事件的payload
payload = {
"action": "created",
"issue": {
"id": 131230,
"project_issues_index": 36,
"subject": "GitLink测试Issue创建",
"description": "这是一个GitLink创建的测试Issue",
"created_at": "2025-07-10 08:00",
"updated_at": "2025-07-10 08:00",
"author": {
"id": 141380,
"login": "qinxinqi",
"name": "qinxinqi",
"email": "qinxinqi@example.org"
}
},
"project": {
"id": 1457398,
"identifier": "testdemo",
"name": "testdemo",
"mirror_url": "https://gitee.com/ttk00/testdemo",
"author": {
"name": "qinxinqi",
"login": "qinxinqi"
}
},
"password": "hzk200407140238"
}
print(f"📋 Issue信息:")
print(f" 标题: {payload['issue']['subject']}")
print(f" 编号: {payload['issue']['project_issues_index']}")
print(f" 动作: {payload['action']}")
print(f" 事件类型: issues")
print(f" 用户: {payload['issue']['author']['name']}")
print()
try:
response = requests.post(WEBHOOK_URL, json=payload, headers=headers, timeout=150) # 增加到150秒
if response.status_code == 200:
result = response.json()
print("✅ GitLink Issue创建webhook处理成功:")
print(f" 处理消息: {result.get('message', '')}")
return True
else:
print(f"❌ webhook处理失败: HTTP {response.status_code}")
return False
except Exception as e:
print(f"❌ webhook请求异常: {str(e)}")
return False
def main():
"""主测试函数"""
print("🎯 真实GitLink Webhook测试")
print("=" * 60)
tests = [
("GitLink Issue评论", test_real_gitlink_webhook),
("GitLink Issue创建", test_gitlink_issue_webhook)
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
print(f"\n{'='*20} {test_name} {'='*20}")
try:
if test_func():
passed += 1
print(f"{test_name} 测试通过")
else:
print(f"{test_name} 测试失败")
except Exception as e:
print(f"💥 {test_name} 测试异常: {str(e)}")
time.sleep(2) # 测试间隔
print("\n" + "=" * 60)
print(f"🎯 GitLink测试完成: {passed}/{total} 通过")
if __name__ == "__main__":
main()