template: add base64encode and base64decode template functions - #5413
Open
pujitha24 wants to merge 1 commit into
Open
template: add base64encode and base64decode template functions#5413pujitha24 wants to merge 1 commit into
pujitha24 wants to merge 1 commit into
Conversation
Motivation: Notification templates have no way to base64-encode a string. Issue prometheus#3127 needs this to build a link into Karma, which expects its silence-search query parameter to be a base64-encoded JSON payload (`?m=<base64(json)>`), e.g. as an action button URL in a Slack message. Approach: Add `base64encode` and `base64decode` to the DefaultFuncs map in template/template.go, alongside the existing `toJson`/`reReplaceAll`/ `urlUnescape` helpers. Both use base64.URLEncoding (the URL/filename-safe alphabet) rather than StdEncoding, since the output is meant to be embedded directly in a URL query parameter: StdEncoding's `+` and `/` characters can be misinterpreted by URL/form parsers (`+` is commonly decoded as a space), silently corrupting the payload, which URLEncoding avoids. The existing `dict`/`list`/`toJson` functions can already build the JSON structure Karma expects; base64encode is the missing piece to turn it into a link. This only adds new template functions; no existing behavior changes. Validation: go build ./template/... go test ./template/... both pass, including new test cases in template/template_test.go for base64encode, base64encode's URL-safe alphabet (verified against an input whose encoding differs between Std and URL alphabets), base64decode, and the base64decode error path for invalid input. docs/notifications.md's function reference table is updated with both new entries. Fixes prometheus#3127 ```release-notes [ENHANCEMENT] Templates: Add `base64encode` and `base64decode` template functions ``` Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
📝 WalkthroughWalkthroughThe template function map now provides URL-safe ChangesBase64 template helpers
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@template/template.go`:
- Around line 355-358: Update the base64decode helper to wrap DecodeString
errors with the helper name using fmt.Errorf and %w, returning the wrapped error
while preserving the empty decoded string and existing successful-decoding
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 23e4d3d5-0ab3-4c3b-a714-62bf66718eed
📒 Files selected for processing (3)
docs/notifications.mdtemplate/template.gotemplate/template_test.go
TheMeier
approved these changes
Jul 31, 2026
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.
Motivation:
Notification templates have no way to base64-encode a string. Issue #3127
needs this to build a link into Karma, which expects its silence-search
query parameter to be a base64-encoded JSON payload
(
?m=<base64(json)>), e.g. as an action button URL in a Slack message.Approach:
Add
base64encodeandbase64decodeto the DefaultFuncs map intemplate/template.go, alongside the existing
toJson/reReplaceAll/urlUnescapehelpers. Both use base64.URLEncoding (the URL/filename-safealphabet) rather than StdEncoding, since the output is meant to be
embedded directly in a URL query parameter: StdEncoding's
+and/characters can be misinterpreted by URL/form parsers (
+is commonlydecoded as a space), silently corrupting the payload, which URLEncoding
avoids. The existing
dict/list/toJsonfunctions can already buildthe JSON structure Karma expects; base64encode is the missing piece to
turn it into a link.
This only adds new template functions; no existing behavior changes.
Validation:
go build ./template/...
go test ./template/...
both pass, including new test cases in template/template_test.go for
base64encode, base64encode's URL-safe alphabet (verified against an
input whose encoding differs between Std and URL alphabets), base64decode,
and the base64decode error path for invalid input.
docs/notifications.md's function reference table is updated with both
new entries.
Fixes #3127
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com