This repository has been archived on 2026-11-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
terraform-git/modules/github_org/main.tf
T
unkin-agent e7eee508d7
ci/woodpecker/pr/plan Pipeline failed
ci/woodpecker/pr/pre-commit Pipeline was successful
manage IUCLabs GitHub organisation
2026-11-09 23:53:49 +11:00

150 lines
4.6 KiB
Terraform

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" } },
)
]...)
repository_access = {
for name, r in var.repository : name => merge(
{ for e in r.codeowners : e => "push" },
{ for e in r.maintainers : e => "maintain" },
)
}
repository_ruleset = merge([
for name, r in var.repository : {
for rs in r.rulesets : "${name}/${rs}" => { repository = name, ruleset = var.ruleset[rs] }
}
]...)
}
resource "github_team" "this" {
for_each = var.team
name = each.value.name
description = each.value.description
privacy = each.value.privacy
}
resource "github_team_membership" "this" {
for_each = local.team_membership
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 = 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
delete_branch_on_merge = each.value.delete_branch_on_merge
auto_init = each.value.auto_init
}
resource "github_branch_default" "this" {
for_each = var.repository
repository = github_repository.this[each.key].name
branch = each.value.default_branch
}
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 = team.key
permission = team.value
}
}
depends_on = [github_team.this]
}
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}/${trimprefix(e, "@")}" : "@${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]
}