From 23c315a49aac2e5714911ae95a087a678ebf42bb Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 22:01:33 +1100 Subject: [PATCH] rename auto-init main to default_branch and reject invalid ruleset shapes --- modules/github_org/main.tf | 3 + .../github_org/tests/github_org.tftest.hcl | 63 +++++++++++++++++++ modules/github_org/variables.tf | 10 +++ 3 files changed, 76 insertions(+) diff --git a/modules/github_org/main.tf b/modules/github_org/main.tf index 51fefd9..686a773 100644 --- a/modules/github_org/main.tf +++ b/modules/github_org/main.tf @@ -117,6 +117,9 @@ resource "github_branch_default" "this" { repository = github_repository.this[each.key].name branch = each.value.default_branch + # auto_init only creates main; rename it instead of pointing at a missing branch + rename = true + wait_for_rename = true } resource "github_repository_collaborators" "this" { diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 993406e..0d7d4fc 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -230,3 +230,66 @@ run "unknown_ruleset_rejected" { expect_failures = [var.repository] } + +run "non_main_default_branch_renamed" { + command = plan + + variables { + repository = { + legacy = { name = "legacy", default_branch = "master", maintainers = ["carol"] } + } + } + + assert { + condition = ( + github_branch_default.this["legacy"].branch == "master" && + github_branch_default.this["legacy"].rename && + github_branch_default.this["legacy"].wait_for_rename + ) + error_message = "default_branch must rename the auto-init main and wait for it to propagate" + } + + assert { + condition = github_repository_file.codeowners["legacy"].branch == "master" + error_message = "CODEOWNERS must be committed to the renamed default branch" + } +} + +run "tag_ruleset_pull_request_rejected" { + command = plan + + variables { + ruleset = { + tagpr = { name = "tagpr", target = "tag", rules = { pull_request = {} } } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "tag_ruleset_status_checks_rejected" { + command = plan + + variables { + ruleset = { + tagci = { name = "tagci", target = "tag", rules = { required_status_checks = { contexts = ["ci"] } } } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "empty_status_check_contexts_rejected" { + command = plan + + variables { + ruleset = { + noci = { name = "noci", rules = { required_status_checks = { contexts = [] } } } + } + repository = {} + } + + expect_failures = [var.ruleset] +} diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index f9b80f7..4781959 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -49,6 +49,16 @@ variable "ruleset" { condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection"])) == 0 error_message = "Ruleset names branch-protection, required-checks and tag-protection are reserved for generated rulesets." } + + validation { + condition = alltrue([for rs in var.ruleset : rs.target == "branch" || (rs.rules.pull_request == null && rs.rules.required_status_checks == null)]) + error_message = "pull_request and required_status_checks are branch-only rules, not allowed on tag rulesets: ${join(", ", [for k, rs in var.ruleset : k if rs.target != "branch" && (rs.rules.pull_request != null || rs.rules.required_status_checks != null)])}." + } + + validation { + condition = alltrue([for rs in var.ruleset : rs.rules.required_status_checks == null || length(rs.rules.required_status_checks.contexts) > 0]) + error_message = "required_status_checks.contexts must not be empty: ${join(", ", [for k, rs in var.ruleset : k if rs.rules.required_status_checks != null && length(rs.rules.required_status_checks.contexts) == 0])}." + } } variable "repository" {