Skip to content

Conversation

@Ragug
Copy link

@Ragug Ragug commented Dec 1, 2025

🚀 Pull Request: Customizable Webhook Body Templating

This PR introduces the ability to use Go's text/template syntax to define the JSON body of outgoing webhooks, replacing the previous static body or simple JSON object.

This feature is inspired by the highly flexible and customizable webhook payloads found in professional monitoring and visualization tools like Uptime Kuma and Grafana, ensuring pgbackweb can integrate seamlessly with virtually any external notification system.

This change required a comprehensive refactoring of the webhook data flow to ensure all necessary event context is correctly built and passed down to the rendering layer.


✨ Key Changes & Features

1. Customizable Webhook Body

  • The Body field for a webhook now accepts a Go text/template string.
  • This allows users to create rich, custom payloads in JSON, dynamically injecting data fields like database name, execution status, and file size.
  • Helper Functions (formatTime, formatFileSize) are exposed in the template context for easy data formatting.

2. Standardized Event Payloads

To support templating, the webhook data flow was standardized using three new structs:

  • internal/service/webhooks/payload.go: Defines the new payload structures:
    • DatabaseStatus
    • DestinationStatus
    • ExecutionDetails
    • WebhookPayload: The main struct passed to the template, containing the above three structs plus EventType and Msg.

3. Refactored Service Runners

  • All event triggers (RunDatabaseHealthy, RunDestinationUnhealthy, RunExecutionSuccess, etc.) are updated across the application to pre-build and pass the detailed DatabaseStatus, DestinationStatus, or full WebhookPayload.
  • This ensures the exact state at the time of the event is captured and used for the webhook.

4. Improved UI Documentation

  • The Webhooks Edit/Create form now includes a detailed template helper panel to guide users.
  • The panel lists all available template fields (.Database.Name, .Execution.FileSize, etc.), provides syntax examples, and documents the available helper functions.

💻 Code Changes & Technical Notes

File/Package Change Description
internal/service/webhooks/payload.go (NEW) Defines the new data models for structured payloads.
internal/service/webhooks/utils.go (NEW) Contains utility functions: ParsePostgresURL, RenderWebhookBody, and buildMessage. Registers template functions like formatFileSize.
internal/service/{databases, destinations, executions} Updated runners to build and pass the structured status data instead of just the ID.
internal/service/webhooks/run_webhook.go Updated all runner methods and runWebhook to accept and use the WebhookPayload.
internal/service/webhooks/send_webhook_request.go Now calls RenderWebhookBody to transform the template string into the final HTTP body.
internal/view/web/dashboard/webhooks/common.go Significantly updated to add template documentation and a better default placeholder.

💬 Maintainer Feedback and Review Notes ⚙️

This feature represents a massive and necessary improvement to the maintainability and flexibility of the webhook system.

The core motivation for this refactor—to eliminate the burden of configuring and maintaining separate, hard-coded JSON payloads for every database and status change—has been successfully addressed by introducing centralized templating. This vastly reduces overhead for administrators.

image image image

This introduces full support for Go's `text/template` engine for webhook request bodies, allowing users to customize the payload sent based on the event data.

This required a significant refactor to standardize the data flow:

1.  **Standardized Payloads:** Introduced the `WebhookPayload` structure, along with `DatabaseStatus`, `DestinationStatus`, and `ExecutionDetails`, to aggregate all necessary context for any event. (See `internal/service/webhooks/payload.go`).
2.  **Service Refactoring:** All service methods (`TestDatabaseAndStoreResult`, `TestDestinationAndStoreResult`, `RunExecution`) now build and pass the structured `WebhookPayload` or status details when calling the `webhooksService` runners.
3.  **Template Rendering Logic:** Implemented `RenderWebhookBody` using `text/template` and registered helper functions (`formatTime`, `formatFileSize`). (See `internal/service/webhooks/utils.go`).
4.  **UI Documentation:** Updated the webhook creation/edit form to include detailed documentation on available fields, helper functions, and template examples.
5.  **Data Flow Update:** The `runWebhook` and `SendWebhookRequest` functions were updated to accept and process the structured payload before sending the request.

This enhancement makes webhooks far more flexible and useful for integrating with external monitoring tools.
@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant