| 1 |
#!/usr/bin/env python3 |
| 2 |
""" |
| 3 |
Weblate webhook handler for automatic synchronization. |
| 4 |
This script handles incoming webhooks from Weblate to trigger repository updates. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
import subprocess |
| 9 |
import json |
| 10 |
from flask import Flask, request, jsonify |
| 11 |
import hmac |
| 12 |
import hashlib |
| 13 |
|
| 14 |
app = Flask(__name__) |
| 15 |
|
| 16 |
# Configuration |
| 17 |
WEBHOOK_SECRET = os.environ.get('WEBLATE_WEBHOOK_SECRET', '') |
| 18 |
REPO_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 19 |
|
| 20 |
def verify_signature(payload, signature): |
| 21 |
"""Verify webhook signature for security.""" |
| 22 |
if not WEBHOOK_SECRET: |
| 23 |
return True # Skip verification if no secret set |
| 24 |
|
| 25 |
expected = hmac.new( |
| 26 |
WEBHOOK_SECRET.encode('utf-8'), |
| 27 |
payload, |
| 28 |
hashlib.sha256 |
| 29 |
).hexdigest() |
| 30 |
|
| 31 |
return hmac.compare_digest(f'sha256={expected}', signature) |
| 32 |
|
| 33 |
def run_command(cmd): |
| 34 |
"""Run a shell command and return the result.""" |
| 35 |
try: |
| 36 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=REPO_PATH) |
| 37 |
return result.returncode == 0, result.stdout, result.stderr |
| 38 |
except Exception as e: |
| 39 |
return False, "", str(e) |
| 40 |
|
| 41 |
@app.route('/webhook/weblate', methods=['POST']) |
| 42 |
def weblate_webhook(): |
| 43 |
"""Handle Weblate webhook notifications.""" |
| 44 |
|
| 45 |
# Verify signature if configured |
| 46 |
signature = request.headers.get('X-Hub-Signature-256', '') |
| 47 |
if not verify_signature(request.data, signature): |
| 48 |
return jsonify({'error': 'Invalid signature'}), 403 |
| 49 |
|
| 50 |
try: |
| 51 |
data = request.json |
| 52 |
event = data.get('event', '') |
| 53 |
component = data.get('component', {}).get('slug', '') |
| 54 |
|
| 55 |
print(f"Received webhook: {event} for component: {component}") |
| 56 |
|
| 57 |
if event in ['translation_completed', 'translation_updated']: |
| 58 |
# Pull latest translations from Weblate |
| 59 |
success, stdout, stderr = run_command('wlc pull rust-coreutils') |
| 60 |
|
| 61 |
if success: |
| 62 |
# Commit and push changes |
| 63 |
run_command('git add src/uu/*/locales/*.ftl') |
| 64 |
success, _, _ = run_command('git commit -m "Update translations from Weblate"') |
| 65 |
|
| 66 |
if success: |
| 67 |
run_command('git push origin main') |
| 68 |
return jsonify({'status': 'success', 'message': 'Translations updated'}) |
| 69 |
else: |
| 70 |
return jsonify({'status': 'no_changes', 'message': 'No new translations'}) |
| 71 |
else: |
| 72 |
return jsonify({'status': 'error', 'message': f'Failed to pull: {stderr}'}), 500 |
| 73 |
|
| 74 |
elif event == 'repository_updated': |
| 75 |
# Source files changed, push to Weblate |
| 76 |
success, stdout, stderr = run_command('wlc push rust-coreutils') |
| 77 |
|
| 78 |
if success: |
| 79 |
return jsonify({'status': 'success', 'message': 'Source pushed to Weblate'}) |
| 80 |
else: |
| 81 |
return jsonify({'status': 'error', 'message': f'Failed to push: {stderr}'}), 500 |
| 82 |
|
| 83 |
return jsonify({'status': 'ignored', 'message': f'Event {event} not handled'}) |
| 84 |
|
| 85 |
except Exception as e: |
| 86 |
return jsonify({'status': 'error', 'message': str(e)}), 500 |
| 87 |
|
| 88 |
@app.route('/health') |
| 89 |
def health_check(): |
| 90 |
"""Health check endpoint.""" |
| 91 |
return jsonify({'status': 'healthy'}) |
| 92 |
|
| 93 |
if __name__ == '__main__': |
| 94 |
# For development only - use proper WSGI server in production |
| 95 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=False) |