Files
Yiqun/gitea-shim/python/config.py

93 lines
2.8 KiB
Python

"""Configuration for GitHub/Gitea API switching."""
import os
from typing import Optional
class APIConfig:
"""Configuration class for API switching between GitHub and Gitea."""
def __init__(self):
self.use_gitea = os.getenv('USE_GITEA', 'false').lower() == 'true'
self.gitea_url = os.getenv('GITEA_URL', 'http://localhost:3000')
self.github_token = os.getenv('GITHUB_TOKEN')
self.gitea_token = os.getenv('GITEA_TOKEN') or self.github_token
def get_client_class(self):
"""Get the appropriate client class based on configuration."""
if self.use_gitea:
try:
from gitea_github_shim import GiteaGitHubShim
return GiteaGitHubShim
except ImportError:
# Fallback to GitHub if Gitea shim is not available
from github import Github
return Github
else:
from github import Github
return Github
def get_client_kwargs(self):
"""Get the appropriate client initialization arguments."""
if self.use_gitea:
return {
'base_url_or_token': self.gitea_token,
'base_url': self.gitea_url
}
else:
# For GitHub, we need to pass the token as a positional argument
# and return empty kwargs since PyGitHub doesn't use keyword args for token
return {}
def get_client_args(self):
"""Get the positional arguments for client initialization."""
if self.use_gitea:
# Gitea shim uses keyword arguments
return []
else:
# GitHub uses positional argument for token
return [self.github_token] if self.github_token else []
# Global configuration instance
config = APIConfig()
def get_github_client():
"""
Get a GitHub-compatible client (either real GitHub or Gitea shim).
Returns:
GitHub-compatible client instance
"""
client_class = config.get_client_class()
args = config.get_client_args()
kwargs = config.get_client_kwargs()
if not config.github_token and not config.gitea_token:
raise ValueError(
"No API token found. Please set either GITHUB_TOKEN or GITEA_TOKEN environment variable."
)
return client_class(*args, **kwargs)
def is_gitea_mode():
"""Check if we're running in Gitea mode."""
return config.use_gitea
def get_api_info():
"""Get information about the current API configuration."""
if config.use_gitea:
return {
'type': 'gitea',
'url': config.gitea_url,
'token_set': bool(config.gitea_token)
}
else:
return {
'type': 'github',
'url': 'https://api.github.com',
'token_set': bool(config.github_token)
}