Skip to content

Commit

Permalink
Refactor branch protection logic to remove archived repository check …
Browse files Browse the repository at this point in the history
…and add require_last_push_approval variable for improved clarity and configurability
  • Loading branch information
Dave Arnold committed Feb 19, 2025
1 parent c381b76 commit a3f229d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 2 additions & 6 deletions branch_protection.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ locals {
)
}

locals {
archived_repo = var.create_repo ? github_repository.repo[0].archived : data.github_repository.existing[0].archived
}
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection
resource "github_branch_protection" "protection" {
for_each = {
for k, v in local.branch_protection_rules : k => v
if var.enforce_prs && !local.archived_repo
for k, v in local.branch_protection_rules : k => v if var.enforce_prs
}

repository_id = var.create_repo ? github_repository.repo[0].node_id : data.github_repository.existing[0].node_id
Expand All @@ -42,7 +38,7 @@ resource "github_branch_protection" "protection" {
required_approving_review_count = var.github_required_approving_review_count
dismiss_stale_reviews = var.github_dismiss_stale_reviews
require_code_owner_reviews = var.github_require_code_owner_reviews
require_last_push_approval = false
require_last_push_approval = var.require_last_push_approval
}

dynamic "required_status_checks" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,10 @@ variable "require_signed_commits" {
description = "Whether to require signed commits for the default branch"
type = bool
default = false
}

variable require_last_push_approval {
description = "Require approval from the last pusher"
type = bool
default = false
}

0 comments on commit a3f229d

Please sign in to comment.