split github_org into team, repository and ruleset submodules
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful

This commit is contained in:
2026-11-10 22:32:17 +11:00
parent 23c315a49a
commit d6d608b2a1
15 changed files with 373 additions and 219 deletions
+2
View File
@@ -6,6 +6,8 @@ Manage Gitea resources, teams, repos, and Woodpecker CI via Terraform.
`config/github.com/IUCLabs/` feeds `modules/github_org` via `environments/github/iuclabs`, authenticated as a GitHub App (`kv/service/github/iuclabs/terraform`). Org membership invites are manual.
`modules/github_org` validates config and calls `modules/team` (team + memberships) and `modules/repository` (repo, default branch, collaborators, CODEOWNERS, rulesets via `modules/ruleset`).
`team/<name>.yaml`:
```yaml
+11 -197
View File
@@ -1,210 +1,24 @@
locals {
team_membership = merge([
for team, t in var.team : merge(
{ for u in t.members : "${team}/${u}" => { team = team, username = u, role = "member" } },
{ for u in t.maintainers : "${team}/${u}" => { team = team, username = u, role = "maintainer" } },
)
]...)
module "team" {
source = "./modules/team"
repository_access = {
for name, r in var.repository : name => merge(
{ for e in r.codeowners : e => "push" },
{ for e in r.maintainers : e => "maintain" },
)
}
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
},
{
for name, r in var.repository : "${name}/tag-protection" => {
repository = name
ruleset = {
name = "tag-protection"
target = "tag"
enforcement = "active"
conditions = { ref_name = { include = ["refs/tags/v*"], exclude = [] } }
rules = {
deletion = true
non_fast_forward = true
pull_request = null
required_status_checks = null
}
}
} if coalesce(r.tag_protection, !r.private)
}],
)...)
}
resource "github_team" "this" {
for_each = var.team
name = each.value.name
description = each.value.description
privacy = each.value.privacy
maintainers = each.value.maintainers
members = each.value.members
}
resource "github_team_membership" "this" {
for_each = local.team_membership
module "repository" {
source = "./modules/repository"
team_id = github_team.this[each.value.team].id
username = each.value.username
role = each.value.role
}
resource "github_repository" "this" {
for_each = var.repository
name = each.value.name
description = each.value.description
visibility = each.value.private ? "private" : "public"
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 = true
archive_on_destroy = true
}
resource "github_branch_default" "this" {
for_each = var.repository
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" {
for_each = { for name, access in local.repository_access : name => access if length(access) > 0 }
repository = github_repository.this[each.key].name
dynamic "user" {
for_each = { for e, p in each.value : e => p if !startswith(e, "@") }
content {
username = user.key
permission = user.value
}
}
dynamic "team" {
for_each = { for e, p in each.value : trimprefix(e, "@") => p if startswith(e, "@") }
content {
team_id = github_team.this[team.key].slug
permission = team.value
}
owner = var.owner
config = each.value
rulesets = { for rs in each.value.rulesets : rs => var.ruleset[rs] }
team_slugs = {
for e in concat(each.value.maintainers, each.value.codeowners) : trimprefix(e, "@") => module.team[trimprefix(e, "@")].slug if startswith(e, "@")
}
}
resource "github_repository_file" "codeowners" {
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
file = ".github/CODEOWNERS"
content = "* ${join(" ", [for e in each.value.maintainers : startswith(e, "@") ? "@${var.owner}/${github_team.this[trimprefix(e, "@")].slug}" : "@${e}"])}\n"
commit_message = "add initial CODEOWNERS"
overwrite_on_create = false
lifecycle {
ignore_changes = [content]
}
}
resource "github_repository_ruleset" "this" {
for_each = local.repository_ruleset
name = each.value.ruleset.name
repository = github_repository.this[each.value.repository].name
target = each.value.ruleset.target
enforcement = each.value.ruleset.enforcement
dynamic "conditions" {
for_each = each.value.ruleset.conditions[*]
content {
ref_name {
include = conditions.value.ref_name.include
exclude = conditions.value.ref_name.exclude
}
}
}
rules {
deletion = each.value.ruleset.rules.deletion
non_fast_forward = each.value.ruleset.rules.non_fast_forward
dynamic "pull_request" {
for_each = each.value.ruleset.rules.pull_request[*]
content {
required_approving_review_count = pull_request.value.required_approving_review_count
dismiss_stale_reviews_on_push = pull_request.value.dismiss_stale_reviews_on_push
require_code_owner_review = pull_request.value.require_code_owner_review
require_last_push_approval = pull_request.value.require_last_push_approval
required_review_thread_resolution = pull_request.value.required_review_thread_resolution
}
}
dynamic "required_status_checks" {
for_each = each.value.ruleset.rules.required_status_checks[*]
content {
strict_required_status_checks_policy = required_status_checks.value.strict
dynamic "required_check" {
for_each = toset(required_status_checks.value.contexts)
content {
context = required_check.value
}
}
}
}
}
depends_on = [github_repository_file.codeowners]
}
@@ -0,0 +1,131 @@
locals {
access = merge(
{ for e in var.config.codeowners : e => "push" },
{ for e in var.config.maintainers : e => "maintain" },
)
default_branch_rules = {
target = "branch"
enforcement = "active"
conditions = { ref_name = { include = ["~DEFAULT_BRANCH"], exclude = [] } }
}
generated_rulesets = { for k, v in {
branch-protection = {
enabled = coalesce(var.config.branch_protection, !var.config.private)
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
}
})
}
required-checks = {
enabled = length(var.config.required_checks) > 0
ruleset = merge(local.default_branch_rules, {
name = "required-checks"
rules = {
deletion = false
non_fast_forward = false
pull_request = null
required_status_checks = { contexts = var.config.required_checks, strict = true }
}
})
}
tag-protection = {
enabled = coalesce(var.config.tag_protection, !var.config.private)
ruleset = {
name = "tag-protection"
target = "tag"
enforcement = "active"
conditions = { ref_name = { include = ["refs/tags/v*"], exclude = [] } }
rules = {
deletion = true
non_fast_forward = true
pull_request = null
required_status_checks = null
}
}
}
} : k => v.ruleset if v.enabled }
}
resource "github_repository" "this" {
name = var.config.name
description = var.config.description
visibility = var.config.private ? "private" : "public"
has_issues = true
has_wiki = false
has_projects = false
allow_squash_merge = var.config.merge_type == "squash"
allow_merge_commit = var.config.merge_type == "merge"
allow_rebase_merge = var.config.merge_type == "rebase"
delete_branch_on_merge = var.config.delete_branch_on_merge
auto_init = true
archive_on_destroy = true
}
resource "github_branch_default" "this" {
repository = github_repository.this.name
branch = var.config.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" {
count = length(local.access) > 0 ? 1 : 0
repository = github_repository.this.name
dynamic "user" {
for_each = { for e, p in local.access : e => p if !startswith(e, "@") }
content {
username = user.key
permission = user.value
}
}
dynamic "team" {
for_each = { for e, p in local.access : trimprefix(e, "@") => p if startswith(e, "@") }
content {
team_id = var.team_slugs[team.key]
permission = team.value
}
}
}
resource "github_repository_file" "codeowners" {
count = length(var.config.maintainers) > 0 ? 1 : 0
repository = github_repository.this.name
branch = github_branch_default.this.branch
file = ".github/CODEOWNERS"
content = "* ${join(" ", [for e in var.config.maintainers : startswith(e, "@") ? "@${var.owner}/${var.team_slugs[trimprefix(e, "@")]}" : "@${e}"])}\n"
commit_message = "add initial CODEOWNERS"
overwrite_on_create = false
lifecycle {
ignore_changes = [content]
}
}
module "ruleset" {
source = "../ruleset"
for_each = merge(var.rulesets, local.generated_rulesets)
repository = github_repository.this.name
ruleset = each.value
depends_on = [github_repository_file.codeowners]
}
@@ -0,0 +1,19 @@
output "repository" {
value = github_repository.this
}
output "branch_default" {
value = github_branch_default.this
}
output "codeowners" {
value = one(github_repository_file.codeowners)
}
output "access" {
value = local.access
}
output "rulesets" {
value = { for k, m in module.ruleset : k => m.ruleset }
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.10"
required_providers {
github = {
source = "integrations/github"
version = "6.13.0"
}
}
}
@@ -0,0 +1,31 @@
variable "owner" {
type = string
}
variable "config" {
type = object({
name = string
description = string
private = bool
default_branch = string
maintainers = list(string)
codeowners = list(string)
merge_type = string
delete_branch_on_merge = bool
branch_protection = bool
tag_protection = bool
required_checks = list(string)
})
}
variable "rulesets" {
description = "Custom ruleset definitions this repository references, keyed by name"
type = any
default = {}
}
variable "team_slugs" {
description = "Slugs of the teams this repository references, keyed by team name"
type = map(string)
default = {}
}
@@ -0,0 +1,45 @@
resource "github_repository_ruleset" "this" {
name = var.ruleset.name
repository = var.repository
target = var.ruleset.target
enforcement = var.ruleset.enforcement
dynamic "conditions" {
for_each = var.ruleset.conditions[*]
content {
ref_name {
include = conditions.value.ref_name.include
exclude = conditions.value.ref_name.exclude
}
}
}
rules {
deletion = var.ruleset.rules.deletion
non_fast_forward = var.ruleset.rules.non_fast_forward
dynamic "pull_request" {
for_each = var.ruleset.rules.pull_request[*]
content {
required_approving_review_count = pull_request.value.required_approving_review_count
dismiss_stale_reviews_on_push = pull_request.value.dismiss_stale_reviews_on_push
require_code_owner_review = pull_request.value.require_code_owner_review
require_last_push_approval = pull_request.value.require_last_push_approval
required_review_thread_resolution = pull_request.value.required_review_thread_resolution
}
}
dynamic "required_status_checks" {
for_each = var.ruleset.rules.required_status_checks[*]
content {
strict_required_status_checks_policy = required_status_checks.value.strict
dynamic "required_check" {
for_each = toset(required_status_checks.value.contexts)
content {
context = required_check.value
}
}
}
}
}
}
@@ -0,0 +1,3 @@
output "ruleset" {
value = github_repository_ruleset.this
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.10"
required_providers {
github = {
source = "integrations/github"
version = "6.13.0"
}
}
}
@@ -0,0 +1,32 @@
variable "repository" {
type = string
}
variable "ruleset" {
type = object({
name = string
target = string
enforcement = string
conditions = object({
ref_name = object({
include = list(string)
exclude = list(string)
})
})
rules = object({
deletion = bool
non_fast_forward = bool
pull_request = object({
required_approving_review_count = number
dismiss_stale_reviews_on_push = bool
require_code_owner_review = bool
require_last_push_approval = bool
required_review_thread_resolution = bool
})
required_status_checks = object({
contexts = list(string)
strict = bool
})
})
})
}
+16
View File
@@ -0,0 +1,16 @@
resource "github_team" "this" {
name = var.name
description = var.description
privacy = var.privacy
}
resource "github_team_membership" "this" {
for_each = merge(
{ for u in var.members : u => "member" },
{ for u in var.maintainers : u => "maintainer" },
)
team_id = github_team.this.id
username = each.key
role = each.value
}
@@ -0,0 +1,11 @@
output "id" {
value = github_team.this.id
}
output "slug" {
value = github_team.this.slug
}
output "membership" {
value = github_team_membership.this
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.10"
required_providers {
github = {
source = "integrations/github"
version = "6.13.0"
}
}
}
@@ -0,0 +1,23 @@
variable "name" {
type = string
}
variable "description" {
type = string
default = ""
}
variable "privacy" {
type = string
default = "closed"
}
variable "maintainers" {
type = list(string)
default = []
}
variable "members" {
type = list(string)
default = []
}
+22 -22
View File
@@ -24,36 +24,36 @@ run "public_repo_defaults" {
}
assert {
condition = github_repository_file.codeowners[".github"].content == "* @IUCLabs/maintainers-slug @carol\n"
condition = module.repository[".github"].codeowners.content == "* @IUCLabs/maintainers-slug @carol\n"
error_message = "CODEOWNERS must list maintainers, teams by owner-qualified slug"
}
assert {
condition = local.repository_access[".github"] == { "@maintainers" = "maintain", carol = "maintain", dave = "push" }
condition = module.repository[".github"].access == { "@maintainers" = "maintain", carol = "maintain", dave = "push" }
error_message = "maintainers get maintain (winning over codeowners), codeowners get push"
}
assert {
condition = keys(github_repository_ruleset.this) == [".github/branch-protection", ".github/tag-protection"] && length(github_team_membership.this) == 2
condition = keys(module.repository[".github"].rulesets) == ["branch-protection", "tag-protection"] && length(module.team["maintainers"].membership) == 2
error_message = "public repo must get only the generated branch-protection and tag-protection rulesets"
}
assert {
condition = (
github_repository_ruleset.this[".github/tag-protection"].target == "tag" &&
github_repository_ruleset.this[".github/tag-protection"].enforcement == "active" &&
github_repository_ruleset.this[".github/tag-protection"].conditions[0].ref_name[0].include == tolist(["refs/tags/v*"]) &&
github_repository_ruleset.this[".github/tag-protection"].rules[0].deletion &&
github_repository_ruleset.this[".github/tag-protection"].rules[0].non_fast_forward
module.repository[".github"].rulesets["tag-protection"].target == "tag" &&
module.repository[".github"].rulesets["tag-protection"].enforcement == "active" &&
module.repository[".github"].rulesets["tag-protection"].conditions[0].ref_name[0].include == tolist(["refs/tags/v*"]) &&
module.repository[".github"].rulesets["tag-protection"].rules[0].deletion &&
module.repository[".github"].rulesets["tag-protection"].rules[0].non_fast_forward
)
error_message = "tag-protection ruleset must make refs/tags/v* undeletable and immutable"
}
assert {
condition = (
github_repository.this[".github"].allow_squash_merge &&
!github_repository.this[".github"].allow_merge_commit &&
!github_repository.this[".github"].allow_rebase_merge
module.repository[".github"].repository.allow_squash_merge &&
!module.repository[".github"].repository.allow_merge_commit &&
!module.repository[".github"].repository.allow_rebase_merge
)
error_message = "merge_type defaults to squash only"
}
@@ -69,7 +69,7 @@ run "private_repo_defaults" {
}
assert {
condition = length(github_repository_ruleset.this) == 0 && length(github_repository_file.codeowners) == 1
condition = length(module.repository["secret"].rulesets) == 0 && module.repository["secret"].codeowners != null
error_message = "private repo defaults to no rulesets but still gets CODEOWNERS"
}
}
@@ -86,8 +86,8 @@ run "merge_type_rebase" {
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
module.repository["r"].repository.allow_rebase_merge && !module.repository["r"].repository.allow_squash_merge && !module.repository["r"].repository.allow_merge_commit &&
module.repository["m"].repository.allow_merge_commit && !module.repository["m"].repository.allow_squash_merge && !module.repository["m"].repository.allow_rebase_merge
)
error_message = "merge_type must enable exactly the matching allow_* flag"
}
@@ -115,17 +115,17 @@ run "generated_and_custom_rulesets" {
}
assert {
condition = toset(keys(github_repository_ruleset.this)) == toset(["checked/required-checks", "checked/signed"])
condition = toset(keys(module.repository["checked"].rulesets)) == toset(["required-checks", "signed"])
error_message = "required_checks with protections off 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
condition = module.repository["checked"].rulesets["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
condition = length(module.repository["checked"].rulesets["required-checks"].rules[0].pull_request) == 0
error_message = "required-checks ruleset must not carry pull_request rules"
}
}
@@ -152,7 +152,7 @@ run "public_tag_protection_only" {
}
assert {
condition = keys(github_repository_ruleset.this) == ["tags/tag-protection"]
condition = keys(module.repository["tags"].rulesets) == ["tag-protection"]
error_message = "tag_protection defaults on for public repos independently of branch_protection"
}
}
@@ -242,15 +242,15 @@ run "non_main_default_branch_renamed" {
assert {
condition = (
github_branch_default.this["legacy"].branch == "master" &&
github_branch_default.this["legacy"].rename &&
github_branch_default.this["legacy"].wait_for_rename
module.repository["legacy"].branch_default.branch == "master" &&
module.repository["legacy"].branch_default.rename &&
module.repository["legacy"].branch_default.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"
condition = module.repository["legacy"].codeowners.branch == "master"
error_message = "CODEOWNERS must be committed to the renamed default branch"
}
}