forked from Lesin/reposync
145 lines
4.6 KiB
Python
145 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
调试GitLink issue创建失败的问题
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
from typing import Dict
|
||
|
||
def _get_gitlink_headers(cookie: str) -> Dict[str, str]:
|
||
"""获取GitLink API请求头,使用cookie认证"""
|
||
# 清理cookie中的无效字符(换行符、回车符、前导和尾随空格)
|
||
cleaned_cookie = cookie.strip().replace('\n', '').replace('\r', '') if cookie else ''
|
||
|
||
return {
|
||
'Cookie': cleaned_cookie,
|
||
'User-Agent': 'IssueSync/1.0.0',
|
||
'Accept': 'application/json',
|
||
'Content-Type': 'application/json'
|
||
}
|
||
|
||
def test_gitlink_auth(cookie: str) -> bool:
|
||
"""测试GitLink cookie是否有效"""
|
||
try:
|
||
url = "https://www.gitlink.org.cn/api/user.json"
|
||
headers = {
|
||
'Cookie': cookie,
|
||
'User-Agent': 'IssueSync/1.0.0',
|
||
'Accept': 'application/json'
|
||
}
|
||
resp = requests.get(url, headers=headers, timeout=10)
|
||
print(f"认证测试 - 状态码: {resp.status_code}")
|
||
if resp.status_code == 200:
|
||
print(f"认证成功 - 用户信息: {resp.json()}")
|
||
return True
|
||
else:
|
||
print(f"认证失败 - 响应: {resp.text}")
|
||
return False
|
||
except Exception as e:
|
||
print(f"认证测试异常: {str(e)}")
|
||
return False
|
||
|
||
def test_create_issue(owner: str, repo: str, cookie: str) -> None:
|
||
"""测试创建GitLink issue"""
|
||
url = f"https://www.gitlink.org.cn/api/v1/{owner}/{repo}/issues.json"
|
||
|
||
# 测试数据
|
||
payload = {
|
||
"subject": "测试issue - 调试创建失败问题",
|
||
"description": "这是一个测试issue,用于调试GitLink创建issue失败的问题",
|
||
"status_id": 1, # 新建状态
|
||
"priority_id": 2, # 普通优先级
|
||
}
|
||
|
||
headers = _get_gitlink_headers(cookie)
|
||
|
||
print(f"创建issue测试")
|
||
print(f"URL: {url}")
|
||
print(f"Headers: {headers}")
|
||
print(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
||
|
||
try:
|
||
resp = requests.post(url, json=payload, headers=headers, timeout=10)
|
||
print(f"响应状态码: {resp.status_code}")
|
||
print(f"响应头: {dict(resp.headers)}")
|
||
print(f"响应内容: {resp.text}")
|
||
|
||
if resp.status_code == 200 or resp.status_code == 201:
|
||
response_data = resp.json()
|
||
issue_id = response_data.get("id", "")
|
||
print(f"创建成功! Issue ID: {issue_id}")
|
||
return issue_id
|
||
else:
|
||
print(f"创建失败! 状态码: {resp.status_code}")
|
||
print(f"错误信息: {resp.text}")
|
||
return None
|
||
except Exception as e:
|
||
print(f"创建issue异常: {str(e)}")
|
||
return None
|
||
|
||
def test_get_issues(owner: str, repo: str, cookie: str) -> None:
|
||
"""测试获取GitLink issues"""
|
||
url = f"https://www.gitlink.org.cn/api/v1/{owner}/{repo}/issues.json"
|
||
headers = _get_gitlink_headers(cookie)
|
||
|
||
params = {
|
||
"state": "all",
|
||
"scope": "all",
|
||
"per_page": 10
|
||
}
|
||
|
||
print(f"获取issues测试")
|
||
print(f"URL: {url}")
|
||
print(f"参数: {params}")
|
||
|
||
try:
|
||
resp = requests.get(url, headers=headers, params=params, timeout=20)
|
||
print(f"响应状态码: {resp.status_code}")
|
||
|
||
if resp.status_code == 200:
|
||
response_data = resp.json()
|
||
issues = response_data.get('issues', [])
|
||
print(f"成功获取 {len(issues)} 个issue")
|
||
if issues:
|
||
print("第一个issue示例:")
|
||
print(json.dumps(issues[0], indent=2, ensure_ascii=False))
|
||
else:
|
||
print(f"获取失败! 响应: {resp.text}")
|
||
except Exception as e:
|
||
print(f"获取issues异常: {str(e)}")
|
||
|
||
def main():
|
||
"""主函数 - 请修改这些参数"""
|
||
# 请根据实际情况修改这些参数
|
||
owner = "your_owner" # 替换为实际的owner
|
||
repo = "your_repo" # 替换为实际的repo
|
||
cookie = "your_cookie" # 替换为实际的cookie
|
||
|
||
print("=== GitLink Issue 创建问题调试 ===")
|
||
print(f"Owner: {owner}")
|
||
print(f"Repo: {repo}")
|
||
print(f"Cookie: {cookie[:50]}..." if len(cookie) > 50 else cookie)
|
||
print()
|
||
|
||
# 1. 测试认证
|
||
print("1. 测试认证...")
|
||
if not test_gitlink_auth(cookie):
|
||
print("认证失败,请检查cookie是否有效")
|
||
return
|
||
print()
|
||
|
||
# 2. 测试获取issues
|
||
print("2. 测试获取issues...")
|
||
test_get_issues(owner, repo, cookie)
|
||
print()
|
||
|
||
# 3. 测试创建issue
|
||
print("3. 测试创建issue...")
|
||
test_create_issue(owner, repo, cookie)
|
||
print()
|
||
|
||
print("调试完成!")
|
||
|
||
if __name__ == "__main__":
|
||
main() |