Skip to content

Commit

Permalink
fix: repo name sanitizition
Browse files Browse the repository at this point in the history
Signed-off-by: Gosha <[email protected]>
  • Loading branch information
gosharo committed Sep 1, 2023
1 parent f2633b9 commit 2ee9671
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/git_provider/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request *
case "repo:push":
webhookPayload = &WebhookPayload{
Event: "push",
Repo: strings.ReplaceAll(gjson.GetBytes(buf.Bytes(), "repository.name").Value().(string), " ", "-"),
Repo: utils.SanitizeString(gjson.GetBytes(buf.Bytes(), "repository.name").Value().(string)),
Branch: gjson.GetBytes(buf.Bytes(), "push.changes.0.new.name").Value().(string),
Commit: gjson.GetBytes(buf.Bytes(), "push.changes.0.commits.0.hash").Value().(string),
UserEmail: utils.ExtractStringsBetweenTags(gjson.GetBytes(buf.Bytes(), "push.changes.0.commits.0.author.raw").Value().(string))[0],
Expand All @@ -179,7 +179,7 @@ func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request *
case "pullrequest:created", "pullrequest:updated":
webhookPayload = &WebhookPayload{
Event: "pull_request",
Repo: strings.ReplaceAll(gjson.GetBytes(buf.Bytes(), "repository.name").Value().(string), " ", "-"),
Repo: utils.SanitizeString(gjson.GetBytes(buf.Bytes(), "repository.name").Value().(string)),
Branch: gjson.GetBytes(buf.Bytes(), "pullrequest.source.branch.name").Value().(string),
Commit: gjson.GetBytes(buf.Bytes(), "pullrequest.source.commit.hash").Value().(string),
User: gjson.GetBytes(buf.Bytes(), "pullrequest.author.display_name").Value().(string),
Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,17 @@ func ExtractStringsBetweenTags(input string) []string {

return result
}

func SanitizeString(input string) string {
// Replace whitespace with "-"
input = strings.ReplaceAll(input, " ", "-")

// Define a regular expression pattern to match characters that are not a-z, A-Z, _, or .
// Use a negated character class to allow a-z, A-Z, _, and .
pattern := regexp.MustCompile(`[^a-zA-Z_1-9\.-]+`)

// Remove characters that don't match the pattern
sanitized := pattern.ReplaceAllString(input, "")

return sanitized
}
28 changes: 28 additions & 0 deletions pkg/utils/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,31 @@ func TestExtractStringsBetweenTags(t *testing.T) {
assert.Equal(test.expected, result)
}
}

func TestSanitizeString(t *testing.T) {
assert := assertion.New(t)

testCases := []struct {
input string
expected string
}{
// Test case 1: Basic test with spaces and invalid characters
{"Hello, World! This is a _sample string with 123 and some spaces.", "Hello-World-This-is-a-_sample-string-with-123-and-some-spaces."},

// Test case 2: No spaces or invalid characters
{"ThisIsAValidString_WithDots.", "ThisIsAValidString_WithDots."},

// Test case 3: Empty string
{"", ""},

// Test case 4: Symbols and special characters
{"$%#@_SymbolTest!.", "_SymbolTest."},
}

for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
result := SanitizeString(tc.input)
assert.Equal(tc.expected, result)
})
}
}
6 changes: 5 additions & 1 deletion pkg/webhook_creator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rookout/piper/pkg/clients"
"github.com/rookout/piper/pkg/conf"
"github.com/rookout/piper/pkg/git_provider"
"github.com/rookout/piper/pkg/utils"
"golang.org/x/net/context"
"strconv"
"strings"
Expand Down Expand Up @@ -86,7 +87,10 @@ func (wc *WebhookCreatorImpl) initWebhooks() error {
if wc.cfg.GitProviderConfig.OrgLevelWebhook && len(wc.cfg.GitProviderConfig.RepoList) != 0 {
return fmt.Errorf("org level webhook wanted but provided repositories list")
}
for _, repo := range strings.Split(strings.ReplaceAll(wc.cfg.GitProviderConfig.RepoList, " ", "-"), ",") {
for _, repo := range strings.Split(wc.cfg.GitProviderConfig.RepoList, ",") {
if wc.cfg.GitProviderConfig.Provider == "bitbucket" {
repo = utils.SanitizeString(repo)
}
hook, err := wc.clients.GitProvider.SetWebhook(&ctx, &repo)
if err != nil {
return err
Expand Down

0 comments on commit 2ee9671

Please sign in to comment.