diff --git a/modules/github_org/modules/repository/main.tf b/modules/github_org/modules/repository/main.tf index c04a066..7af6b61 100644 --- a/modules/github_org/modules/repository/main.tf +++ b/modules/github_org/modules/repository/main.tf @@ -10,7 +10,7 @@ resource "github_repository" "this" { name = var.config.name description = var.config.description # a fork's visibility follows its source and cannot be changed - visibility = local.fork_source == null ? (var.config.private ? "private" : "public") : null + visibility = local.fork_source == null ? (coalesce(var.config.private, true) ? "private" : "public") : null has_issues = true has_wiki = false has_projects = false @@ -28,8 +28,8 @@ resource "github_repository" "this" { 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 + # auto_init only creates main so rename it; a fork must select an existing branch + rename = local.fork_source == null wait_for_rename = true } @@ -58,12 +58,13 @@ resource "github_repository_collaborators" "this" { 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 + 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" + # a fork may inherit the upstream CODEOWNERS; ours must win for code-owner rulesets + overwrite_on_create = local.fork_source != null lifecycle { ignore_changes = [content] diff --git a/modules/github_org/modules/repository/variables.tf b/modules/github_org/modules/repository/variables.tf index e1aa84e..be4e2c8 100644 --- a/modules/github_org/modules/repository/variables.tf +++ b/modules/github_org/modules/repository/variables.tf @@ -6,7 +6,7 @@ variable "config" { type = object({ name = string description = string - private = bool + private = optional(bool) default_branch = string maintainers = list(string) codeowners = list(string) diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 666a16d..1b72dc2 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -118,6 +118,8 @@ run "fork_from_source" { repository = { autocraft = { name = "autocraft", fork_from = "storytold/autocraft", maintainers = ["@maintainers"] } plain = { name = "plain" } + secret = { name = "secret", maintainers = ["@maintainers"] } + public = { name = "public", private = false, fork_from = "storytold/public" } } } @@ -132,7 +134,7 @@ run "fork_from_source" { } assert { - condition = keys(module.ruleset) == ["autocraft/branch-protection", "autocraft/ref-names-tag", "autocraft/tag-protection", "plain/branch-protection", "plain/ref-names-tag", "plain/tag-protection"] && module.repository["autocraft"].codeowners != null + condition = length([for k in keys(module.ruleset) : k if startswith(k, "autocraft/")]) == 3 && module.repository["autocraft"].codeowners != null error_message = "forks get the org rulesets and CODEOWNERS like any other repository" } @@ -144,6 +146,34 @@ run "fork_from_source" { ) error_message = "non-forks keep auto_init and configured visibility; forks keep repo settings" } + + assert { + condition = ( + !module.repository["autocraft"].branch_default.rename && + module.repository["autocraft"].codeowners.overwrite_on_create + ) + error_message = "forks must select the existing default branch and overwrite an inherited CODEOWNERS" + } + + assert { + condition = ( + module.repository["plain"].branch_default.rename && + !module.repository["secret"].codeowners.overwrite_on_create + ) + error_message = "non-forks must rename the auto_init branch and never overwrite CODEOWNERS" + } +} + +run "private_fork_rejected" { + command = plan + + variables { + repository = { + bad = { name = "bad", fork_from = "storytold/autocraft", private = true } + } + } + + expect_failures = [var.repository] } run "invalid_fork_from_rejected" { diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index b8b56c3..e6f6cca 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -100,7 +100,7 @@ variable "repository" { type = map(object({ name = string description = optional(string, "") - private = optional(bool, true) + private = optional(bool) default_branch = optional(string, "main") maintainers = optional(list(string), []) codeowners = optional(list(string), []) @@ -117,6 +117,11 @@ variable "repository" { error_message = "fork_from must be \"/\": ${join(", ", [for name, repo in var.repository : name if repo.fork_from != null && !can(regex("^[^/]+/[^/]+$", coalesce(repo.fork_from, "-")))])}." } + validation { + condition = alltrue([for repo in var.repository : !(repo.fork_from != null && repo.private == true)]) + error_message = "private cannot be set on a fork, its visibility follows the source: ${join(", ", [for name, repo in var.repository : name if repo.fork_from != null && repo.private == true])}." + } + 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)])}."