4.8 KiB
4.8 KiB
Gitea GitHub Shim
A Python library that provides a GitHub-compatible interface for Gitea, allowing you to use the same code with both GitHub and Gitea APIs.
Features
- Drop-in replacement for PyGitHub's
Github
class - Automatic switching between GitHub and Gitea based on environment variables
- GitHub-compatible interface - minimal code changes required
- Error handling and fallback mechanisms
- Comprehensive model wrappers for repositories, users, and pull requests
Installation
- Clone this repository or copy the
gitea-shim/python
directory to your project - Install the required dependencies:
pip install gitea
Configuration
The shim automatically detects whether to use GitHub or Gitea based on environment variables:
GitHub Mode (Default)
export GITHUB_TOKEN="your_github_token"
# USE_GITEA is not set or set to 'false'
Gitea Mode
export USE_GITEA="true"
export GITEA_URL="http://your-gitea-instance:3000"
export GITEA_TOKEN="your_gitea_token" # or use GITHUB_TOKEN
Usage
Basic Usage
from gitea_shim import get_github_client
# This will automatically use GitHub or Gitea based on your environment
gh = get_github_client()
# Get a repository (works the same for both APIs)
repo = gh.get_repo("owner/repo")
# Get user information
user = gh.get_user()
# Create a pull request
pr = repo.create_pull(
title="My PR",
body="Description",
head="feature-branch",
base="main"
)
Migration from PyGitHub
Before (PyGitHub only):
from github import Github
gh = Github("your_token")
repo = gh.get_repo("owner/repo")
After (with shim):
from gitea_shim import get_github_client
gh = get_github_client() # Works with both GitHub and Gitea
repo = gh.get_repo("owner/repo") # Same interface!
Advanced Configuration
from gitea_shim import config, is_gitea_mode, get_api_info
# Check which API you're using
if is_gitea_mode():
print("Using Gitea API")
else:
print("Using GitHub API")
# Get detailed API information
api_info = get_api_info()
print(f"API Type: {api_info['type']}")
print(f"URL: {api_info['url']}")
print(f"Token Set: {api_info['token_set']}")
Supported Methods
Client Methods
get_repo(full_name)
- Get repository by owner/repo nameget_user(login=None)
- Get user (authenticated or by login)get_organization(login)
- Get organizationcreate_repo(name, **kwargs)
- Create new repositorysearch_repositories(query, **kwargs)
- Search repositoriessearch_users(query, **kwargs)
- Search usersget_api_status()
- Get API statusget_rate_limit()
- Get rate limit infoget_emojis()
- Get available emojisget_gitignore_templates()
- Get gitignore templatesget_license_templates()
- Get license templates
Repository Methods
get_pull(number)
- Get pull request by numberget_pulls(**kwargs)
- Get list of pull requestscreate_pull(title, body, head, base, **kwargs)
- Create pull requestget_contents(path, ref=None)
- Get file contentsget_branches()
- Get all branchesget_branch(branch)
- Get specific branchcreate_fork(organization=None)
- Fork repositoryget_commits(**kwargs)
- Get commitsget_commit(sha)
- Get specific commit
Pull Request Methods
update(**kwargs)
- Update pull requestmerge(**kwargs)
- Merge pull requestget_commits()
- Get commits in PRget_files()
- Get files changed in PRcreate_review_comment(body, commit_id, path, position)
- Add review commentcreate_issue_comment(body)
- Add issue comment
Testing
Run the test suite to verify the shim works correctly:
cd gitea-shim/python
python test_shim.py
Environment Variables
Variable | Description | Default |
---|---|---|
USE_GITEA |
Enable Gitea mode | false |
GITEA_URL |
Gitea instance URL | http://localhost:3000 |
GITHUB_TOKEN |
GitHub/Gitea API token | Required |
GITEA_TOKEN |
Gitea-specific token (optional) | Uses GITHUB_TOKEN |
Error Handling
The shim includes comprehensive error handling:
- Graceful fallbacks when Gitea methods don't exist
- Compatible error messages with GitHub's error format
- Automatic retry logic for transient failures
- Detailed logging for debugging
Limitations
- Some GitHub-specific features may not be available in Gitea
- Rate limiting is mocked for Gitea (returns unlimited)
- Some advanced search features may differ between APIs
- Draft pull requests may not be supported in all Gitea versions
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.