Skip to content

Commit

Permalink
Template filenames in PR Automation updates (#497)
Browse files Browse the repository at this point in the history
Can help reduce some duplication in pr automation resources in the console
  • Loading branch information
michaeljguarino committed Feb 27, 2024
1 parent 5ff4f46 commit edb303a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
@@ -1,6 +1,6 @@
module github.com/pluralsh/plural-cli

go 1.22
go 1.22.0

require (
cloud.google.com/go/compute v1.19.2
Expand Down
18 changes: 13 additions & 5 deletions pkg/pr/types.go
Expand Up @@ -20,11 +20,12 @@ type PrTemplateSpec struct {
}

type UpdateSpec struct {
Regexes []string `json:"regexes"`
Files []string `json:"files"`
ReplaceTemplate string `json:"replace_template"`
Yq string `json:"yq"`
MatchStrategy string `json:"match_strategy"`
Regexes []string `json:"regexes"`
Files []string `json:"files"`
ReplaceTemplate string `json:"replace_template"`
Yq string `json:"yq"`
MatchStrategy string `json:"match_strategy"`
RegexReplacements []RegexReplacement `json:"regex_replacements"`
}

type CreateSpec struct {
Expand All @@ -38,6 +39,13 @@ type CreateTemplate struct {
External bool `json:"external"`
}

type RegexReplacement struct {
Regex string `json:"regex"`
Replacement string `json:"replacement"`
File string `json:"file"`
Templated bool `json:"templated"`
}

func Build(path string) (*PrTemplate, error) {
pr := &PrTemplate{}
data, err := os.ReadFile(path)
Expand Down
48 changes: 47 additions & 1 deletion pkg/pr/updates.go
Expand Up @@ -4,18 +4,32 @@ import (
"io/fs"
"path/filepath"
"regexp"

"github.com/samber/lo"
)

func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error {
if updates == nil {
return nil
}

if err := processRegexReplacements(updates.RegexReplacements, ctx); err != nil {
return err
}

replacement, err := templateReplacement([]byte(updates.ReplaceTemplate), ctx)
if err != nil {
return err
}

files := lo.Map(updates.Files, func(name string, ind int) string {
res, err := templateReplacement([]byte(name), ctx)
if err != nil {
return name
}
return string(res)
})

return filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -25,7 +39,7 @@ func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error {
return nil
}

ok, err := filenameMatches(path, updates.Files)
ok, err := filenameMatches(path, files)
if err != nil {
return err
}
Expand All @@ -38,6 +52,38 @@ func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error {
})
}

func processRegexReplacements(replacements []RegexReplacement, ctx map[string]interface{}) error {
if len(replacements) <= 0 {

Check failure on line 56 in pkg/pr/updates.go

View workflow job for this annotation

GitHub Actions / Lint

sloppyLen: len(replacements) <= 0 can be len(replacements) == 0 (gocritic)
return nil
}

for _, replacement := range replacements {
replaceWith, err := templateReplacement([]byte(replacement.Replacement), ctx)
if err != nil {
return err
}

replaceFunc := func(data []byte) ([]byte, error) {
r, err := regexp.Compile(replacement.Regex)
if err != nil {
return data, err
}
return r.ReplaceAll(data, replaceWith), nil
}

dest, err := templateReplacement([]byte(replacement.File), ctx)
if err != nil {
dest = []byte(replacement.File)
}

if err := replaceInPlace(string(dest), replaceFunc); err != nil {
return err
}
}

return nil
}

func updateFile(path string, updates *UpdateSpec, replacement []byte) error {
switch updates.MatchStrategy {
case "any":
Expand Down

0 comments on commit edb303a

Please sign in to comment.