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

fix: escape non-standard characters in the filename path #106

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions cmd/generate/codeowners/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"sort"
"strings"
"regexp"

"github.com/open-sauced/pizza-cli/pkg/config"
)
Expand Down Expand Up @@ -58,13 +59,13 @@ func writeGitHubCodeownersChunk(authorStats AuthorStats, config *config.Spec, fi
}

if len(topContributors) > 0 {
_, err := fmt.Fprintf(file, "%s @%s\n", srcFilename, strings.Join(resultSlice, " @"))
_, err := fmt.Fprintf(file, "%s @%s\n", cleanFilename(srcFilename), strings.Join(resultSlice, " @"))
if err != nil {
return nil, fmt.Errorf("error writing to %s file: %w", outputPath, err)
}
} else {
// no code owners to attribute to file
_, err := fmt.Fprintf(file, "%s\n", srcFilename)
_, err := fmt.Fprintf(file, "%s\n", cleanFilename(srcFilename))
if err != nil {
return nil, fmt.Errorf("error writing to %s file: %w", outputPath, err)
}
Expand Down Expand Up @@ -116,3 +117,13 @@ func getTopContributorAttributions(authorStats AuthorStats, n int, config *confi

return topContributors
}

func cleanFilename(filename string) string {
// Split the filename in case its rename, see https://github.com/open-sauced/pizza-cli/issues/101
parsedFilename := strings.Split(filename, " ")[0]
// Replace anything that is not a word, period, single quote, dash, space, forward slash, or backslash with an escaped version
re := regexp.MustCompile(`([^\w\.\'\-\s\/\\])`)
escapedFilename := re.ReplaceAllString(parsedFilename, "\\$0")

return escapedFilename
}
27 changes: 27 additions & 0 deletions cmd/generate/codeowners/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package codeowners

import (
"testing"
)

func TestCleanFilename(testRunner *testing.T) {
var tests = []struct {
name string
input string
expected string
}{
{"path/to/(home).go", "path/to/(home).go", `path/to/\(home\).go`},
{"path/to/[home].go", "path/to/[home].go", `path/to/\[home\].go`},
{"path/to/+page.go", "path/to/+page.go", `path/to/\+page.go`},
{"path/to/go-home.go", "path/to/go-home.go", `path/to/go-home.go`},
}

for _, testItem := range tests {
testRunner.Run(testItem.name, func(tester *testing.T) {
ans := cleanFilename(testItem.input)
if ans != testItem.expected {
tester.Errorf("got %s, expected %s", ans, testItem.expected)
}
})
}
}
Loading