# Gitea Webhook Ambassador (Python) A high-performance Python webhook service for connecting Gitea and Jenkins, supporting intelligent dispatch, high concurrency processing, and deduplication strategies. ## ๐Ÿš€ Features ### 1. Intelligent Dispatch Strategy - **dev branches** โ†’ Trigger alpha environment build - **prod branches** โ†’ Trigger production environment build - **other branches** โ†’ Configurable default strategy ### 2. High Concurrency Processing - **Asynchronous Task Queue**: Use Celery + Redis for high concurrency - **Task Queueing Mechanism**: Prevent build loss, ensure tasks are executed in order - **Load Balancing**: Support multiple worker instances ### 3. Deduplication Strategy - **Deduplication based on commit hash + branch**: Prevent repeated triggers - **Time Window Deduplication**: Only trigger once for the same commit within a specified time window - **Intelligent Deduplication**: Support configurable deduplication strategies ## ๐Ÿ—๏ธ Architecture Design ``` Gitea Webhook โ†’ FastAPI โ†’ Celery Queue โ†’ Jenkins Workers โ†“ โ†“ โ†“ โ†“ Signature Verification Routing Dispatch Task Queueing Concurrent Execution โ†“ โ†“ โ†“ โ†“ Deduplication Environment Judgment Persistent Storage Status Feedback ``` ## ๐Ÿ“ Project Structure ``` gitea-webhook-ambassador-python/ โ”œโ”€โ”€ app/ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”œโ”€โ”€ main.py # FastAPI application entry โ”‚ โ”œโ”€โ”€ config.py # Configuration management โ”‚ โ”œโ”€โ”€ models/ # Data models โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”œโ”€โ”€ gitea.py # Gitea webhook model โ”‚ โ”‚ โ””โ”€โ”€ jenkins.py # Jenkins job model โ”‚ โ”œโ”€โ”€ services/ # Business logic โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”œโ”€โ”€ webhook_service.py # Webhook processing service โ”‚ โ”‚ โ”œโ”€โ”€ jenkins_service.py # Jenkins integration service โ”‚ โ”‚ โ”œโ”€โ”€ queue_service.py # Queue management service โ”‚ โ”‚ โ””โ”€โ”€ dedup_service.py # Deduplication service โ”‚ โ”œโ”€โ”€ api/ # API routes โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”œโ”€โ”€ webhook.py # Webhook endpoint โ”‚ โ”‚ โ”œโ”€โ”€ health.py # Health check โ”‚ โ”‚ โ””โ”€โ”€ admin.py # Admin interface โ”‚ โ”œโ”€โ”€ core/ # Core components โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”œโ”€โ”€ security.py # Security validation โ”‚ โ”‚ โ”œโ”€โ”€ database.py # Database connection โ”‚ โ”‚ โ””โ”€โ”€ cache.py # Cache management โ”‚ โ””โ”€โ”€ tasks/ # Celery tasks โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ””โ”€โ”€ jenkins_tasks.py # Jenkins task processing โ”œโ”€โ”€ tests/ # Test files โ”œโ”€โ”€ docker/ # Docker configuration โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ docker-compose.yml # Development environment โ””โ”€โ”€ README.md ``` ## ๐Ÿ› ๏ธ Tech Stack - **Web Framework**: FastAPI - **Task Queue**: Celery + Redis - **Database**: PostgreSQL (production) / SQLite (development) - **Cache**: Redis - **Monitoring**: Prometheus + Grafana - **Logging**: Structured logging with JSON - **Testing**: pytest + pytest-asyncio ## ๐Ÿš€ Quick Start ### 1. Install Dependencies ```bash pip install -r requirements.txt ``` ### 2. Configure Environment ```bash cp .env.example .env # Edit the .env file to configure Jenkins and database connections ``` ### 3. Start Service ```bash # Start Redis docker run -d -p 6379:6379 redis:alpine # Start Celery worker celery -A app.tasks worker --loglevel=info # Start FastAPI application uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 ``` ## ๐Ÿ“‹ Configuration ### Environment Dispatch Configuration ```yaml environments: dev: branches: ["dev", "develop", "development"] jenkins_job: "alpha-build" jenkins_url: "https://jenkins-alpha.example.com" prod: branches: ["prod", "production", "main", "master"] jenkins_job: "production-build" jenkins_url: "https://jenkins-prod.example.com" default: jenkins_job: "default-build" jenkins_url: "https://jenkins-default.example.com" ``` ### Deduplication Configuration ```yaml deduplication: enabled: true window_seconds: 300 # 5-minute deduplication window strategy: "commit_branch" # commit_hash + branch cache_ttl: 3600 # Cache for 1 hour ``` ### Queue Configuration ```yaml queue: max_concurrent: 10 max_retries: 3 retry_delay: 60 # seconds priority_levels: 3 ``` ## ๐Ÿ”ง API Endpoints ### Webhook Endpoint ``` POST /webhook/gitea ``` ### Health Check ``` GET /health GET /health/queue GET /health/jenkins ``` ### Admin Endpoints ``` GET /admin/queue/status GET /admin/queue/stats POST /admin/queue/clear ``` ## ๐Ÿงช Testing ```bash # Run all tests pytest # Run specific test pytest tests/test_webhook_service.py # Run performance test pytest tests/test_performance.py ``` ## ๐Ÿ“Š Monitoring Metrics - Webhook receive rate - Task queue length - Jenkins build success rate - Response time distribution - Deduplication hit rate ## ๐Ÿ”’ Security Features - Webhook signature verification - API key authentication - Request rate limiting - Input validation and sanitization - Secure logging