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/modules/repository/main.tf
T
unkin-agent d6d608b2a1
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful
split github_org into team, repository and ruleset submodules
2026-11-10 22:32:17 +11:00

132 lines
4.0 KiB
Terraform

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]
}