';
echo '';
echo '
';
require_once __DIR__ . '/storage.php';
$github = StorageConfig::getGitHubConfig();
if (empty($github['token']) || empty($github['owner']) || empty($github['repo'])) {
echo '❌ GitHub 配置不完整
';
exit;
}
echo 'GitHub 连接诊断
';
// 通用 HTTP 头部设置
$headers = [
'Authorization: token ' . $github['token'],
'User-Agent: VvvebJs-Editor/1.0 (GitHub-API-Client)',
'Accept: application/vnd.github.v3+json',
'Content-Type: application/json'
];
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers),
'timeout' => 30
]
]);
echo '1. GitHub Token 验证
';
$response = @file_get_contents('https://api.github.com/user', false, $context);
if ($response === false) {
echo '❌ 无法连接到 GitHub API
';
$error = error_get_last();
echo '错误: ' . htmlspecialchars($error['message']) . '
';
} else {
$user = json_decode($response, true);
echo '✅ 有效 (HTTP 200)
';
echo '用户信息: ' . htmlspecialchars($user['name'] ?? $user['login']) . '
';
echo '用户名: ' . htmlspecialchars($user['login']) . '
';
}
echo '2. 仓库访问检查
';
$repo_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}";
$response = @file_get_contents($repo_url, false, $context);
if ($response === false) {
echo '❌ 无法访问仓库
';
$error = error_get_last();
echo '错误: ' . htmlspecialchars($error['message']) . '
';
} else {
$repo = json_decode($response, true);
echo '✅ 可访问 (HTTP 200)
';
echo '仓库全名: ' . htmlspecialchars($repo['full_name']) . '
';
echo '私有仓库: ' . ($repo['private'] ? '是' : '否') . '
';
echo '默认分支: ' . htmlspecialchars($repo['default_branch']) . '
';
// 自动使用仓库的默认分支
$actual_branch = $repo['default_branch'];
echo '⚠️ 将使用默认分支: ' . htmlspecialchars($actual_branch) . '
';
}
echo '3. 分支检查
';
// 使用实际的默认分支进行检查
$branch_to_check = isset($actual_branch) ? $actual_branch : $github['branch'];
$branch_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/branches/{$branch_to_check}";
$response = @file_get_contents($branch_url, false, $context);
if ($response === false) {
echo '❌ 分支不存在 (HTTP 404)
';
echo '⚠️ 尝试创建分支...
';
// 尝试获取主分支的最新 commit
$commits_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/commits?per_page=1";
$commits_response = @file_get_contents($commits_url, false, $context);
if ($commits_response) {
$commits = json_decode($commits_response, true);
if (!empty($commits)) {
$latest_sha = $commits[0]['sha'];
// 创建分支
$create_context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'ref' => "refs/heads/{$branch_to_check}",
'sha' => $latest_sha
]),
'timeout' => 30
]
]);
$create_response = @file_get_contents("https://api.github.com/repos/{$github['owner']}/{$github['repo']}/git/refs", false, $create_context);
if ($create_response) {
echo '✅ 分支创建成功
';
} else {
echo '❌ 分支创建失败
';
}
}
}
} else {
echo '✅ 分支存在
';
}
echo '4. 文件创建权限测试
';
$test_path = trim($github['path'], '/') . '/test-' . time() . '.txt';
$file_content = base64_encode('VvvebJs test file - ' . date('Y-m-d H:i:s'));
// 改进的文件创建请求
$create_file_context = stream_context_create([
'http' => [
'method' => 'PUT',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Test file creation',
'content' => $file_content,
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
$file_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/contents/{$test_path}";
$response = @file_get_contents($file_url, false, $create_file_context);
if ($response === false) {
echo '❌ 无法创建文件
';
$error = error_get_last();
echo '错误详情: ' . htmlspecialchars($error['message']) . '
';
// 检查是否是路径问题
if (strpos($github['path'], '/') !== false) {
echo '💡 尝试在根目录创建测试文件...
';
$root_test_path = 'vvvebjs-test-' . time() . '.txt';
$root_file_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/contents/{$root_test_path}";
$root_response = @file_get_contents($root_file_url, false, $create_file_context);
if ($root_response) {
echo '✅ 根目录文件创建成功
';
echo '⚠️ 建议将 GITHUB_PATH 设置为空或根目录
';
// 清理测试文件
$cleanup_context = stream_context_create([
'http' => [
'method' => 'DELETE',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Cleanup test file',
'sha' => json_decode($root_response, true)['content']['sha'],
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
@file_get_contents($root_file_url, false, $cleanup_context);
}
}
} else {
echo '✅ 文件创建成功
';
$file_info = json_decode($response, true);
echo '文件路径: ' . htmlspecialchars($file_info['content']['path']) . '
';
// 清理测试文件
$cleanup_context = stream_context_create([
'http' => [
'method' => 'DELETE',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Cleanup test file',
'sha' => $file_info['content']['sha'],
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
$cleanup_response = @file_get_contents($file_url, false, $cleanup_context);
if ($cleanup_response) {
echo '✅ 测试文件已清理
';
}
}
echo '5. 推荐配置
';
if (isset($actual_branch) && $actual_branch !== $github['branch']) {
echo '⚠️ 建议更新分支配置:
';
echo 'GITHUB_BRANCH=' . htmlspecialchars($actual_branch) . '
';
}
echo '✅ 推荐的环境变量配置:
';
echo '';
echo 'GITHUB_TOKEN=' . substr($github['token'], 0, 8) . '...
';
echo 'GITHUB_OWNER=' . htmlspecialchars($github['owner']) . '
';
echo 'GITHUB_REPO=' . htmlspecialchars($github['repo']) . '
';
echo 'GITHUB_BRANCH=' . htmlspecialchars(isset($actual_branch) ? $actual_branch : $github['branch']) . '
';
echo 'GITHUB_PATH= # 建议留空或设置为根目录
';
echo '
';
echo '';
echo '