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 branch protection pattern to TF ress ID collision #19

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion core/gh2tf_repo_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/testdata/repo1.full.golden.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/testdata/repo2.full.golden.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down