# Gitea Webhook Ambassador

A service that receives Gitea webhooks and triggers corresponding Jenkins jobs based on repository and branch configurations.

## Features

- Receives Gitea webhooks and triggers Jenkins jobs
- Configurable repository to Jenkins job mappings
- Branch-specific job mappings with regex pattern support
- API key management for secure access
- SQLite persistence for configurations and logs
- Configurable worker pool for job processing
- Automatic retry with exponential backoff
- Webhook event deduplication
- Comprehensive logging and monitoring

## Configuration

The service is configured using a YAML file. Here's an example configuration:

```yaml
server:
  port: 8080
  webhookPath: "/webhook"
  secretHeader: "X-Gitea-Signature"
  secretKey: "custom-secret-key"

jenkins:
  url: "http://jenkins.example.com"
  username: "jenkins-user"
  token: "jenkins-api-token"
  timeout: 30

admin:
  token: "admin-api-token"  # Token for admin API access

database:
  path: "data/gitea-webhook-ambassador.db"  # Path to SQLite database file

logging:
  level: "info"
  format: "json"
  file: ""

worker:
  poolSize: 10
  queueSize: 100
  maxRetries: 3
  retryBackoff: 1

eventCleanup:
  interval: 3600
  expireAfter: 7200
```

## API Endpoints

### Admin API

All admin API endpoints require the `X-Admin-Token` header with the configured admin token.

#### Create API Key
```
POST /admin/api-keys
Content-Type: application/json

{
  "key": "api-key-value",
  "description": "Key description"
}
```

#### List API Keys
```
GET /admin/api-keys
```

#### Delete API Key
```
DELETE /admin/api-keys/delete?key=api-key-value
```

### Project Mapping API

All project mapping API endpoints require the `X-API-Key` header with a valid API key.

#### Create Project Mapping
```
POST /api/projects
Content-Type: application/json

{
  "repository_name": "owner/repo",
  "default_job": "default-jenkins-job",
  "branch_jobs": [
    {
      "branch_name": "main",
      "job_name": "main-job"
    }
  ],
  "branch_patterns": [
    {
      "pattern": "^feature/.*$",
      "job_name": "feature-job"
    }
  ]
}
```

#### Get Project Mapping
```
GET /api/projects?repository=owner/repo
```

### Trigger Logs API

Requires the `X-API-Key` header with a valid API key.

#### Get Trigger Logs
```
GET /api/logs?repository=owner/repo&branch=main&since=2024-01-01T00:00:00Z&limit=100
```

Query parameters:
- `repository`: Filter by repository name (optional)
- `branch`: Filter by branch name (optional)
- `since`: Filter by timestamp (RFC3339 format, optional)
- `limit`: Maximum number of logs to return (default: 100, max: 1000)

### Webhook Endpoint

```
POST /webhook
X-Gitea-Signature: custom-secret-key

{
  "ref": "refs/heads/main",
  "after": "commit-sha",
  "repository": {
    "full_name": "owner/repo",
    "clone_url": "https://gitea.example.com/owner/repo.git"
  },
  "pusher": {
    "login": "username",
    "email": "user@example.com"
  }
}
```

## Building and Running

### Prerequisites
- Go 1.24 or later
- SQLite3

### Build
```bash
make build
```

### Run
```bash
make run
```

### Docker
```bash
# Build Docker image
make docker-build

# Run with Docker
docker run -p 8080:8080 -v /path/to/config.yaml:/app/config/config.yaml freeleaps/gitea-webhook-ambassador
```

## License

This project is licensed under the MIT License - see the LICENSE file for details. 