A modular Node.js server for receiving and processing Gogs webhooks. The server logs all incoming webhooks to the console and provides an extensible callback system for custom event handling.
npm install
Start the server with default configuration:
npm start
The server will start on port 3000 and log all incoming webhooks to the console.
npm run dev
Configure the server using environment variables:
PORT=8080 WEBHOOK_PATH=/gogs WEBHOOK_SECRET=your-secret npm start
Available environment variables:
PORT: Server port (default: 3000)WEBHOOK_PATH: Webhook endpoint path (default: /webhook)WEBHOOK_SECRET: Optional webhook secret for signature verificationPOST /webhook - Webhook endpointGET /health - Health check endpointThe server is designed to be easily extended with custom callbacks. See examples/with-callbacks.js for a complete example.
import { WebhookServer } from './src/server.js';
const server = new WebhookServer({ port: 3000 });
const handler = server.getHandler();
// Handle push events
handler.on('push', async (payload, headers) => {
console.log('Push to:', payload.repository.name);
console.log('Commits:', payload.commits.length);
// Your custom logic here
});
// Handle pull request events
handler.on('pull_request', async (payload, headers) => {
console.log('PR:', payload.action);
// Your custom logic here
});
server.start();
// Handle all events
handler.onAny(async (eventType, payload, headers) => {
console.log('Received event:', eventType);
// Send to analytics, external logging, etc.
});
node examples/with-callbacks.js
http://your-server:3000/webhookapplication/jsonpush - Repository pushcreate - Branch or tag creationdelete - Branch or tag deletionpull_request - Pull request actionsissues - Issue actionsissue_comment - Issue comment actionsrelease - Release actions.
├── src/
│ ├── index.js # Main entry point
│ ├── server.js # HTTP server implementation
│ ├── webhookHandler.js # Webhook processing and callbacks
│ └── logger.js # Logging utility
├── examples/
│ └── with-callbacks.js # Example with custom callbacks
├── package.json
└── README.md
src/server.js)src/webhookHandler.js)src/logger.js)You can test the webhook server using curl:
# Test health check
curl http://localhost:3000/health
# Test webhook with push event
curl -X POST http://localhost:3000/webhook \
-H "Content-Type: application/json" \
-H "X-Gogs-Event: push" \
-H "X-Gogs-Delivery: 12345" \
-d '{
"ref": "refs/heads/main",
"repository": {
"name": "test-repo",
"full_name": "user/test-repo"
},
"pusher": {
"username": "testuser"
},
"commits": [
{
"id": "abc123",
"message": "Test commit"
}
]
}'
ISC