test_github_integration.py•3.54 kB
"""
Test script for GitHub API integration.
This script tests:
1. GitHub client initialization
2. Repository retrieval
3. File content fetching
4. Code search
Run this before starting the full MCP server to verify GitHub API works.
"""
import asyncio
import sys
from src.github.client import GitHubClient
from config import settings
async def test_github_client():
"""Test GitHub client functionality."""
print("=" * 60)
print("GitHub API Integration Test")
print("=" * 60)
print()
# Initialize client
print("1. Initializing GitHub client...")
client = GitHubClient()
print(" ✓ Client initialized")
print()
# Test 1: Get a public repository
print("2. Testing repository retrieval...")
try:
repo_info = await client.get_repository("anthropics", "anthropic-sdk-python")
print(f" ✓ Repository: {repo_info['full_name']}")
print(f" ✓ Description: {repo_info['description']}")
print(f" ✓ Stars: {repo_info['stars']}")
print(f" ✓ Language: {repo_info['language']}")
print()
except Exception as e:
print(f" ✗ Error: {e}")
print()
# Test 2: Get repository structure
print("3. Testing repository structure...")
try:
structure = await client.get_repository_structure("anthropics", "anthropic-sdk-python")
print(f" ✓ Repository: {structure['name']}")
print(f" ✓ Files/folders: {len(structure['children'])}")
print(" Top-level items:")
for item in structure['children'][:5]: # Show first 5
icon = "📄" if item['type'] == 'file' else "📁"
print(f" {icon} {item['name']}")
print()
except Exception as e:
print(f" ✗ Error: {e}")
print()
# Test 3: Get file content
print("4. Testing file content retrieval...")
try:
file_data = await client.get_file_content("anthropics", "anthropic-sdk-python", "README.md")
print(f" ✓ File: {file_data['name']}")
print(f" ✓ Size: {file_data['size']} bytes")
print(f" ✓ Content preview: {file_data['content'][:100]}...")
print()
except Exception as e:
print(f" ✗ Error: {e}")
print()
# Test 4: Search code
print("5. Testing code search...")
try:
results = await client.search_code("anthropic", repo="anthropics/anthropic-sdk-python")
print(f" ✓ Found {len(results)} results")
if results:
print(" Top result:")
print(f" - File: {results[0]['name']}")
print(f" - Path: {results[0]['path']}")
print()
except Exception as e:
print(f" ✗ Error: {e}")
print()
# Cleanup
client.close()
print("=" * 60)
print("Test Complete!")
print("=" * 60)
print()
# Check if token is configured
if not settings.github_token:
print("⚠️ WARNING: No GitHub token configured!")
print(" Set GITHUB_TOKEN in .env file for higher rate limits")
print(" Without a token, you're limited to 60 requests/hour")
else:
print("✓ GitHub token is configured")
print()
if __name__ == "__main__":
try:
asyncio.run(test_github_client())
except KeyboardInterrupt:
print("\n\nTest interrupted by user")
sys.exit(0)
except Exception as e:
print(f"\n\n✗ FATAL ERROR: {e}")
import traceback
traceback.print_exc()
sys.exit(1)