Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow path templating for pull request remediations #5216

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const (

// ContentBytesLimit is the maximum number of bytes for the content
ContentBytesLimit = 5120

// PathBytesLimit is the maximum number of bytes for the path
PathBytesLimit = 200
)

var _ fsModifier = (*contentModification)(nil)
Expand Down Expand Up @@ -79,15 +82,20 @@ func prConfigToEntries(prCfg *pb.RuleType_Definition_Remediate_PullRequestRemedi
return nil, fmt.Errorf("cannot parse content template (index %d): %w", i, err)
}

pathTemplate, err := util.NewSafeTextTemplate(&cnt.Path, fmt.Sprintf("Path[%d]", i))
if err != nil {
return nil, fmt.Errorf("cannot parse path template (index %d): %w", i, err)
}

mode := ghModeNonExecFile
if cnt.GetMode() != "" {
mode = *cnt.Mode
}

entries[i] = &fsEntry{
Path: cnt.Path,
Mode: mode,
contentTemplate: contentTemplate,
pathTemplate: pathTemplate,
}
}

Expand All @@ -104,11 +112,18 @@ func (ca *contentModification) createFsModEntries(
}
for i, entry := range ca.entries {
content := new(bytes.Buffer)
path := new(bytes.Buffer)

if err := entry.contentTemplate.Execute(ctx, content, data, ContentBytesLimit); err != nil {
return fmt.Errorf("cannot execute content template (index %d): %w", i, err)
}

if err := entry.pathTemplate.Execute(ctx, path, data, PathBytesLimit); err != nil {
return fmt.Errorf("cannot execute path template (index %d): %w", i, err)
}

entry.Content = content.String()
entry.Path = path.String()
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type fsEntry struct {
contentTemplate *util.SafeTemplate
pathTemplate *util.SafeTemplate

Path string `json:"path"`
Content string `json:"content"`
Expand Down
Loading
Loading