reposync/test_gitlink_api.py

182 lines
6.5 KiB
Python
Raw Permalink 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
"""
测试GitLink API端点和参数
"""
import requests
import json
from loguru import logger
def test_gitlink_endpoints():
"""测试不同的GitLink API端点"""
# 测试不同的端点格式
endpoints = [
"https://www.gitlink.org.cn/api/v1/yuyebo22/fix/issues.json",
"https://www.gitlink.org.cn/api/yuyebo22/fix/issues.json",
"https://www.gitlink.org.cn/api/v1/yuyebo22/fix/issues",
"https://www.gitlink.org.cn/api/yuyebo22/fix/issues",
"https://www.gitlink.org.cn/api/v1/yuyebo22/fix",
"https://www.gitlink.org.cn/api/yuyebo22/fix",
]
logger.info("🔍 测试GitLink API端点可访问性")
for endpoint in endpoints:
logger.info(f"\n📡 测试端点: {endpoint}")
try:
response = requests.get(endpoint, timeout=10)
logger.info(f"📤 状态码: {response.status_code}")
logger.info(f"📤 响应头: {dict(response.headers)}")
if response.status_code == 200:
logger.info(f"✅ 端点可访问: {endpoint}")
try:
data = response.json()
logger.info(f"📄 响应数据: {json.dumps(data, indent=2, ensure_ascii=False)}")
except:
logger.info(f"📄 响应文本: {response.text[:200]}...")
elif response.status_code == 401:
logger.warning(f"⚠️ 需要认证: {endpoint}")
elif response.status_code == 404:
logger.warning(f"⚠️ 端点不存在: {endpoint}")
else:
logger.warning(f"⚠️ 其他状态码: {response.status_code}")
except requests.exceptions.Timeout:
logger.error(f"❌ 请求超时: {endpoint}")
except requests.exceptions.ConnectionError:
logger.error(f"❌ 连接错误: {endpoint}")
except Exception as e:
logger.error(f"❌ 其他错误: {str(e)}")
def test_gitlink_issue_creation():
"""测试GitLink Issue创建"""
# 这里需要实际的token
token = "your_actual_token_here" # 需要替换为实际token
if token == "your_actual_token_here":
logger.warning("⚠️ 请先设置实际的GitLink token")
return
# 测试不同的API格式
test_cases = [
{
"url": "https://www.gitlink.org.cn/api/v1/yuyebo22/fix/issues.json",
"headers": {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
},
"payload": {
"subject": "测试Issue",
"description": "这是一个测试Issue",
"status_id": 1,
"priority_id": 2,
}
},
{
"url": "https://www.gitlink.org.cn/api/yuyebo22/fix/issues.json",
"headers": {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
},
"payload": {
"title": "测试Issue", # 尝试使用title而不是subject
"body": "这是一个测试Issue", # 尝试使用body而不是description
"status_id": 1,
"priority_id": 2,
}
},
{
"url": "https://www.gitlink.org.cn/api/v1/yuyebo22/fix/issues.json",
"headers": {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
},
"payload": {
"subject": "测试Issue",
"description": "这是一个测试Issue",
# 不包含status_id和priority_id
}
}
]
logger.info("\n🚀 测试GitLink Issue创建")
for i, test_case in enumerate(test_cases, 1):
logger.info(f"\n📝 测试用例 {i}:")
logger.info(f"📤 URL: {test_case['url']}")
logger.info(f"📤 Headers: {test_case['headers']}")
logger.info(f"📤 Payload: {test_case['payload']}")
try:
response = requests.post(
test_case['url'],
headers=test_case['headers'],
json=test_case['payload'],
timeout=30
)
logger.info(f"📤 响应状态码: {response.status_code}")
logger.info(f"📤 响应头: {dict(response.headers)}")
logger.info(f"📤 响应内容: {response.text}")
if response.status_code == 201 or response.status_code == 200:
logger.info(f"✅ 测试用例 {i} 成功")
try:
result = response.json()
logger.info(f"📄 解析结果: {json.dumps(result, indent=2, ensure_ascii=False)}")
except:
pass
else:
logger.warning(f"⚠️ 测试用例 {i} 失败: {response.status_code}")
except Exception as e:
logger.error(f"❌ 测试用例 {i} 异常: {str(e)}")
def test_gitlink_documentation():
"""查找GitLink API文档"""
logger.info("\n📚 查找GitLink API文档")
doc_urls = [
"https://www.gitlink.org.cn/api/docs",
"https://www.gitlink.org.cn/docs/api",
"https://www.gitlink.org.cn/help/api",
"https://www.gitlink.org.cn/api",
]
for url in doc_urls:
logger.info(f"\n🔍 检查文档: {url}")
try:
response = requests.get(url, timeout=10)
logger.info(f"📤 状态码: {response.status_code}")
if response.status_code == 200:
logger.info(f"✅ 文档可访问: {url}")
# 检查是否包含API相关信息
content = response.text.lower()
if 'api' in content or 'issue' in content or 'pull' in content:
logger.info(f"📄 包含API相关信息")
else:
logger.warning(f"⚠️ 文档不可访问: {response.status_code}")
except Exception as e:
logger.error(f"❌ 访问失败: {str(e)}")
def main():
"""主函数"""
logger.info("🚀 开始GitLink API测试")
# 测试端点可访问性
test_gitlink_endpoints()
# 查找API文档
test_gitlink_documentation()
# 测试Issue创建需要实际token
test_gitlink_issue_creation()
logger.info("\n✅ 测试完成")
if __name__ == "__main__":
main()