Skip to content

Commit

Permalink
Merge pull request #70 from mineiros-io/mariux/fix-multi-branch-prote…
Browse files Browse the repository at this point in the history
…ction

fix: handling multiple branch protections with dependencies to teams
  • Loading branch information
soerenmartius committed Jun 2, 2021
2 parents e341508 + a539771 commit fc0c278
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.2]

### Fixed

- Fix terraform typing issue when defining branch protections for multiple branches

## [0.9.1]

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

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

[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.1...HEAD
[0.9.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.0...v0.9.1
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.2...HEAD
[0.9.2]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.1...v0.9.2

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

[0.9.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.9.0...v0.9.1
[0.9.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.8.0...v0.9.0
[0.8.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.7.0...v0.8.0
[0.7.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.6.1...v0.7.0
Expand Down
12 changes: 5 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ locals {
topics = concat(local.standard_topics, var.extra_topics)
template = var.template == null ? [] : [var.template]
issue_labels_create = var.issue_labels_create == null ? lookup(var.defaults, "issue_labels_create", local.issue_labels_create_computed) : var.issue_labels_create
branch_protections_v0 = var.branch_protections == null ? [] : var.branch_protections
branch_protections_v3 = var.branch_protections_v3 == null ? local.branch_protections_v0 : var.branch_protections_v3
branch_protections_v3 = var.branch_protections_v3 == null ? var.branch_protections : var.branch_protections_v3

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

Expand All @@ -39,7 +38,7 @@ locals {
}

locals {
branch_protections = [
branch_protections = try([
for b in local.branch_protections_v3 : merge({
branch = null
enforce_admins = null
Expand All @@ -48,7 +47,7 @@ locals {
required_pull_request_reviews = {}
restrictions = {}
}, b)
]
], [])

required_status_checks = [
for b in local.branch_protections :
Expand Down Expand Up @@ -188,7 +187,7 @@ resource "github_branch_protection_v3" "branch_protection" {
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
dismissal_teams = [for t in required_pull_request_reviews.value.dismissal_teams : replace(lower(t), "/[^a-z0-9]/", "-")]
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
}
Expand All @@ -199,8 +198,7 @@ resource "github_branch_protection_v3" "branch_protection" {

content {
users = restrictions.value.users
# TODO: try to convert teams to team-slug array
teams = restrictions.value.teams
teams = [for t in restrictions.value.teams : replace(lower(t), "/[^a-z0-9]/", "-")]
apps = restrictions.value.apps
}
}
Expand Down
16 changes: 12 additions & 4 deletions test/unit-complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,20 @@ module "repository" {
required_pull_request_reviews = {
dismiss_stale_reviews = true
dismissal_users = [var.team_user]
dismissal_teams = [github_team.team.slug]
dismissal_teams = [github_team.team.name]
require_code_owner_reviews = true
required_approving_review_count = 1
}

restrictions = {
users = [var.team_user]
teams = [
github_team.team.slug
]
teams = [github_team.team.name]
}
},
{
branch = github_branch.development.branch
enforce_admins = true
require_signed_commits = true
}
]

Expand All @@ -122,6 +125,11 @@ module "repository" {
projects = var.projects
}

resource "github_branch" "development" {
repository = module.repository.repository.name
branch = "development"
}

# ---------------------------------------------------------------------------------------------------------------------
# TEST B
# We are creating a repository using some defaults defined in
Expand Down

0 comments on commit fc0c278

Please sign in to comment.