reposync/test_gitee_real_webhook.py

303 lines
11 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 -*-
"""
模拟真实的Gitee webhook请求测试
使用实际的Gitee webhook数据进行测试
"""
import requests
import json
import time
# 测试配置
WEBHOOK_URL = "http://localhost:8001/webhook"
def test_real_gitee_webhook():
"""测试真实的Gitee webhook请求"""
print("🔍 模拟真实的Gitee webhook请求...")
print("=" * 60)
# 真实的Gitee webhook headers
headers = {
"Content-Type": "application/json",
"X-GIT-OSCHINA-EVENT": "Issue Hook",
"X-Gitee-Token": "hzk200407140238",
"X-Gitee-Event": "Issue Hook",
"User-Agent": "git-oschina-hook",
"X-Gitee-Timestamp": "1752060378420",
"X-Gitee-Ping": "false"
}
# 真实的Gitee webhook payload
payload = {
"iid": "ICL7X4",
"url": "https://gitee.com/ttk00/testdemo/issues/ICL7X4",
"sign": "",
"user": {
"id": 15796863,
"url": "https://gitee.com/lirenqiu",
"name": "黄泽楷",
"type": "User",
"email": "15796863+lirenqiu@user.noreply.gitee.com",
"login": "lirenqiu",
"remark": None,
"html_url": "https://gitee.com/lirenqiu",
"username": "lirenqiu",
"user_name": "lirenqiu",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"issue": {
"id": 21145432,
"body": "wewwe",
"user": {
"id": 15796863,
"url": "https://gitee.com/lirenqiu",
"name": "黄泽楷",
"type": "User",
"email": "15796863+lirenqiu@user.noreply.gitee.com",
"login": "lirenqiu",
"remark": None,
"html_url": "https://gitee.com/lirenqiu",
"username": "lirenqiu",
"user_name": "lirenqiu",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"ident": None,
"state": "open",
"title": "webhook-test-5",
"labels": [],
"number": "ICL7X4",
"assignee": None,
"comments": 0,
"deadline": None,
"html_url": "https://gitee.com/ttk00/testdemo/issues/ICL7X4",
"milestone": None,
"type_name": "任务",
"created_at": "2025-07-09T19:26:16+08:00",
"state_name": "待办的",
"updated_at": "2025-07-09T19:26:16+08:00",
"description": "wewwe",
"category_name": None,
"collaborators": [],
"plan_started_at": None
},
"state": "open",
"title": "webhook-test-5",
"action": "open",
"sender": {
"id": 15796863,
"url": "https://gitee.com/lirenqiu",
"name": "黄泽楷",
"type": "User",
"email": "15796863+lirenqiu@user.noreply.gitee.com",
"login": "lirenqiu",
"remark": None,
"html_url": "https://gitee.com/lirenqiu",
"username": "lirenqiu",
"user_name": "lirenqiu",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"hook_id": 2008053,
"project": {
"id": 41394009,
"url": "https://gitee.com/ttk00/testdemo",
"fork": False,
"name": "testdemo",
"path": "testdemo",
"owner": {
"id": 14897624,
"url": "https://gitee.com/ttk00",
"name": "ttk",
"type": "User",
"email": "14897624+ttk00@user.noreply.gitee.com",
"login": "ttk00",
"remark": None,
"html_url": "https://gitee.com/ttk00",
"username": "ttk00",
"user_name": "ttk00",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"git_url": "git://gitee.com/ttk00/testdemo.git",
"license": None,
"private": False,
"ssh_url": "git@gitee.com:ttk00/testdemo.git",
"svn_url": "svn://gitee.com/ttk00/testdemo",
"has_wiki": True,
"homepage": "",
"html_url": "https://gitee.com/ttk00/testdemo",
"language": None,
"clone_url": "https://gitee.com/ttk00/testdemo.git",
"full_name": "ttk00/testdemo",
"has_pages": False,
"namespace": "ttk00",
"pushed_at": "2025-06-24T11:04:25+08:00",
"created_at": "2025-06-13T20:50:44+08:00",
"has_issues": True,
"updated_at": "2025-07-09T19:26:17+08:00",
"description": "用来测试本地reposyncer服务是否可以正常启动以及远端服务器的issue/pr同步功能",
"forks_count": 0,
"git_ssh_url": "git@gitee.com:ttk00/testdemo.git",
"git_svn_url": "svn://gitee.com/ttk00/testdemo",
"git_http_url": "https://gitee.com/ttk00/testdemo.git",
"default_branch": "master",
"watchers_count": 2,
"stargazers_count": 0,
"open_issues_count": 19,
"name_with_namespace": "ttk/testdemo",
"path_with_namespace": "ttk00/testdemo"
},
"assignee": None,
"hook_url": None,
"password": "hzk200407140238",
"hook_name": "issue_hooks",
"milestone": None,
"push_data": None,
"timestamp": "1752060378420",
"enterprise": None,
"repository": {
"id": 41394009,
"url": "https://gitee.com/ttk00/testdemo",
"fork": False,
"name": "testdemo",
"path": "testdemo",
"owner": {
"id": 14897624,
"url": "https://gitee.com/ttk00",
"name": "ttk",
"type": "User",
"email": "14897624+ttk00@user.noreply.gitee.com",
"login": "ttk00",
"remark": None,
"html_url": "https://gitee.com/ttk00",
"username": "ttk00",
"user_name": "ttk00",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"git_url": "git://gitee.com/ttk00/testdemo.git",
"license": None,
"private": False,
"ssh_url": "git@gitee.com:ttk00/testdemo.git",
"svn_url": "svn://gitee.com/ttk00/testdemo",
"has_wiki": True,
"homepage": "",
"html_url": "https://gitee.com/ttk00/testdemo",
"language": None,
"clone_url": "https://gitee.com/ttk00/testdemo.git",
"full_name": "ttk00/testdemo",
"has_pages": False,
"namespace": "ttk00",
"pushed_at": "2025-06-24T11:04:25+08:00",
"created_at": "2025-06-13T20:50:44+08:00",
"has_issues": True,
"updated_at": "2025-07-09T19:26:17+08:00",
"description": "用来测试本地reposyncer服务是否可以正常启动以及远端服务器的issue/pr同步功能",
"forks_count": 0,
"git_ssh_url": "git@gitee.com:ttk00/testdemo.git",
"git_svn_url": "svn://gitee.com/ttk00/testdemo",
"git_http_url": "https://gitee.com/ttk00/testdemo.git",
"default_branch": "master",
"watchers_count": 2,
"stargazers_count": 0,
"open_issues_count": 19,
"name_with_namespace": "ttk/testdemo",
"path_with_namespace": "ttk00/testdemo"
},
"updated_by": {
"id": 15796863,
"url": "https://gitee.com/lirenqiu",
"name": "黄泽楷",
"type": "User",
"email": "15796863+lirenqiu@user.noreply.gitee.com",
"login": "lirenqiu",
"remark": None,
"html_url": "https://gitee.com/lirenqiu",
"username": "lirenqiu",
"user_name": "lirenqiu",
"avatar_url": "https://gitee.com/assets/no_portrait.png",
"site_admin": False
},
"action_desc": "open",
"description": "wewwe",
"target_user": None,
"change_duration": None
}
print(f"📋 Issue信息:")
print(f" 标题: {payload['issue']['title']}")
print(f" 编号: {payload['issue']['number']}")
print(f" 动作: {payload['action']}")
print(f" 用户: {payload['user']['name']} ({payload['user']['login']})")
print(f" 仓库: {payload['project']['full_name']}")
print()
print("📤 发送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("✅ Gitee 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', '')}")
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 main():
"""主测试函数"""
print("🎯 真实Gitee Webhook测试")
print("=" * 60)
if test_real_gitee_webhook():
print("\n🎉 真实Gitee webhook测试成功")
print("✅ 系统能够正确处理真实的Gitee webhook请求")
else:
print("\n❌ 真实Gitee webhook测试失败")
print("⚠️ 请检查服务状态和配置")
if __name__ == "__main__":
main()