Skip to content

Commit

Permalink
Allow path templating for pull request remediations
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Dec 17, 2024
1 parent 9286b31 commit a99155e
Show file tree
Hide file tree
Showing 4 changed files with 1,635 additions and 1,624 deletions.
17 changes: 16 additions & 1 deletion internal/engine/actions/remediate/pull_request/types_content.go
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

0 comments on commit a99155e

Please sign in to comment.