121 lines
3.7 KiB
Python
121 lines
3.7 KiB
Python
"""Utility functions for GitHub/Gitea migration."""
|
|
|
|
import os
|
|
import logging
|
|
from typing import Optional, Dict, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def setup_api_environment():
|
|
"""
|
|
Set up environment variables for API configuration.
|
|
|
|
This function helps configure the environment for either GitHub or Gitea mode.
|
|
"""
|
|
# Check if we should use Gitea
|
|
use_gitea = os.getenv('USE_GITEA', 'false').lower() == 'true'
|
|
|
|
if use_gitea:
|
|
logger.info("Configuring for Gitea mode")
|
|
|
|
# Ensure GITEA_URL is set
|
|
if not os.getenv('GITEA_URL'):
|
|
os.environ['GITEA_URL'] = 'http://localhost:3000'
|
|
logger.info(f"Set GITEA_URL to {os.environ['GITEA_URL']}")
|
|
|
|
# Use GITEA_TOKEN if available, otherwise fall back to GITHUB_TOKEN
|
|
if not os.getenv('GITEA_TOKEN') and os.getenv('GITHUB_TOKEN'):
|
|
os.environ['GITEA_TOKEN'] = os.environ['GITHUB_TOKEN']
|
|
logger.info("Using GITHUB_TOKEN as GITEA_TOKEN")
|
|
|
|
else:
|
|
logger.info("Configuring for GitHub mode")
|
|
|
|
# Ensure GITHUB_TOKEN is set
|
|
if not os.getenv('GITHUB_TOKEN'):
|
|
logger.warning("GITHUB_TOKEN not set")
|
|
|
|
|
|
def validate_api_configuration() -> Dict[str, Any]:
|
|
"""
|
|
Validate the current API configuration.
|
|
|
|
Returns:
|
|
Dictionary with validation results
|
|
"""
|
|
use_gitea = os.getenv('USE_GITEA', 'false').lower() == 'true'
|
|
|
|
if use_gitea:
|
|
gitea_url = os.getenv('GITEA_URL')
|
|
gitea_token = os.getenv('GITEA_TOKEN') or os.getenv('GITHUB_TOKEN')
|
|
|
|
return {
|
|
'mode': 'gitea',
|
|
'url_set': bool(gitea_url),
|
|
'token_set': bool(gitea_token),
|
|
'url': gitea_url,
|
|
'valid': bool(gitea_url and gitea_token)
|
|
}
|
|
else:
|
|
github_token = os.getenv('GITHUB_TOKEN')
|
|
|
|
return {
|
|
'mode': 'github',
|
|
'url_set': True, # GitHub URL is fixed
|
|
'token_set': bool(github_token),
|
|
'url': 'https://api.github.com',
|
|
'valid': bool(github_token)
|
|
}
|
|
|
|
|
|
def migrate_github_imports():
|
|
"""
|
|
Helper function to show how to migrate GitHub imports.
|
|
|
|
This function doesn't actually do anything, but serves as documentation
|
|
for the migration process.
|
|
"""
|
|
migration_examples = {
|
|
'old_import': 'from github import Github',
|
|
'new_import': 'from gitea_shim import get_github_client',
|
|
'old_usage': 'gh = Github(token)',
|
|
'new_usage': 'gh = get_github_client()',
|
|
'old_repo': 'repo = gh.get_repo("owner/repo")',
|
|
'new_repo': 'repo = gh.get_repo("owner/repo") # Same interface!'
|
|
}
|
|
|
|
logger.info("Migration examples:")
|
|
for key, value in migration_examples.items():
|
|
logger.info(f"{key}: {value}")
|
|
|
|
|
|
def get_api_client_info() -> Dict[str, Any]:
|
|
"""
|
|
Get information about the current API client configuration.
|
|
|
|
Returns:
|
|
Dictionary with API client information
|
|
"""
|
|
try:
|
|
from .config import get_api_info
|
|
return get_api_info()
|
|
except ImportError:
|
|
return validate_api_configuration()
|
|
|
|
|
|
def log_api_configuration():
|
|
"""Log the current API configuration for debugging."""
|
|
config = get_api_client_info()
|
|
logger.info(f"API Configuration: {config}")
|
|
|
|
if not config.get('valid', False):
|
|
logger.warning("API configuration is invalid!")
|
|
if config['mode'] == 'gitea':
|
|
if not config['url_set']:
|
|
logger.error("GITEA_URL not set")
|
|
if not config['token_set']:
|
|
logger.error("GITEA_TOKEN or GITHUB_TOKEN not set")
|
|
else:
|
|
if not config['token_set']:
|
|
logger.error("GITHUB_TOKEN not set") |