Remote provisioning: preview only one in pull requests (#103119)

* Limit number of previews to 10

* Use variable to check too many

* Single resource preview
This commit is contained in:
Roberto Jiménez Sánchez 2025-03-31 20:13:04 +02:00 committed by GitHub
parent 15c5b45724
commit ccb127cc79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 61 deletions

View File

@ -25,21 +25,27 @@ type resourcePreview struct {
}
const previewsCommentTemplate = `Hey there! 🎉
Grafana spotted some changes for your resources in this pull request:
Grafana spotted some changes for a single resource in this pull request:
## Summary
| File Name | Kind | Path | Action | Links |
|-----------|------|------|--------|-------|
{{- range .}}
| {{.Filename}} | {{.Kind}} | {{.Path}} | {{.Action}} | {{- if .OriginalURL}}[Original]({{.OriginalURL}}){{- end}}{{- if and .OriginalURL .PreviewURL}}, {{end}}{{- if .PreviewURL}}[Preview]({{.PreviewURL}}){{- end}} |
File Name: {{.Filename}}
Kind: {{.Kind}}
Path: {{.Path}}
Action: {{.Action}}
Links:
{{- if .OriginalURL}}
- [Original]({{.OriginalURL}})
{{- end}}
{{- if .PreviewURL}}
- [Preview]({{.PreviewURL}})
{{- end}}
Click the preview links above to view how your changes will look and compare them with the original and current versions.
Click the preview link above to view how your changes will look and compare them with the original and current versions.
{{- range .}}
{{- if .PreviewScreenshotURL}}
### Preview of {{.Filename}}
![Preview]({{.PreviewScreenshotURL}})
{{- end}}{{- end}}`
{{- end}}`
// PreviewRenderer is an interface for rendering a preview of a file
type PreviewRenderer interface {
@ -62,9 +68,9 @@ func NewPreviewer(renderer PreviewRenderer, urlProvider func(namespace string) s
}
// GenerateComment creates a formatted comment for dashboard previews
func (p *Previewer) GenerateComment(previews []resourcePreview) (string, error) {
func (p *Previewer) GenerateComment(preview *resourcePreview) (string, error) {
var buf bytes.Buffer
if err := p.template.Execute(&buf, previews); err != nil {
if err := p.template.Execute(&buf, preview); err != nil {
return "", fmt.Errorf("execute previews comment template: %w", err)
}
return buf.String(), nil

View File

@ -91,60 +91,41 @@ func (c *PullRequestWorker) Process(ctx context.Context,
return nil
}
progress.SetMessage(ctx, "processing pull request files")
previews := make([]resourcePreview, 0, len(files))
for _, f := range files {
result := jobs.JobResourceResult{
Path: f.Path,
}
if err := resources.IsPathSupported(f.Path); err != nil {
result.Action = repository.FileActionIgnored
progress.Record(ctx, result)
continue
}
result.Action = f.Action
fileInfo, err := prRepo.Read(ctx, f.Path, ref)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
parsed, err := parser.Parse(ctx, fileInfo, true)
if err != nil {
if errors.Is(err, resources.ErrUnableToReadResourceBytes) {
logger.Debug("file is not a resource", "path", f.Path)
result.Action = repository.FileActionIgnored
progress.Record(ctx, result)
} else {
result.Error = fmt.Errorf("failed to parse resource: %w", err)
progress.Record(ctx, result)
}
continue
}
result.Resource = parsed.GVR.Resource
result.Group = parsed.GVR.Group
result.Name = parsed.Obj.GetName()
preview, err := c.previewer.Preview(ctx, f, job.Namespace, repo.Config().Name, cfg.GitHub.Branch, ref, options.URL, cfg.GitHub.GenerateDashboardPreviews)
if err != nil {
result.Error = fmt.Errorf("create preview: %w", err)
progress.Record(ctx, result)
continue
}
previews = append(previews, *preview)
progress.Record(ctx, result)
}
if len(previews) == 0 {
progress.SetFinalMessage(ctx, "no previews to add")
if len(files) > 1 {
progress.SetFinalMessage(ctx, "too many files to preview")
return nil
}
f := files[0]
progress.SetMessage(ctx, "processing file preview")
if err := resources.IsPathSupported(f.Path); err != nil {
progress.SetFinalMessage(ctx, "file path is not supported")
return nil
}
fileInfo, err := prRepo.Read(ctx, f.Path, ref)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
_, err = parser.Parse(ctx, fileInfo, true)
if err != nil {
if errors.Is(err, resources.ErrUnableToReadResourceBytes) {
progress.SetFinalMessage(ctx, "file changes is not valid resource")
return nil
} else {
return fmt.Errorf("parse resource: %w", err)
}
}
preview, err := c.previewer.Preview(ctx, f, job.Namespace, repo.Config().Name, cfg.GitHub.Branch, ref, options.URL, cfg.GitHub.GenerateDashboardPreviews)
if err != nil {
return fmt.Errorf("generate preview: %w", err)
}
progress.SetMessage(ctx, "generating previews comment")
comment, err := c.previewer.GenerateComment(previews)
comment, err := c.previewer.GenerateComment(preview)
if err != nil {
return fmt.Errorf("generate comment: %w", err)
}
@ -152,7 +133,7 @@ func (c *PullRequestWorker) Process(ctx context.Context,
if err := prRepo.CommentPullRequest(ctx, options.PR, comment); err != nil {
return fmt.Errorf("comment pull request: %w", err)
}
logger.Info("previews comment added", "number", len(previews))
logger.Info("preview comment added")
return nil
}