From 691eca53ea206b4f04142b91ed35073d94a260a1 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Mon, 9 Nov 2026 23:58:40 +1100 Subject: [PATCH] scope GitHub App secret to the github unit, use team slugs --- Makefile | 5 +--- README.md | 2 +- environments/github/iuclabs/terragrunt.hcl | 14 +++++++++-- modules/github_org/main.tf | 8 +++--- .../github_org/tests/github_org.tftest.hcl | 25 ++++++++++++++++--- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 813d2b4..1aa2908 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,7 @@ define vault_env fi && \ export CONSUL_HTTP_TOKEN=$$(vault read -field=token consul_root/au/syd1/creds/terraform-git) && \ export GITEA_TOKEN=$$(vault kv get -field=token kv/service/gitea/gitadmin/tokens/terraform-git) && \ - export WOODPECKER_TOKEN=$$(vault kv get -field=token kv/service/woodpecker/tokens/gitadmin) && \ - export GITHUB_APP_ID=$$(vault kv get -field=app_id kv/service/github/iuclabs/terraform) && \ - export GITHUB_APP_INSTALLATION_ID=$$(vault kv get -field=installation_id kv/service/github/iuclabs/terraform) && \ - export GITHUB_APP_PEM_FILE="$$(vault kv get -field=pem kv/service/github/iuclabs/terraform)" + export WOODPECKER_TOKEN=$$(vault kv get -field=token kv/service/woodpecker/tokens/gitadmin) endef init: diff --git a/README.md b/README.md index b41fbd2..222d223 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,4 @@ maintainers: ["@maintainers"] codeowners: [] ``` -`maintainers` (users or `@team`) get `maintain` and seed the initial `.github/CODEOWNERS`, which is then left to humans. `codeowners` get `push` so they can be added to CODEOWNERS later. Private repositories cannot use rulesets on the free plan; plan fails if they do. +`maintainers` (users or `@team`) get `maintain` and seed the initial `.github/CODEOWNERS` on `auto_init` repositories, which is then left to humans. `codeowners` get `push` so they can be added to CODEOWNERS later. Private repositories cannot use rulesets on the free plan; plan fails if they do. diff --git a/environments/github/iuclabs/terragrunt.hcl b/environments/github/iuclabs/terragrunt.hcl index dd8f031..38aed34 100644 --- a/environments/github/iuclabs/terragrunt.hcl +++ b/environments/github/iuclabs/terragrunt.hcl @@ -9,12 +9,22 @@ include "config" { } locals { - owner = "IUCLabs" - github = include.config.locals.github + owner = "IUCLabs" + github = include.config.locals.github + vault_path = "kv/service/github/iuclabs/terraform" } terraform { source = "../../../modules/github_org" + + extra_arguments "github_app" { + commands = ["plan", "apply", "destroy", "refresh", "import"] + env_vars = { + GITHUB_APP_ID = run_cmd("--terragrunt-quiet", "vault", "kv", "get", "-field=app_id", local.vault_path) + GITHUB_APP_INSTALLATION_ID = run_cmd("--terragrunt-quiet", "vault", "kv", "get", "-field=installation_id", local.vault_path) + GITHUB_APP_PEM_FILE = run_cmd("--terragrunt-quiet", "vault", "kv", "get", "-field=pem", local.vault_path) + } + } } generate "provider" { diff --git a/modules/github_org/main.tf b/modules/github_org/main.tf index 9e52362..3229fb0 100644 --- a/modules/github_org/main.tf +++ b/modules/github_org/main.tf @@ -75,21 +75,19 @@ resource "github_repository_collaborators" "this" { dynamic "team" { for_each = { for e, p in each.value : trimprefix(e, "@") => p if startswith(e, "@") } content { - team_id = team.key + team_id = github_team.this[team.key].slug 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 } + for_each = { for name, r in var.repository : name => r if length(r.maintainers) > 0 && r.auto_init } 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" + content = "* ${join(" ", [for e in each.value.maintainers : startswith(e, "@") ? "@${var.owner}/${github_team.this[trimprefix(e, "@")].slug}" : "@${e}"])}\n" commit_message = "add initial CODEOWNERS" overwrite_on_create = false diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index e756662..62bdcde 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -1,4 +1,8 @@ -mock_provider "github" {} +mock_provider "github" { + mock_resource "github_team" { + defaults = { slug = "maintainers-slug" } + } +} variables { owner = "IUCLabs" @@ -20,8 +24,8 @@ run "public_repo_with_ruleset" { } assert { - condition = github_repository_file.codeowners[".github"].content == "* @IUCLabs/maintainers @carol\n" - error_message = "CODEOWNERS must list maintainers, teams qualified by owner" + condition = github_repository_file.codeowners[".github"].content == "* @IUCLabs/maintainers-slug @carol\n" + error_message = "CODEOWNERS must list maintainers, teams by owner-qualified slug" } assert { @@ -35,6 +39,21 @@ run "public_repo_with_ruleset" { } } +run "no_codeowners_without_auto_init" { + command = plan + + variables { + repository = { + empty = { name = "empty", private = false, auto_init = false, maintainers = ["@maintainers"] } + } + } + + assert { + condition = length(github_repository_file.codeowners) == 0 + error_message = "CODEOWNERS needs an initialised default branch" + } +} + run "private_repo_with_ruleset_rejected" { command = plan