From 037d37945049276dbad00fddce60d66979418010 Mon Sep 17 00:00:00 2001 From: yoanm Date: Sun, 21 Jan 2024 08:16:29 +0100 Subject: [PATCH 1/4] Fix branch protection pattern to TF ress ID collision --- core/gh2tf_repo_mapper.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/gh2tf_repo_mapper.go b/core/gh2tf_repo_mapper.go index 4546204..f75a505 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 +const 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 { From b19e08bdd4a265d1a32efbc53dd556dff7c65662 Mon Sep 17 00:00:00 2001 From: yoanm Date: Sun, 21 Jan 2024 08:18:52 +0100 Subject: [PATCH 2/4] Fix --- core/gh2tf_repo_mapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/gh2tf_repo_mapper.go b/core/gh2tf_repo_mapper.go index f75a505..398cc12 100644 --- a/core/gh2tf_repo_mapper.go +++ b/core/gh2tf_repo_mapper.go @@ -30,7 +30,7 @@ var falseString = "false" const patternToIdReplacer = strings.NewReplacer( ".", "_DOT_", "/", "_SLASH_", - "\\", "_ESC_" + "\\", "_ESC_", // fnmatch special chars "*", "_STAR_", "[", "_SEQ-O_", From d66f1b411703ce1d5269f555afa7ba6309ce69f0 Mon Sep 17 00:00:00 2001 From: yoanm Date: Sun, 21 Jan 2024 08:19:52 +0100 Subject: [PATCH 3/4] Fix --- core/gh2tf_repo_mapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/gh2tf_repo_mapper.go b/core/gh2tf_repo_mapper.go index 398cc12..25d1292 100644 --- a/core/gh2tf_repo_mapper.go +++ b/core/gh2tf_repo_mapper.go @@ -27,7 +27,7 @@ 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 -const patternToIdReplacer = strings.NewReplacer( +var patternToIdReplacer = strings.NewReplacer( ".", "_DOT_", "/", "_SLASH_", "\\", "_ESC_", From f5493b1ec0034b33959f086c01e8000c223ad181 Mon Sep 17 00:00:00 2001 From: yo May Date: Sun, 21 Jan 2024 08:48:33 +0100 Subject: [PATCH 4/4] Fix --- core/gh2tf_repo_mapper.go | 4 ++-- core/testdata/repo1.full.golden.tf | 4 ++-- core/testdata/repo2.full.golden.tf | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/gh2tf_repo_mapper.go b/core/gh2tf_repo_mapper.go index 25d1292..91b1780 100644 --- a/core/gh2tf_repo_mapper.go +++ b/core/gh2tf_repo_mapper.go @@ -26,8 +26,8 @@ const ( 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( +// 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_", 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