Skip to content

template: add base64encode and base64decode template functions - #5413

Open
pujitha24 wants to merge 1 commit into
prometheus:mainfrom
pujitha24:auto/issue-3127
Open

template: add base64encode and base64decode template functions#5413
pujitha24 wants to merge 1 commit into
prometheus:mainfrom
pujitha24:auto/issue-3127

Conversation

@pujitha24

Copy link
Copy Markdown

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 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 #3127

[ENHANCEMENT] Templates: Add `base64encode` and `base64decode` template functions

Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com

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>
@pujitha24
pujitha24 requested a review from a team as a code owner July 30, 2026 18:12
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The template function map now provides URL-safe base64encode and base64decode helpers. Documentation describes both functions, and template expansion tests cover successful encoding and decoding plus invalid input errors.

Changes

Base64 template helpers

Layer / File(s) Summary
Template helpers and validation
template/template.go, template/template_test.go, docs/notifications.md
DefaultFuncs registers URL-safe base64 encoding and decoding, decoding errors propagate through template execution, documentation lists both helpers, and tests cover success, URL-safe output, and invalid input.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, specific, and matches the main change: adding base64 template helpers.
Description check ✅ Passed The description covers motivation, approach, validation, docs, release notes, and the linked issue.
Linked Issues check ✅ Passed The PR adds the requested base64 template support for Karma silence payloads and includes tests and docs, satisfying #3127.
Out of Scope Changes check ✅ Passed The changes stay focused on the new template helpers, related tests, and docs, with no unrelated edits.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 846bdbf and 8f5c910.

📒 Files selected for processing (3)
  • docs/notifications.md
  • template/template.go
  • template/template_test.go

Comment thread template/template.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature-request] Generate JSON and base64 encode to generate silence payload for Karma

2 participants