forked from jkcl/reposync
187 lines
6.1 KiB
Python
187 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
快速Gitlink问题诊断脚本
|
||
基于日志中的具体错误进行针对性测试
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
import urllib3
|
||
|
||
# 禁用SSL警告
|
||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||
|
||
def test_exact_log_issue():
|
||
"""测试日志中显示的具体问题"""
|
||
print("=== 测试日志中的具体问题 ===")
|
||
|
||
# 从日志中提取的信息
|
||
owner = "gonggong123zzz"
|
||
repo_name = "repo2"
|
||
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
|
||
|
||
base_url = "https://www.gitlink.org.cn"
|
||
project_url = f"{base_url}/api/v1/projects"
|
||
|
||
params = {
|
||
'owner': owner,
|
||
'name': repo_name
|
||
}
|
||
|
||
headers = {
|
||
'Cookie': cookie,
|
||
'Content-Type': 'application/json',
|
||
'Accept': 'application/json'
|
||
}
|
||
|
||
print(f"测试项目: {owner}/{repo_name}")
|
||
print(f"请求URL: {project_url}")
|
||
print(f"请求参数: {params}")
|
||
print(f"请求头: {headers}")
|
||
print("-" * 50)
|
||
|
||
try:
|
||
response = requests.get(project_url, params=params, headers=headers, timeout=30, verify=False)
|
||
print(f"响应状态码: {response.status_code}")
|
||
print(f"响应头: {dict(response.headers)}")
|
||
print(f"响应内容: {response.text}")
|
||
|
||
if response.status_code == 200:
|
||
try:
|
||
data = response.json()
|
||
print(f"JSON解析成功: {json.dumps(data, indent=2, ensure_ascii=False)}")
|
||
|
||
if isinstance(data, list):
|
||
if data:
|
||
print(f"✓ 找到 {len(data)} 个项目")
|
||
project = data[0]
|
||
project_id = project.get('id')
|
||
print(f"项目ID: {project_id}")
|
||
|
||
# 测试PR列表获取
|
||
if project_id:
|
||
test_pr_list(project_id, headers, base_url)
|
||
else:
|
||
print("✗ 项目列表为空")
|
||
else:
|
||
print(f"✗ 响应格式不是列表: {type(data)}")
|
||
|
||
except json.JSONDecodeError as e:
|
||
print(f"✗ JSON解析失败: {e}")
|
||
print(f"响应内容可能是HTML: {response.text[:200]}...")
|
||
else:
|
||
print(f"✗ 请求失败,状态码: {response.status_code}")
|
||
|
||
except Exception as e:
|
||
print(f"✗ 请求异常: {e}")
|
||
|
||
def test_pr_list(project_id, headers, base_url):
|
||
"""测试PR列表获取"""
|
||
print(f"\n=== 测试PR列表获取 (项目ID: {project_id}) ===")
|
||
|
||
pr_url = f"{base_url}/api/v1/projects/{project_id}/pull_requests"
|
||
|
||
try:
|
||
response = requests.get(pr_url, headers=headers, timeout=30, verify=False)
|
||
print(f"PR列表响应状态码: {response.status_code}")
|
||
print(f"PR列表响应内容: {response.text[:500]}...")
|
||
|
||
if response.status_code == 200:
|
||
try:
|
||
prs = response.json()
|
||
print(f"✓ PR列表获取成功,数量: {len(prs) if isinstance(prs, list) else 'N/A'}")
|
||
except json.JSONDecodeError as e:
|
||
print(f"✗ PR列表JSON解析失败: {e}")
|
||
else:
|
||
print(f"✗ PR列表获取失败")
|
||
|
||
except Exception as e:
|
||
print(f"✗ PR列表请求异常: {e}")
|
||
|
||
def test_alternative_approaches():
|
||
"""测试替代方法"""
|
||
print("\n=== 测试替代方法 ===")
|
||
|
||
owner = "gonggong123zzz"
|
||
repo_name = "repo2"
|
||
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
|
||
base_url = "https://www.gitlink.org.cn"
|
||
|
||
headers = {
|
||
'Cookie': cookie,
|
||
'Content-Type': 'application/json',
|
||
'Accept': 'application/json'
|
||
}
|
||
|
||
# 方法1: 直接通过路径获取项目
|
||
endpoints = [
|
||
f"{base_url}/api/v1/repos/{owner}/{repo_name}",
|
||
f"{base_url}/api/v1/projects/{owner}/{repo_name}",
|
||
f"{base_url}/api/v1/repositories/{owner}/{repo_name}"
|
||
]
|
||
|
||
for endpoint in endpoints:
|
||
print(f"\n尝试端点: {endpoint}")
|
||
try:
|
||
response = requests.get(endpoint, headers=headers, timeout=30, verify=False)
|
||
print(f"状态码: {response.status_code}")
|
||
if response.status_code == 200:
|
||
print(f"✓ 成功: {endpoint}")
|
||
print(f"响应: {response.text[:300]}...")
|
||
break
|
||
else:
|
||
print(f"✗ 失败: {endpoint}")
|
||
except Exception as e:
|
||
print(f"✗ 异常: {endpoint}, 错误: {e}")
|
||
|
||
def test_auth_status():
|
||
"""测试认证状态"""
|
||
print("\n=== 测试认证状态 ===")
|
||
|
||
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
|
||
base_url = "https://www.gitlink.org.cn"
|
||
|
||
headers = {
|
||
'Cookie': cookie,
|
||
'Content-Type': 'application/json',
|
||
'Accept': 'application/json'
|
||
}
|
||
|
||
try:
|
||
response = requests.get(f"{base_url}/api/v1/user", headers=headers, timeout=30, verify=False)
|
||
print(f"用户信息响应状态码: {response.status_code}")
|
||
print(f"用户信息响应内容: {response.text}")
|
||
|
||
if response.status_code == 200:
|
||
print("✓ 认证成功")
|
||
else:
|
||
print("✗ 认证失败,Cookie可能已过期")
|
||
|
||
except Exception as e:
|
||
print(f"✗ 认证测试异常: {e}")
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("Gitlink 快速问题诊断工具")
|
||
print("=" * 60)
|
||
|
||
# 1. 测试认证状态
|
||
test_auth_status()
|
||
|
||
# 2. 测试日志中的具体问题
|
||
test_exact_log_issue()
|
||
|
||
# 3. 测试替代方法
|
||
test_alternative_approaches()
|
||
|
||
print("\n" + "=" * 60)
|
||
print("诊断完成")
|
||
print("\n可能的问题:")
|
||
print("1. Cookie过期 - 需要重新登录获取新的Cookie")
|
||
print("2. 项目不存在 - 检查项目名称和所有者")
|
||
print("3. API端点错误 - 查看Gitlink官方API文档")
|
||
print("4. 权限不足 - 确认用户对项目有访问权限")
|
||
|
||
if __name__ == "__main__":
|
||
main() |