generate github rulesets from repo flags, trim repo schema
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful

This commit is contained in:
2026-11-10 21:52:52 +11:00
parent a99c8f04fd
commit 132e93c0d4
6 changed files with 212 additions and 67 deletions
+55 -13
View File
@@ -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
+113 -11
View File
@@ -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
+26 -10
View File
@@ -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 {