Skip to content
Closed
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
12 changes: 6 additions & 6 deletions examples/public-repository-complete-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}]
}
}
]

Expand Down
72 changes: 15 additions & 57 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
}

Expand Down
12 changes: 6 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
# }]
# }
# }
# ]
}
Expand Down