Customizable Webhook Body Templating#158
Open
Ragug wants to merge 1 commit intoeduardolat:developfrom
Open
Conversation
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.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Pull Request: Customizable Webhook Body Templating
This PR introduces the ability to use Go's
text/templatesyntax 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
pgbackwebcan 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
Bodyfield for a webhook now accepts a Gotext/templatestring.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:DatabaseStatusDestinationStatusExecutionDetailsWebhookPayload: The main struct passed to the template, containing the above three structs plusEventTypeandMsg.3. Refactored Service Runners
RunDatabaseHealthy,RunDestinationUnhealthy,RunExecutionSuccess, etc.) are updated across the application to pre-build and pass the detailedDatabaseStatus,DestinationStatus, or fullWebhookPayload.4. Improved UI Documentation
.Database.Name,.Execution.FileSize, etc.), provides syntax examples, and documents the available helper functions.💻 Code Changes & Technical Notes
internal/service/webhooks/payload.go(NEW)internal/service/webhooks/utils.go(NEW)ParsePostgresURL,RenderWebhookBody, andbuildMessage. Registers template functions likeformatFileSize.internal/service/{databases, destinations, executions}internal/service/webhooks/run_webhook.gorunWebhookto accept and use theWebhookPayload.internal/service/webhooks/send_webhook_request.goRenderWebhookBodyto transform the template string into the final HTTP body.internal/view/web/dashboard/webhooks/common.go💬 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.