From 3198f8d5d14df7f48e2889dbdac1fbfa6c9ce95c Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Thu, 19 Nov 2026 22:19:47 +1100 Subject: [PATCH 1/4] 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)])}." -- 2.47.3 From 361f34d820659d9b8ec39733fa62137e4575234d Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Thu, 19 Nov 2026 22:24:19 +1100 Subject: [PATCH 2/4] keep fork default branch and CODEOWNERS, reject private forks --- modules/github_org/modules/repository/main.tf | 19 +++++------ .../modules/repository/variables.tf | 2 +- .../github_org/tests/github_org.tftest.hcl | 32 ++++++++++++++++++- modules/github_org/variables.tf | 7 +++- 4 files changed, 48 insertions(+), 12 deletions(-) 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)])}." -- 2.47.3 From bab6233613c40f206caab59bc05fccb52bec7acf Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Thu, 19 Nov 2026 22:28:01 +1100 Subject: [PATCH 3/4] reject private on forks and document fork_from public-upstream constraint --- README.md | 3 +++ modules/github_org/tests/github_org.tftest.hcl | 13 ++++++++++++- modules/github_org/variables.tf | 8 ++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 401d911..cb0850b 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,11 @@ merge_type: squash # squash | merge | rebase delete_branch_on_merge: true rulesets: [] # opt-in rulesets from ruleset/ exclude_rulesets: [] # opt out of non-mandatory all_repos rulesets +fork_from: null # owner/repo: fork a public upstream (see below) ``` +`fork_from: owner/repo` creates the repository as a fork of a public upstream. Visibility follows the source, so `private` must be omitted. `default_branch` must match the upstream default branch. Adding `fork_from` to an existing repository recreates it. + `ruleset/.yaml`: ```yaml diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 1b72dc2..ef329fc 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -119,7 +119,6 @@ run "fork_from_source" { 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" } } } @@ -176,6 +175,18 @@ run "private_fork_rejected" { expect_failures = [var.repository] } +run "public_fork_rejected" { + command = plan + + variables { + repository = { + bad = { name = "bad", fork_from = "storytold/autocraft", private = false } + } + } + + expect_failures = [var.repository] +} + run "invalid_fork_from_rejected" { command = plan diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index e6f6cca..95297bd 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -96,7 +96,7 @@ variable "ruleset" { } variable "repository" { - description = "Map of repositories to create, keyed by name" + description = "Map of repositories to create, keyed by name; fork_from (owner/repo) forks a public upstream and requires private to be omitted" type = map(object({ name = string description = optional(string, "") @@ -114,12 +114,12 @@ variable "repository" { 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, "-")))])}." + error_message = "fork_from must be \"/\" of a public upstream: ${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])}." + condition = alltrue([for repo in var.repository : repo.fork_from == null || repo.private == null]) + error_message = "private cannot be set on a fork, its visibility follows the source and only public upstreams can be forked: ${join(", ", [for name, repo in var.repository : name if repo.fork_from != null && repo.private != null])}." } validation { -- 2.47.3 From 1889c95a46c80749a23384ac449552a33964edbd Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Thu, 19 Nov 2026 22:30:38 +1100 Subject: [PATCH 4/4] document fork_from as create-only and private as unset by default --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb0850b..7d48bbf 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ members: [] ```yaml description: "" -private: true +private: null # null = private; must be omitted with fork_from default_branch: main maintainers: [] # users or @team: maintain access, seed initial .github/CODEOWNERS codeowners: [] # push access @@ -45,7 +45,7 @@ exclude_rulesets: [] # opt out of non-mandatory all_repos rulesets fork_from: null # owner/repo: fork a public upstream (see below) ``` -`fork_from: owner/repo` creates the repository as a fork of a public upstream. Visibility follows the source, so `private` must be omitted. `default_branch` must match the upstream default branch. Adding `fork_from` to an existing repository recreates it. +`fork_from: owner/repo` creates the repository as a fork of a public upstream. Visibility follows the source, so `private` must be omitted. `default_branch` must match the upstream default branch. `fork_from` only works when creating a new repository: the old one is archived on destroy and keeps the name, so to convert an existing repository delete it on GitHub (or `terragrunt state rm` its repository resources) first. `ruleset/.yaml`: -- 2.47.3