# GitHub Webhook Testing

This directory contains tools for testing GitHub webhooks locally during development.

## Quick Start

1. **Start your local server:**
   ```bash
   npm run dev
   ```

2. **Run webhook tests:**
   ```bash
   # Test all webhooks
   node test/webhooks/test-github-webhooks.js

   # Test specific webhook
   node test/webhooks/test-github-webhooks.js installation
   node test/webhooks/test-github-webhooks.js pull_request.opened
   ```

## Configuration

The test script uses the following defaults:

- **Webhook Secret**: `dev-webhook-secret-2pjGGmaNy2gyEY4o3aU`
- **Server URL**: `http://localhost:3001/cfapi/github/webhooks`

To use different values:

```bash
# Custom server URL
SERVER_URL=http://localhost:4000/webhooks node test/webhooks/test-github-webhooks.js

# Custom webhook secret
GH_APP_WEBHOOK_SECRET=my-secret node test/webhooks/test-github-webhooks.js
```

## Available Webhook Events

- `ping` - GitHub ping event
- `installation` - App installation created
- `installation_repositories` - Repositories added to installation
- `pull_request.opened` - Pull request opened
- `pull_request.closed` - Pull request closed (includes merge scenario)
- `pull_request.synchronize` - Pull request updated with new commits
- `marketplace_purchase` - Marketplace purchase event
- `check_suite` - Check suite requested

## Adding New Test Events

To add a new webhook event for testing:

1. Add the payload to `webhookPayloads` object in `test-github-webhooks.js`
2. Use actual payload structure from [GitHub Webhook Events documentation](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads)
3. Include all required fields for your event handlers

## Development Setup

For local development, ensure your `.env` file includes:

```env
GH_APP_WEBHOOK_SECRET=dev-webhook-secret-2pjGGmaNy2gyEY4o3aU
```

This matches the default secret used by the test script.

## Testing with Real GitHub Webhooks

For testing with actual GitHub webhooks:

1. **Install ngrok:**
   ```bash
   brew install ngrok  # macOS
   # or download from https://ngrok.com
   ```

2. **Expose your local server:**
   ```bash
   ngrok http 3001
   ```

3. **Update your GitHub App webhook URL:**
   - Go to your GitHub App settings
   - Set webhook URL to: `https://[your-ngrok-subdomain].ngrok.io/cfapi/github/webhooks`
   - Keep the same webhook secret

4. **Monitor webhooks:**
   - Check your server logs
   - View webhook deliveries in GitHub App settings

## Troubleshooting

### "Cannot set headers after they are sent" error
- Ensure CORS middleware is placed before webhook routes in `server.js`

### Webhook not being processed
- Check that `ghAppMiddleware` is created with the correct path prefix
- Verify webhook secret matches between test script and server
- Ensure event handlers are registered in `github-app.js`

### Signature verification failing
- Webhook secret must match exactly (including any whitespace)
- Payload must be stringified consistently
- Check that headers are properly formatted

## Example Output

```
GitHub Webhook Local Testing
Server URL: http://localhost:3001/cfapi/github/webhooks
Webhook Secret: dev-webhoo...

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Testing webhook: installation
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Delivery ID: 550e8400-e29b-41d4-a716-446655440000
Signature: sha256=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0...
Payload size: 1234 bytes

Response:
Status: 200 (45ms)
Body: Webhook processed successfully
✓ Webhook delivered successfully
```
