Provisioning: Make image renderer note optional in PR comments (#113837)

* Provisioning: Remove image renderer note from PR comment template

Removes the 'NOTE: The image renderer is not configured' message from
the pull request comment template when image renderer is unavailable.
This addresses issue #656 in git-ui-sync-project.

- Updated commentTemplateMissingImageRenderer to be empty
- Updated testdata to reflect the change
- All unit tests pass

* Provisioning: Make image renderer note optional in PR comments

Make the image renderer note in pull request comments optional based on
the allowImageRendering configuration flag. When enabled, the note now
includes a link to the setup documentation.

- Add showImageRendererNote boolean field to commenter struct
- Update NewCommenter to accept showImageRendererNote parameter
- Update template to conditionally show note with documentation link
- Pass allowImageRendering from APIBuilder to commenter in register.go
- Update ProvidePullRequestWorker to use cfg.ProvisioningAllowImageRendering
- Add tests to verify note appears/disappears based on flag

Fixes https://github.com/grafana/git-ui-sync-project/issues/656

* Format code with go fmt

* Remove redundant text from image renderer note

Remove 'The image renderer is not configured.' from the note message.
The note now focuses on actionable guidance with the documentation link.

* Fix compilation error: use cfg.ProvisioningAllowImageRendering directly

Cannot access unexported field allowImageRendering from webhooks package.
Use cfg.ProvisioningAllowImageRendering directly since we have access to cfg.
This commit is contained in:
Roberto Jiménez Sánchez
2025-11-14 10:33:28 +01:00
committed by GitHub
parent 5b34d92dca
commit 1cc21a0705
5 changed files with 85 additions and 17 deletions
@@ -10,16 +10,18 @@ import (
)
type commenter struct {
templateDashboard *template.Template
templateTable *template.Template
templateRenderInfo *template.Template
templateDashboard *template.Template
templateTable *template.Template
templateRenderInfo *template.Template
showImageRendererNote bool
}
func NewCommenter() Commenter {
func NewCommenter(showImageRendererNote bool) Commenter {
return &commenter{
templateDashboard: template.Must(template.New("dashboard").Parse(commentTemplateSingleDashboard)),
templateTable: template.Must(template.New("table").Parse(commentTemplateTable)),
templateRenderInfo: template.Must(template.New("setup").Parse(commentTemplateMissingImageRenderer)),
templateDashboard: template.Must(template.New("dashboard").Parse(commentTemplateSingleDashboard)),
templateTable: template.Must(template.New("table").Parse(commentTemplateTable)),
templateRenderInfo: template.Must(template.New("setup").Parse(commentTemplateMissingImageRenderer)),
showImageRendererNote: showImageRendererNote,
}
}
@@ -53,7 +55,7 @@ func (c *commenter) generateComment(_ context.Context, info changeInfo) (string,
}
}
if info.MissingImageRenderer {
if info.MissingImageRenderer && c.showImageRendererNote {
if err := c.templateRenderInfo.Execute(&buf, info); err != nil {
return "", fmt.Errorf("unable to execute template: %w", err)
}
@@ -101,9 +103,8 @@ and {{ .SkippedFiles }} more files.
{{ end}}
`
// TODO: this should expand and show links to setup docs
const commentTemplateMissingImageRenderer = `
NOTE: The image renderer is not configured
NOTE: To enable dashboard previews in pull requests, refer to the [image rendering setup documentation](https://grafana.com/docs/grafana/latest/observability-as-code/provision-resources/git-sync-setup/#configure-webhooks-and-image-rendering).
`
// TODO: does this have some value?
@@ -21,7 +21,7 @@ func TestCommenter_Comment_FailedToComment(t *testing.T) {
repo := NewMockPullRequestRepo(t)
repo.On("CommentPullRequest", context.Background(), 1, mock.Anything).Return(errors.New("failed"))
commenter := NewCommenter()
commenter := NewCommenter(false)
err := commenter.Comment(context.Background(), repo, 1, changeInfo{})
require.Error(t, err)
}
@@ -138,9 +138,78 @@ func TestGenerateComment(t *testing.T) {
require.NoError(t, err)
repo.On("CommentPullRequest", context.Background(), 1, string(expect)).Return(nil)
commenter := NewCommenter()
commenter := NewCommenter(false)
err = commenter.Comment(context.Background(), repo, 1, tc.Input)
require.NoError(t, err)
})
}
}
func TestCommenter_ShowImageRendererNote(t *testing.T) {
t.Run("note appears when showImageRendererNote is true", func(t *testing.T) {
repo := NewMockPullRequestRepo(t)
info := changeInfo{
GrafanaBaseURL: "http://host/",
Changes: []fileChangeInfo{
{
Parsed: &resources.ParsedResource{
Info: &repository.FileInfo{
Path: "file.json",
},
Action: v0alpha1.ResourceActionUpdate,
GVK: schema.GroupVersionKind{Kind: "Dashboard"},
},
Title: "Existing Dashboard",
GrafanaURL: "http://grafana/d/uid",
PreviewURL: "http://grafana/admin/preview",
},
},
MissingImageRenderer: true,
}
var capturedComment string
repo.On("CommentPullRequest", context.Background(), 1, mock.MatchedBy(func(comment string) bool {
capturedComment = comment
return true
})).Return(nil)
commenter := NewCommenter(true)
err := commenter.Comment(context.Background(), repo, 1, info)
require.NoError(t, err)
require.Contains(t, capturedComment, "NOTE: To enable dashboard previews")
require.Contains(t, capturedComment, "https://grafana.com/docs/grafana/latest/observability-as-code/provision-resources/git-sync-setup/#configure-webhooks-and-image-rendering")
})
t.Run("note does not appear when showImageRendererNote is false", func(t *testing.T) {
repo := NewMockPullRequestRepo(t)
info := changeInfo{
GrafanaBaseURL: "http://host/",
Changes: []fileChangeInfo{
{
Parsed: &resources.ParsedResource{
Info: &repository.FileInfo{
Path: "file.json",
},
Action: v0alpha1.ResourceActionUpdate,
GVK: schema.GroupVersionKind{Kind: "Dashboard"},
},
Title: "Existing Dashboard",
GrafanaURL: "http://grafana/d/uid",
PreviewURL: "http://grafana/admin/preview",
},
},
MissingImageRenderer: true,
}
var capturedComment string
repo.On("CommentPullRequest", context.Background(), 1, mock.MatchedBy(func(comment string) bool {
capturedComment = comment
return true
})).Return(nil)
commenter := NewCommenter(false)
err := commenter.Comment(context.Background(), repo, 1, info)
require.NoError(t, err)
require.NotContains(t, capturedComment, "NOTE: To enable dashboard previews")
})
}
@@ -2,6 +2,4 @@ Hey there! 🎉
Grafana spotted some changes to your dashboard.
See the [original](http://grafana/d/uid) and [preview](http://grafana/admin/preview) of file.json.
NOTE: The image renderer is not configured
See the [original](http://grafana/d/uid) and [preview](http://grafana/admin/preview) of file.json.
@@ -38,7 +38,7 @@ func ProvidePullRequestWorker(
parsers := resources.NewParserFactory(clients)
screenshotRenderer := NewScreenshotRenderer(renderer, blobstore)
evaluator := NewEvaluator(screenshotRenderer, parsers, urlProvider, registry)
commenter := NewCommenter()
commenter := NewCommenter(cfg.ProvisioningAllowImageRendering)
return NewPullRequestWorker(evaluator, commenter, registry)
}