From 132e93c0d4a185c5828822db357d89ffa7221108 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 21:52:52 +1100 Subject: [PATCH] generate github rulesets from repo flags, trim repo schema --- README.md | 36 ++--- .../IUCLabs/repository/.github.yaml | 3 - .../github.com/IUCLabs/ruleset/default.yaml | 12 -- modules/github_org/main.tf | 68 ++++++++-- .../github_org/tests/github_org.tftest.hcl | 124 ++++++++++++++++-- modules/github_org/variables.tf | 36 +++-- 6 files changed, 212 insertions(+), 67 deletions(-) delete mode 100644 config/github.com/IUCLabs/ruleset/default.yaml diff --git a/README.md b/README.md index 222d223..b0151dd 100644 --- a/README.md +++ b/README.md @@ -15,33 +15,33 @@ maintainers: [neoloc] members: [] ``` -`ruleset/.yaml` (applied per repository; org rulesets need a paid plan): +`repository/.yaml` (every field, with defaults): + +```yaml +description: "" +private: true +default_branch: main +maintainers: [] # users or @team: maintain access, seed initial .github/CODEOWNERS +codeowners: [] # push access, for adding to CODEOWNERS later +merge_type: squash # squash | merge | rebase +delete_branch_on_merge: true +branch_protection: !private # generated `branch-protection` ruleset on the default branch +required_checks: [] # generated `required-checks` ruleset (strict) +rulesets: [] # custom rulesets from ruleset/ +``` + +`ruleset/.yaml` (custom extras; `branch-protection` and `required-checks` are reserved): ```yaml target: branch enforcement: active conditions: ref_name: - include: ["~DEFAULT_BRANCH"] + include: ["refs/heads/release/*"] exclude: [] rules: deletion: true non_fast_forward: true - pull_request: - required_approving_review_count: 0 - dismiss_stale_reviews_on_push: true - required_status_checks: - contexts: [ci/woodpecker] ``` -`repository/.yaml` (squash-only, delete branch on merge, `main` by default): - -```yaml -description: "IUCLabs organisation profile" -private: false -rulesets: [default] -maintainers: ["@maintainers"] -codeowners: [] -``` - -`maintainers` (users or `@team`) get `maintain` and seed the initial `.github/CODEOWNERS` on `auto_init` repositories, which is then left to humans. `codeowners` get `push` so they can be added to CODEOWNERS later. Private repositories cannot use rulesets on the free plan; plan fails if they do. +Private repositories cannot use `branch_protection`, `required_checks` or `rulesets` on the free plan; plan fails if they do. diff --git a/config/github.com/IUCLabs/repository/.github.yaml b/config/github.com/IUCLabs/repository/.github.yaml index b08224c..9a8a208 100644 --- a/config/github.com/IUCLabs/repository/.github.yaml +++ b/config/github.com/IUCLabs/repository/.github.yaml @@ -1,7 +1,4 @@ description: "IUCLabs organisation profile" private: false -rulesets: - - default maintainers: - "@maintainers" -codeowners: [] diff --git a/config/github.com/IUCLabs/ruleset/default.yaml b/config/github.com/IUCLabs/ruleset/default.yaml deleted file mode 100644 index 1715997..0000000 --- a/config/github.com/IUCLabs/ruleset/default.yaml +++ /dev/null @@ -1,12 +0,0 @@ -target: branch -enforcement: active -conditions: - ref_name: - include: ["~DEFAULT_BRANCH"] - exclude: [] -rules: - deletion: true - non_fast_forward: true - pull_request: - required_approving_review_count: 0 - dismiss_stale_reviews_on_push: true diff --git a/modules/github_org/main.tf b/modules/github_org/main.tf index 293d971..09ce5fc 100644 --- a/modules/github_org/main.tf +++ b/modules/github_org/main.tf @@ -13,11 +13,53 @@ locals { ) } - repository_ruleset = merge([ - for name, r in var.repository : { - for rs in r.rulesets : "${name}/${rs}" => { repository = name, ruleset = var.ruleset[rs] } - } - ]...) + default_branch_rules = { + target = "branch" + enforcement = "active" + conditions = { ref_name = { include = ["~DEFAULT_BRANCH"], exclude = [] } } + } + + repository_ruleset = merge(concat( + [ + for name, r in var.repository : { + for rs in r.rulesets : "${name}/${rs}" => { repository = name, ruleset = var.ruleset[rs] } + } + ], + [{ + for name, r in var.repository : "${name}/branch-protection" => { + repository = name + ruleset = merge(local.default_branch_rules, { + name = "branch-protection" + rules = { + deletion = true + non_fast_forward = true + pull_request = { + required_approving_review_count = 0 + dismiss_stale_reviews_on_push = true + require_code_owner_review = false + require_last_push_approval = false + required_review_thread_resolution = false + } + required_status_checks = null + } + }) + } if coalesce(r.branch_protection, !r.private) + }, + { + for name, r in var.repository : "${name}/required-checks" => { + repository = name + ruleset = merge(local.default_branch_rules, { + name = "required-checks" + rules = { + deletion = false + non_fast_forward = false + pull_request = null + required_status_checks = { contexts = r.required_checks, strict = true } + } + }) + } if length(r.required_checks) > 0 + }], + )...) } resource "github_team" "this" { @@ -42,14 +84,14 @@ resource "github_repository" "this" { name = each.value.name description = each.value.description visibility = each.value.private ? "private" : "public" - has_issues = each.value.has_issues - has_wiki = each.value.has_wiki - has_projects = each.value.has_projects - allow_squash_merge = each.value.allow_squash_merge - allow_merge_commit = each.value.allow_merge_commit - allow_rebase_merge = each.value.allow_rebase_merge + has_issues = true + has_wiki = false + has_projects = false + allow_squash_merge = each.value.merge_type == "squash" + allow_merge_commit = each.value.merge_type == "merge" + allow_rebase_merge = each.value.merge_type == "rebase" delete_branch_on_merge = each.value.delete_branch_on_merge - auto_init = each.value.auto_init + auto_init = true archive_on_destroy = true } @@ -83,7 +125,7 @@ resource "github_repository_collaborators" "this" { } resource "github_repository_file" "codeowners" { - for_each = { for name, r in var.repository : name => r if length(r.maintainers) > 0 && r.auto_init } + for_each = { for name, r in var.repository : name => r if length(r.maintainers) > 0 } repository = github_repository.this[each.key].name branch = github_branch_default.this[each.key].branch diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 3c5e53b..f839c1c 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -10,16 +10,16 @@ variables { maintainers = { name = "maintainers", maintainers = ["alice"], members = ["bob"] } } ruleset = { - default = { name = "default", rules = { deletion = true, pull_request = {} } } + signed = { name = "signed", rules = { deletion = true, pull_request = {} } } } } -run "public_repo_with_ruleset" { +run "public_repo_defaults" { command = plan variables { repository = { - ".github" = { name = ".github", private = false, rulesets = ["default"], maintainers = ["@maintainers", "carol"], codeowners = ["carol", "dave"] } + ".github" = { name = ".github", private = false, maintainers = ["@maintainers", "carol"], codeowners = ["carol", "dave"] } } } @@ -34,38 +34,140 @@ run "public_repo_with_ruleset" { } assert { - condition = length(github_repository_ruleset.this) == 1 && length(github_team_membership.this) == 2 - error_message = "expected one ruleset and two team memberships" + condition = keys(github_repository_ruleset.this) == [".github/branch-protection"] && length(github_team_membership.this) == 2 + error_message = "public repo must get only the generated branch-protection ruleset" + } + + assert { + condition = ( + github_repository.this[".github"].allow_squash_merge && + !github_repository.this[".github"].allow_merge_commit && + !github_repository.this[".github"].allow_rebase_merge + ) + error_message = "merge_type defaults to squash only" } } -run "no_codeowners_without_auto_init" { +run "private_repo_defaults" { command = plan variables { repository = { - empty = { name = "empty", private = false, auto_init = false, maintainers = ["@maintainers"] } + secret = { name = "secret", maintainers = ["@maintainers"] } } } assert { - condition = length(github_repository_file.codeowners) == 0 - error_message = "CODEOWNERS needs an initialised default branch" + condition = length(github_repository_ruleset.this) == 0 && length(github_repository_file.codeowners) == 1 + error_message = "private repo defaults to no rulesets but still gets CODEOWNERS" } } -run "private_repo_with_ruleset_rejected" { +run "merge_type_rebase" { command = plan variables { repository = { - secret = { name = "secret", private = true, rulesets = ["default"] } + r = { name = "r", merge_type = "rebase" } + m = { name = "m", merge_type = "merge" } + } + } + + assert { + condition = ( + github_repository.this["r"].allow_rebase_merge && !github_repository.this["r"].allow_squash_merge && !github_repository.this["r"].allow_merge_commit && + github_repository.this["m"].allow_merge_commit && !github_repository.this["m"].allow_squash_merge && !github_repository.this["m"].allow_rebase_merge + ) + error_message = "merge_type must enable exactly the matching allow_* flag" + } +} + +run "invalid_merge_type_rejected" { + command = plan + + variables { + repository = { + bad = { name = "bad", merge_type = "fast-forward" } } } expect_failures = [var.repository] } +run "generated_and_custom_rulesets" { + command = plan + + variables { + repository = { + checked = { name = "checked", private = false, branch_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] } + } + } + + assert { + condition = toset(keys(github_repository_ruleset.this)) == toset(["checked/required-checks", "checked/signed"]) + error_message = "required_checks without branch_protection yields only required-checks plus custom rulesets" + } + + assert { + condition = github_repository_ruleset.this["checked/required-checks"].rules[0].required_status_checks[0].strict_required_status_checks_policy + error_message = "required-checks ruleset must be strict" + } + + assert { + condition = length(github_repository_ruleset.this["checked/required-checks"].rules[0].pull_request) == 0 + error_message = "required-checks ruleset must not carry pull_request rules" + } +} + +run "private_branch_protection_rejected" { + command = plan + + variables { + repository = { + secret = { name = "secret", branch_protection = true } + } + } + + expect_failures = [var.repository] +} + +run "private_required_checks_rejected" { + command = plan + + variables { + repository = { + secret = { name = "secret", required_checks = ["ci/woodpecker"] } + } + } + + expect_failures = [var.repository] +} + +run "private_rulesets_rejected" { + command = plan + + variables { + repository = { + secret = { name = "secret", rulesets = ["signed"] } + } + } + + expect_failures = [var.repository] +} + +run "reserved_ruleset_name_rejected" { + command = plan + + variables { + ruleset = { + branch-protection = { name = "branch-protection", rules = { deletion = true } } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + run "unknown_ruleset_rejected" { command = plan diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index e79ef15..14ce930 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -44,6 +44,11 @@ variable "ruleset" { }) })) default = {} + + validation { + condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks"])) == 0 + error_message = "Ruleset names branch-protection and required-checks are reserved for generated rulesets." + } } variable "repository" { @@ -52,24 +57,35 @@ variable "repository" { name = string description = optional(string, "") private = optional(bool, true) - rulesets = optional(list(string), []) + default_branch = optional(string, "main") maintainers = optional(list(string), []) codeowners = optional(list(string), []) - default_branch = optional(string, "main") - has_issues = optional(bool, true) - has_wiki = optional(bool, false) - has_projects = optional(bool, false) - allow_squash_merge = optional(bool, true) - allow_merge_commit = optional(bool, false) - allow_rebase_merge = optional(bool, false) + merge_type = optional(string, "squash") delete_branch_on_merge = optional(bool, true) - auto_init = optional(bool, true) + branch_protection = optional(bool) + required_checks = optional(list(string), []) + rulesets = optional(list(string), []) })) default = {} + validation { + condition = alltrue([for repo in var.repository : contains(["squash", "merge", "rebase"], repo.merge_type)]) + error_message = "merge_type must be squash, merge or rebase: ${join(", ", [for name, repo in var.repository : name if !contains(["squash", "merge", "rebase"], repo.merge_type)])}." + } + + validation { + condition = alltrue([for repo in var.repository : !(repo.private && coalesce(repo.branch_protection, false))]) + error_message = "branch_protection is not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && coalesce(repo.branch_protection, false)])}." + } + + validation { + condition = alltrue([for repo in var.repository : !(repo.private && length(repo.required_checks) > 0)]) + error_message = "required_checks are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.required_checks) > 0])}." + } + validation { condition = alltrue([for repo in var.repository : !(repo.private && length(repo.rulesets) > 0)]) - error_message = "Rulesets are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.rulesets) > 0])}." + error_message = "rulesets are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.rulesets) > 0])}." } validation {