From 3198f8d5d14df7f48e2889dbdac1fbfa6c9ce95c Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Thu, 19 Nov 2026 22:19:47 +1100 Subject: [PATCH] add fork_from to github repositories --- modules/github_org/modules/repository/main.tf | 13 +++-- .../modules/repository/variables.tf | 1 + .../github_org/tests/github_org.tftest.hcl | 47 +++++++++++++++++++ modules/github_org/variables.tf | 6 +++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/modules/github_org/modules/repository/main.tf b/modules/github_org/modules/repository/main.tf index 083f115..c04a066 100644 --- a/modules/github_org/modules/repository/main.tf +++ b/modules/github_org/modules/repository/main.tf @@ -3,12 +3,14 @@ locals { { for e in var.config.codeowners : e => "push" }, { for e in var.config.maintainers : e => "maintain" }, ) + fork_source = var.config.fork_from == null ? null : split("/", var.config.fork_from) } resource "github_repository" "this" { - name = var.config.name - description = var.config.description - visibility = var.config.private ? "private" : "public" + 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 has_issues = true has_wiki = false has_projects = false @@ -16,8 +18,11 @@ resource "github_repository" "this" { 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 + auto_init = local.fork_source == null archive_on_destroy = true + fork = local.fork_source == null ? null : "true" + source_owner = try(local.fork_source[0], null) + source_repo = try(local.fork_source[1], null) } resource "github_branch_default" "this" { diff --git a/modules/github_org/modules/repository/variables.tf b/modules/github_org/modules/repository/variables.tf index 61c6b51..e1aa84e 100644 --- a/modules/github_org/modules/repository/variables.tf +++ b/modules/github_org/modules/repository/variables.tf @@ -12,6 +12,7 @@ variable "config" { codeowners = list(string) merge_type = string delete_branch_on_merge = bool + fork_from = optional(string) }) } diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 01ff6cf..666a16d 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -111,6 +111,53 @@ run "merge_type_rebase" { } } +run "fork_from_source" { + command = plan + + variables { + repository = { + autocraft = { name = "autocraft", fork_from = "storytold/autocraft", maintainers = ["@maintainers"] } + plain = { name = "plain" } + } + } + + assert { + condition = ( + module.repository["autocraft"].repository.fork == "true" && + module.repository["autocraft"].repository.source_owner == "storytold" && + module.repository["autocraft"].repository.source_repo == "autocraft" && + !module.repository["autocraft"].repository.auto_init + ) + error_message = "fork_from must fork the source repository without auto_init" + } + + 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 + error_message = "forks get the org rulesets and CODEOWNERS like any other repository" + } + + assert { + condition = ( + module.repository["plain"].repository.auto_init && + module.repository["plain"].repository.visibility == "private" && + module.repository["autocraft"].repository.allow_squash_merge + ) + error_message = "non-forks keep auto_init and configured visibility; forks keep repo settings" + } +} + +run "invalid_fork_from_rejected" { + command = plan + + variables { + repository = { + bad = { name = "bad", fork_from = "storytold" } + } + } + + expect_failures = [var.repository] +} + run "invalid_merge_type_rejected" { command = plan diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index aa2cdcd..b8b56c3 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -108,9 +108,15 @@ variable "repository" { delete_branch_on_merge = optional(bool, true) rulesets = optional(list(string), []) exclude_rulesets = optional(list(string), []) + fork_from = optional(string) })) default = {} + validation { + condition = alltrue([for repo in var.repository : repo.fork_from == null || can(regex("^[^/]+/[^/]+$", repo.fork_from))]) + 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 : 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)])}."