diff --git a/core/gh2tf_repo_mapper.go b/core/gh2tf_repo_mapper.go index 4546204..91b1780 100644 --- a/core/gh2tf_repo_mapper.go +++ b/core/gh2tf_repo_mapper.go @@ -25,6 +25,20 @@ const ( //nolint:gochecknoglobals // Easier than duplicate it everywhere needed var falseString = "false" +// Replace branch protection pattern's special chars by dedicated string in order to avoid ID collision (pattern is used for TF ressource generation) +// in case every special chars are replaced by the same string => "?.*", "[.]" or "?/?" would all lead to the same "---" ID for instance. +var patternToIdReplacer = strings.NewReplacer( + ".", "_DOT_", + "/", "_SLASH_", + "\\", "_ESC_", + // fnmatch special chars + "*", "_STAR_", + "[", "_SEQ-O_", + "]", "_SEQ-C_", + "?", "_Q-MARK_", + "!", "_EX-MARK_", +) + func MapToRepositoryRes(repoConfig *GhRepoConfig, valGen tfsig.ValueGenerator, repoTfId string) *ghrepository.Config { if repoConfig == nil { return nil @@ -283,7 +297,7 @@ func MapToBranchProtectionRes( } if branchProtectionConfig.Pattern != nil { - idEnd = tfsig.ToTerraformIdentifier(*branchProtectionConfig.Pattern) + idEnd = tfsig.ToTerraformIdentifier(patternToIdReplacer.Replace(*branchProtectionConfig.Pattern)) } if branchProtectionConfig.StatusChecks != nil { diff --git a/core/testdata/repo1.full.golden.tf b/core/testdata/repo1.full.golden.tf index 4571232..4c8bd1b 100644 --- a/core/testdata/repo1.full.golden.tf +++ b/core/testdata/repo1.full.golden.tf @@ -87,7 +87,7 @@ resource "github_branch_protection" "repo1-default" { } } -resource "github_branch_protection" "repo1-feature-branch1" { +resource "github_branch_protection" "repo1-feature_SLASH_branch1" { repository_id = github_repository.repo1.node_id pattern = "feature/branch1" enforce_admins = true @@ -111,7 +111,7 @@ resource "github_branch_protection" "repo1-feature-branch1" { } } -resource "github_branch_protection" "repo1-feature-branch2" { +resource "github_branch_protection" "repo1-feature_SLASH_branch2" { repository_id = github_repository.repo1.node_id pattern = "feature/branch2" enforce_admins = false diff --git a/core/testdata/repo2.full.golden.tf b/core/testdata/repo2.full.golden.tf index 35282f6..bf6f683 100644 --- a/core/testdata/repo2.full.golden.tf +++ b/core/testdata/repo2.full.golden.tf @@ -87,7 +87,7 @@ resource "github_branch_protection" "repo2-default" { } } -resource "github_branch_protection" "repo2-feature-branch2" { +resource "github_branch_protection" "repo2-feature_SLASH_branch2" { repository_id = github_repository.repo2.node_id pattern = "feature/branch2" enforce_admins = false @@ -111,7 +111,7 @@ resource "github_branch_protection" "repo2-feature-branch2" { } } -resource "github_branch_protection" "repo2-feature-branch3" { +resource "github_branch_protection" "repo2-feature_SLASH_branch3" { repository_id = github_repository.repo2.node_id pattern = "feature/branch3" enforce_admins = true