diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index b318ddd3..98227ac1 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -76,23 +76,23 @@ module "repository" { enforce_admins = true require_signed_commits = true - required_status_checks = [{ + required_status_checks = { strict = true contexts = ["ci/travis"] - }] + } - required_pull_request_reviews = [{ + required_pull_request_reviews = { dismiss_stale_reviews = true dismissal_users = ["terraform-test-user-1"] dismissal_teams = [github_team.team.slug] require_code_owner_reviews = true required_approving_review_count = 1 - }] + } - restrictions = [{ + restrictions = { users = ["terraform-test-user"] teams = ["team-1"] - }] + } } ] diff --git a/main.tf b/main.tf index 873a1cf7..89a6b7cc 100644 --- a/main.tf +++ b/main.tf @@ -23,41 +23,11 @@ locals { branch = null enforce_admins = null require_signed_commits = null - required_status_checks = [] - required_pull_request_reviews = [] - restrictions = [] + required_status_checks = {} + required_pull_request_reviews = {} + restrictions = {} }, b) ] - - required_status_checks = [ - for b in local.branch_protection_rules : [ - for r in b.required_status_checks : merge({ - strict = null - contexts = [] - }, r) - ] - ] - - required_pull_request_reviews = [ - for b in local.branch_protection_rules : [ - for r in b.required_pull_request_reviews : merge({ - dismiss_stale_reviews = true - dismissal_users = [] - dismissal_teams = [] - require_code_owner_reviews = null - required_approving_review_count = null - }, r) - ] - ] - - restrictions = [ - for b in local.branch_protection_rules : [ - for r in b.restrictions : merge({ - users = [] - teams = [] - }, r) - ] - ] } resource "github_repository" "repository" { @@ -107,34 +77,22 @@ resource "github_branch_protection" "branch_protection_rule" { enforce_admins = local.branch_protection_rules[count.index].enforce_admins require_signed_commits = local.branch_protection_rules[count.index].require_signed_commits - dynamic "required_status_checks" { - for_each = local.required_status_checks[count.index] - - content { - strict = required_status_checks.value.strict - contexts = required_status_checks.value.contexts - } + required_status_checks { + strict = lookup(local.branch_protection_rules[count.index].required_status_checks, "strict", null) + contexts = lookup(local.branch_protection_rules[count.index].required_status_checks, "contexts", []) } - dynamic "required_pull_request_reviews" { - for_each = local.required_pull_request_reviews[count.index] - - content { - dismiss_stale_reviews = required_pull_request_reviews.value.dismiss_stale_reviews - dismissal_users = required_pull_request_reviews.value.dismissal_users - dismissal_teams = required_pull_request_reviews.value.dismissal_teams - require_code_owner_reviews = required_pull_request_reviews.value.require_code_owner_reviews - required_approving_review_count = required_pull_request_reviews.value.required_approving_review_count - } + required_pull_request_reviews { + dismiss_stale_reviews = lookup(local.branch_protection_rules[count.index].required_pull_request_reviews, "dismiss_stale_reviews", true) + dismissal_users = lookup(local.branch_protection_rules[count.index].required_pull_request_reviews, "dismissal_users", []) + dismissal_teams = lookup(local.branch_protection_rules[count.index].required_pull_request_reviews, "dismissal_teams", []) + require_code_owner_reviews = lookup(local.branch_protection_rules[count.index].required_pull_request_reviews, "require_code_owner_reviews", null) + required_approving_review_count = lookup(local.branch_protection_rules[count.index].required_pull_request_reviews, "required_approving_review_count", null) } - dynamic "restrictions" { - for_each = local.restrictions[count.index] - - content { - users = restrictions.value.users - teams = restrictions.value.teams - } + restrictions { + users = lookup(local.branch_protection_rules[count.index].restrictions, "users", null) + teams = lookup(local.branch_protection_rules[count.index].restrictions, "teams", null) } } diff --git a/variables.tf b/variables.tf index 6b031c86..5b426476 100644 --- a/variables.tf +++ b/variables.tf @@ -202,23 +202,23 @@ variable "branch_protection_rules" { # enforce_admins = true # require_signed_commits = true # - # required_status_checks = [{ + # required_status_checks = { # strict = false # contexts = ["ci/travis"] - # }] + # } # - # required_pull_request_reviews = [{ + # required_pull_request_reviews = { # dismiss_stale_reviews = true # dismissal_users = ["user1", "user2"] # dismissal_teams = ["team-slug-1", "team-slug-2"] # require_code_owner_reviews = true # required_approving_review_count = 1 - # }] + # } # - # restrictions = [{ + # restrictions = { # users = ["user1"] # teams = ["team-slug-1"] - # }] + # } # } # ] }