|
|
9 mēneši atpakaļ | |
|---|---|---|
| examples | 9 mēneši atpakaļ | |
| src | 9 mēneši atpakaļ | |
| .env.example | 9 mēneši atpakaļ | |
| .gitignore | 9 mēneši atpakaļ | |
| README.md | 9 mēneši atpakaļ | |
| package.json | 9 mēneši atpakaļ |
A modular Node.js server for receiving and processing Gogs webhooks. The server logs all incoming webhooks to the console, executes configured commands based on webhook events, and provides an extensible callback system for custom event handling.
npm install
Copy the example environment file:
cp .env.example .env
Edit .env to configure commands for webhook events (see Configuration section below)
Start the server:
npm start
The server will start on port 3000 and execute configured commands when webhooks are received.
npm run dev
The server is configured using a .env file. Copy .env.example to .env and edit it:
cp .env.example .env
PORT=3000
WEBHOOK_PATH=/webhook
WEBHOOK_SECRET=your-secret-here
Configure commands to execute when webhooks are received. Commands support variable substitution:
Available Variables:
{{repo}} - Repository name (e.g., "my-repo"){{full_repo}} - Full repository name (e.g., "user/my-repo"){{branch}} - Branch name (e.g., "main", "feature/new-feature"){{pusher}} - Username of person who triggered the event{{event}} - Event type (e.g., "push", "pull_request"){{commit}} - Latest commit hash (short, for push events){{commit_msg}} - Latest commit message (for push events){{pr_number}} - Pull request number (for PR events){{pr_action}} - Pull request action (opened, closed, etc.){{tag}} - Tag name (for create/delete tag events)Example .env configuration:
# Execute command on push events
WEBHOOK_PUSH_ENABLED=true
WEBHOOK_PUSH_COMMAND=echo "Push to {{branch}} by {{pusher}} in {{repo}}"
# Deploy only when pushing to main branch
WEBHOOK_PUSH_FILTER_BRANCH=main
WEBHOOK_PUSH_COMMAND=/path/to/deploy.sh {{branch}} {{repo}} {{pusher}}
# Execute command on pull request events
WEBHOOK_PULL_REQUEST_ENABLED=true
WEBHOOK_PULL_REQUEST_COMMAND=echo "PR #{{pr_number}} {{pr_action}} by {{pusher}}"
# Global command (runs for all events)
WEBHOOK_GLOBAL_ENABLED=true
WEBHOOK_GLOBAL_COMMAND=/path/to/notify.sh {{event}} {{repo}} {{pusher}}
Supported Event Types:
WEBHOOK_PUSH_* - Push eventsWEBHOOK_PULL_REQUEST_* - Pull request eventsWEBHOOK_CREATE_* - Branch/tag creationWEBHOOK_DELETE_* - Branch/tag deletionWEBHOOK_RELEASE_* - Release eventsWEBHOOK_GLOBAL_* - All eventsCommand Settings:
COMMAND_TIMEOUT=300000 # Command timeout in ms (default: 5 minutes)
COMMAND_WORKING_DIR=/workspace # Working directory for commands
POST /webhook - Webhook endpointGET /health - Health check endpoint.env:
WEBHOOK_PUSH_ENABLED=true
WEBHOOK_PUSH_FILTER_BRANCH=main
WEBHOOK_PUSH_COMMAND=/home/deploy/deploy.sh {{repo}} {{branch}} {{commit}}
deploy.sh:
#!/bin/bash
REPO=$1
BRANCH=$2
COMMIT=$3
echo "Deploying $REPO from $BRANCH (commit: $COMMIT)"
cd /var/www/$REPO
git pull origin $BRANCH
npm install
npm run build
systemctl restart $REPO
.env:
WEBHOOK_GLOBAL_ENABLED=true
WEBHOOK_GLOBAL_COMMAND=curl -X POST https://api.slack.com/webhook -d '{"text":"{{event}} in {{repo}} by {{pusher}}"}'
.env:
WEBHOOK_PULL_REQUEST_ENABLED=true
WEBHOOK_PULL_REQUEST_COMMAND=/home/scripts/run-tests.sh {{repo}} {{pr_number}} {{pusher}}
The 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
│ ├── commandExecutor.js # Command execution with variable substitution
│ ├── config.js # Configuration loader (.env parser)
│ └── logger.js # Logging utility
├── examples/
│ └── with-callbacks.js # Example with custom callbacks
├── .env.example # Example configuration file
├── package.json
└── README.md
src/server.js)src/webhookHandler.js)src/logger.js)src/config.js)src/commandExecutor.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