Skip to content

Commit fc0c278

Browse files
Merge pull request #70 from mineiros-io/mariux/fix-multi-branch-protection
fix: handling multiple branch protections with dependencies to teams
2 parents e341508 + a539771 commit fc0c278

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.9.2]
11+
12+
### Fixed
13+
14+
- Fix terraform typing issue when defining branch protections for multiple branches
15+
1016
## [0.9.1]
1117

1218
### Added
@@ -250,11 +256,12 @@ Please review plans and report regressions and issues asap so we can improve doc
250256

251257
<!-- markdown-link-check-disable -->
252258

253-
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.1...HEAD
254-
[0.9.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.0...v0.9.1
259+
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.2...HEAD
260+
[0.9.2]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.1...v0.9.2
255261

256262
<!-- markdown-link-check-enable -->
257263

264+
[0.9.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.0...v0.9.1
258265
[0.9.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.8.0...v0.9.0
259266
[0.8.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.7.0...v0.8.0
260267
[0.7.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.6.1...v0.7.0

main.tf

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ locals {
2626
topics = concat(local.standard_topics, var.extra_topics)
2727
template = var.template == null ? [] : [var.template]
2828
issue_labels_create = var.issue_labels_create == null ? lookup(var.defaults, "issue_labels_create", local.issue_labels_create_computed) : var.issue_labels_create
29-
branch_protections_v0 = var.branch_protections == null ? [] : var.branch_protections
30-
branch_protections_v3 = var.branch_protections_v3 == null ? local.branch_protections_v0 : var.branch_protections_v3
29+
branch_protections_v3 = var.branch_protections_v3 == null ? var.branch_protections : var.branch_protections_v3
3130

3231
issue_labels_create_computed = local.has_issues || length(var.issue_labels) > 0
3332

@@ -39,7 +38,7 @@ locals {
3938
}
4039

4140
locals {
42-
branch_protections = [
41+
branch_protections = try([
4342
for b in local.branch_protections_v3 : merge({
4443
branch = null
4544
enforce_admins = null
@@ -48,7 +47,7 @@ locals {
4847
required_pull_request_reviews = {}
4948
restrictions = {}
5049
}, b)
51-
]
50+
], [])
5251

5352
required_status_checks = [
5453
for b in local.branch_protections :
@@ -188,7 +187,7 @@ resource "github_branch_protection_v3" "branch_protection" {
188187
content {
189188
dismiss_stale_reviews = required_pull_request_reviews.value.dismiss_stale_reviews
190189
dismissal_users = required_pull_request_reviews.value.dismissal_users
191-
dismissal_teams = required_pull_request_reviews.value.dismissal_teams
190+
dismissal_teams = [for t in required_pull_request_reviews.value.dismissal_teams : replace(lower(t), "/[^a-z0-9]/", "-")]
192191
require_code_owner_reviews = required_pull_request_reviews.value.require_code_owner_reviews
193192
required_approving_review_count = required_pull_request_reviews.value.required_approving_review_count
194193
}
@@ -199,8 +198,7 @@ resource "github_branch_protection_v3" "branch_protection" {
199198

200199
content {
201200
users = restrictions.value.users
202-
# TODO: try to convert teams to team-slug array
203-
teams = restrictions.value.teams
201+
teams = [for t in restrictions.value.teams : replace(lower(t), "/[^a-z0-9]/", "-")]
204202
apps = restrictions.value.apps
205203
}
206204
}

test/unit-complete/main.tf

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,20 @@ module "repository" {
9494
required_pull_request_reviews = {
9595
dismiss_stale_reviews = true
9696
dismissal_users = [var.team_user]
97-
dismissal_teams = [github_team.team.slug]
97+
dismissal_teams = [github_team.team.name]
9898
require_code_owner_reviews = true
9999
required_approving_review_count = 1
100100
}
101101

102102
restrictions = {
103103
users = [var.team_user]
104-
teams = [
105-
github_team.team.slug
106-
]
104+
teams = [github_team.team.name]
107105
}
106+
},
107+
{
108+
branch = github_branch.development.branch
109+
enforce_admins = true
110+
require_signed_commits = true
108111
}
109112
]
110113

@@ -122,6 +125,11 @@ module "repository" {
122125
projects = var.projects
123126
}
124127

128+
resource "github_branch" "development" {
129+
repository = module.repository.repository.name
130+
branch = "development"
131+
}
132+
125133
# ---------------------------------------------------------------------------------------------------------------------
126134
# TEST B
127135
# We are creating a repository using some defaults defined in

0 commit comments

Comments
 (0)