Overview
Nectr installs GitHub webhooks on repositories to receive real-time PR events. Webhook endpoint:POST /api/v1/webhooks/github
Events subscribed: pull_request, issues
Source: app/integrations/github/webhook_manager.py:10
Installation
install_webhook()
File: app/integrations/github/webhook_manager.py:10
owner— GitHub org/username (e.g.,"octocat")repo— Repository name (e.g.,"hello-world")access_token— GitHub personal access token or OAuth token (requiresadmin:repo_hookscope)backend_url— Your Nectr backend URL (default:http://localhost:8000)
webhook_id— Unique webhook ID from GitHubwebhook_secret— 64-char secret for HMAC verification
webhook_id and webhook_secret in the database:
Uninstallation
uninstall_webhook()
File: app/integrations/github/webhook_manager.py:50
404— Webhook already deleted (logs warning, doesn’t raise)- Other errors — Raises
httpx.HTTPStatusError
Webhook Verification
HMAC Signature Check
File:app/api/v1/webhooks/github.py (typical implementation)
Webhook Payload
Pull Request Event
Headers:X-GitHub-Event: pull_requestX-GitHub-Delivery: <uuid>X-Hub-Signature-256: sha256=<hmac>
opened— New PR createdsynchronize— PR updated (new commits pushed)reopened— Closed PR reopened
closed— PR merged/closed (no review needed)labeled,unlabeled,assigned, etc. — Metadata changes only
Configuration
Environment Variables
Required GitHub Scopes
For OAuth tokens, the user must grant:admin:repo_hook— Install/delete webhooksrepo— Read PR diffs, post comments
- Repository permissions:
contents: read— Read file contentspull_requests: write— Post review commentswebhooks: write— Install/delete webhooks
Database Schema
Typical table:Error Scenarios
403 Forbidden
Cause:access_token doesn’t have admin:repo_hook scope.
Solution: Re-authenticate user with correct scopes.
404 Not Found
Cause: Repository doesn’t exist or user doesn’t have access. Solution: Check repository name and user permissions.422 Validation Failed
Cause: Webhook already exists with the samepayload_url.
Solution:
Testing
Local Webhooks with ngrok
Manual Webhook Trigger
GitHub UI:- Go to
https://github.com/owner/repo/settings/hooks - Click on installed webhook
- Click “Recent Deliveries” → “Redeliver”
Webhook Payload Simulation
Security Best Practices
- Always verify signatures — Use
hmac.compare_digestto prevent timing attacks - Store secrets encrypted — Use
cryptography.fernetor database-level encryption - Rate limit webhook endpoint — GitHub can send many events quickly (e.g., force push)
- Use HTTPS only — Set
insecure_ssl: "0"in webhook config - Rotate secrets periodically — Delete + reinstall webhooks every 90 days
Monitoring
Webhook Delivery Logs
GitHub UI:https://github.com/owner/repo/settings/hooks/<webhook_id>/deliveries
Metrics to track:
- Delivery failures — 5xx errors, timeouts
- Signature mismatches — Possible secret leak
- High latency — Endpoint taking >5s to respond (GitHub timeout: 10s)
Alerting
Next Steps
- Review Flow — What happens after webhook is received
- GitHub Client — GitHub API wrapper implementation