diff --git a/pkg/git_provider/bitbucket.go b/pkg/git_provider/bitbucket.go index a9c09582..70cc7658 100644 --- a/pkg/git_provider/bitbucket.go +++ b/pkg/git_provider/bitbucket.go @@ -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], @@ -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), diff --git a/pkg/utils/common.go b/pkg/utils/common.go index 96d793a9..f6537b80 100644 --- a/pkg/utils/common.go +++ b/pkg/utils/common.go @@ -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 +} diff --git a/pkg/utils/common_test.go b/pkg/utils/common_test.go index 105ae11b..e283fc7c 100644 --- a/pkg/utils/common_test.go +++ b/pkg/utils/common_test.go @@ -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) + }) + } +} diff --git a/pkg/webhook_creator/main.go b/pkg/webhook_creator/main.go index d17de15a..2b819971 100644 --- a/pkg/webhook_creator/main.go +++ b/pkg/webhook_creator/main.go @@ -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" @@ -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