This application is a Cloudflare Worker designed to aggregate events from the company's Pact Broker. It processes webhook events, organizes them, and posts summaries to a specified Slack channel. The application leverages Durable Objects for stateful event aggregation and provides endpoints for debugging and manual processing.
- Quiet Period: Events are aggregated over a quiet period of 10 seconds to ensure batching of related events.
- Event Bucketing: Events are grouped into 1-minute buckets for efficient processing.
- Flushing Interval: Events are flushed and processed if they remain unprocessed for more than 5 minutes.
Copy the environment templates and configure them:
# Copy environment variables template
cp .env.example .env
# Copy local and production wrangler configuration template
cp wrangler.dev.template.jsonc wrangler.dev.jsonc
cp wrangler.prod.template.jsonc wrangler.prod.jsoncThen edit both .env, wrangler.dev.jsonc and wrangler.prod.jsonc with your specific values:
- SLACK_TOKEN: Your Slack bot token
- DEBUG_KEY: A secret key for accessing debug endpoints
- SLACK_CHANNEL: Target Slack channel (e.g.,
#ci) - GITHUB_BASE_URL: Your GitHub organization URL
- PACTICIPANT_TO_REPO_MAP: JSON mapping of service names to repository names
Set up secrets for production deployment:
# Set authentication secrets
wrangler secret put SLACK_TOKEN
wrangler secret put DEBUG_KEYwrangler types -c wrangler.dev.jsoncgenerates Env type using .env and vars in wrangler.dev.jsonc
For local development (uses wrangler.dev.jsonc):
npm run dev
# or
wrangler dev --config wrangler.dev.jsoncFor production-like local testing (uses wrangler.jsonc):
npm run dev:prodCRON jobs do not work in dev. You have to trigger the job execution manually (DEV_PORT is the port reported):
curl http://localhost:$DEV_PORT/trigger\?key\=$DEBUG_KEYThe production deployment uses wrangler.prod.jsonc which contains environment-specific configuration values. This file is excluded from git (listed in .gitignore) to avoid committing sensitive company information while still allowing non-secret environment variables to be managed as configuration rather than secrets.
The deployment script automatically handles the production configuration:
npm run deployThis command performs the following steps:
- Copies
wrangler.prod.jsonctowrangler.jsonc(temporary file) - Generates TypeScript types with
wrangler types - Deploys the worker with
wrangler deploy - Cleans up by removing the temporary
wrangler.jsoncfile
If you need to deploy manually:
wrangler deployNote: Manual deployment requires that wrangler.jsonc exists in the project root. For production deployments, always use npm run deploy to ensure the correct configuration is used.
The slack workspace to which the messages are being sent.
This is the Bot User OAuth Token found at OAuth & Permissions at https://api.slack.com/apps
e.g. xoxb-......................
wrangler secret put SLACK_TOKEN --name pact-slack-aggregatorand to set the key for authorization:
wrangler secret put DEBUG_KEY --name pact-slack-aggregator
wrangler tail pact-slack-aggregatorcurl https://psa.workers.dev/debug\?key\=DEBUG_KEY
curl -X GET $PACT_URL/webhooksIn folder pact-broker-webhooks read the README file for instructions.
For a Pact broker webhook to communicate from inside Docker to the host localhost POST to http://host.docker.internal:8787 and
run wrangler at ip 0.0.0.0:
wrangler dev --port 8787 --ip 0.0.0.0 --config wrangler.dev.jsonc
Also add to the docker file the environment variables:
PACT_BROKER_WEBHOOK_SCHEME_WHITELIST: 'https http'
And map host.docker.internal so the container can reach services running on the host (eg. webhook target on host port 8787)
extra_hosts:
- "host.docker.internal:host-gateway"