From 732940c70cddec68fb6acf8fa0ccdc3d7f83d7b9 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sat, 14 Nov 2026 18:21:57 +1100 Subject: [PATCH] Stop perpetual drift on mirrors and whitelist teams --- Makefile | 5 +++ .../repository/container-agent-base.yaml | 1 - .../unkin/repository/container-agent-dev.yaml | 1 - .../repository/container-agent-infra.yaml | 1 - .../unkin/repository/jellyfin-ha.yaml | 2 - .../repository/plugin-docker-buildx.yaml | 2 - .../gitea_instance/modules/repository/main.tf | 39 +++++++++--------- .../repository/tests/repository.tftest.hcl | 40 +++++++++++++++++++ 8 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 modules/gitea_instance/modules/repository/tests/repository.tftest.hcl diff --git a/Makefile b/Makefile index 8772e8b..7b39958 100644 --- a/Makefile +++ b/Makefile @@ -73,5 +73,10 @@ pre-commit: test: @tofu -chdir=modules/github_org init -backend=false -input=false >/dev/null && \ tofu -chdir=modules/github_org test + @tofu -chdir=modules/gitea_instance/modules/repository init -backend=false -input=false >/dev/null && \ + tofu -chdir=modules/gitea_instance/modules/repository test @terragrunt render --working-dir environments/au/syd1 --json --out /dev/stdout 2>/dev/null | \ python3 -c 'import json,sys; i=json.loads(sys.stdin.read().splitlines()[-1])["inputs"]; bad=[k for m in i.values() for k in m if k.startswith("github.com/")]; sys.exit(f"github.com config in gitea inputs: {bad}" if bad else 0)' + @# Gitea silently drops whitelist teams without access to the repo, so they would drift on every apply. + @terragrunt render --working-dir environments/au/syd1 --json --out /dev/stdout 2>/dev/null | \ + python3 -c 'import json,sys; i=json.loads(sys.stdin.read().splitlines()[-1])["inputs"]; t=i["team"]; ok=lambda b,n: (lambda m: m.get("include_all_repositories") or b["repository"] in (m.get("repositories") or []))(t.get(b["gitea_url"]+"/"+b["organisation"]+"/"+n, {})); bad=[k+": "+n for k,b in i["branch_protection"].items() for f in ("push","merge","approval") for n in b.get(f+"_whitelist_teams") or [] if not ok(b,n)]; sys.exit(f"whitelist teams without repo access: {bad}" if bad else 0)' diff --git a/config/git.unkin.net/unkin/repository/container-agent-base.yaml b/config/git.unkin.net/unkin/repository/container-agent-base.yaml index 682bca9..273c8bb 100644 --- a/config/git.unkin.net/unkin/repository/container-agent-base.yaml +++ b/config/git.unkin.net/unkin/repository/container-agent-base.yaml @@ -11,5 +11,4 @@ branch_protection: status_check_contexts: - "ci/woodpecker/pr/build" approval_whitelist_teams: - - "docker" - "forgebot" diff --git a/config/git.unkin.net/unkin/repository/container-agent-dev.yaml b/config/git.unkin.net/unkin/repository/container-agent-dev.yaml index 2ca6ab4..e054d5f 100644 --- a/config/git.unkin.net/unkin/repository/container-agent-dev.yaml +++ b/config/git.unkin.net/unkin/repository/container-agent-dev.yaml @@ -11,5 +11,4 @@ branch_protection: status_check_contexts: - "ci/woodpecker/pr/build" approval_whitelist_teams: - - "docker" - "forgebot" diff --git a/config/git.unkin.net/unkin/repository/container-agent-infra.yaml b/config/git.unkin.net/unkin/repository/container-agent-infra.yaml index 4445258..ac5c94f 100644 --- a/config/git.unkin.net/unkin/repository/container-agent-infra.yaml +++ b/config/git.unkin.net/unkin/repository/container-agent-infra.yaml @@ -11,5 +11,4 @@ branch_protection: status_check_contexts: - "ci/woodpecker/pr/build" approval_whitelist_teams: - - "docker" - "forgebot" diff --git a/config/git.unkin.net/unkin/repository/jellyfin-ha.yaml b/config/git.unkin.net/unkin/repository/jellyfin-ha.yaml index 34c098e..183e99b 100644 --- a/config/git.unkin.net/unkin/repository/jellyfin-ha.yaml +++ b/config/git.unkin.net/unkin/repository/jellyfin-ha.yaml @@ -10,5 +10,3 @@ branch_protection: enable_push: false status_check_contexts: - "ci/woodpecker/pr/build" - approval_whitelist_teams: - - "docker" diff --git a/config/git.unkin.net/unkin/repository/plugin-docker-buildx.yaml b/config/git.unkin.net/unkin/repository/plugin-docker-buildx.yaml index e936913..28c2df7 100644 --- a/config/git.unkin.net/unkin/repository/plugin-docker-buildx.yaml +++ b/config/git.unkin.net/unkin/repository/plugin-docker-buildx.yaml @@ -10,5 +10,3 @@ branch_protection: enable_push: false status_check_contexts: - "ci/woodpecker/pr/build" - approval_whitelist_teams: - - "docker" diff --git a/modules/gitea_instance/modules/repository/main.tf b/modules/gitea_instance/modules/repository/main.tf index 80c8e03..280a3d6 100644 --- a/modules/gitea_instance/modules/repository/main.tf +++ b/modules/gitea_instance/modules/repository/main.tf @@ -1,28 +1,31 @@ locals { fork_source = var.fork_from == null ? null : split("/", var.fork_from) + # Gitea stores merge settings in the pull-request unit; without it they read back false. + no_prs = var.has_pull_requests == false } resource "gitea_repository" "this" { count = var.fork_from == null ? 1 : 0 - username = var.organisation - name = var.name - description = var.description - private = var.private - default_branch = var.default_branch - has_issues = var.has_issues - has_wiki = var.has_wiki - has_pull_requests = var.has_pull_requests - has_projects = var.has_projects - allow_merge_commits = var.allow_merge_commits - allow_rebase = var.allow_rebase - allow_rebase_explicit = var.allow_rebase_explicit - allow_squash_merge = var.allow_squash_merge - archived = var.archived - repo_template = var.repo_template - website = var.website - autodetect_manual_merge = var.autodetect_manual_merge - archive_on_destroy = true + username = var.organisation + name = var.name + description = var.description + private = var.private + default_branch = var.default_branch + has_issues = var.has_issues + has_wiki = var.has_wiki + has_pull_requests = var.has_pull_requests + has_projects = var.has_projects + allow_merge_commits = local.no_prs ? false : var.allow_merge_commits + allow_rebase = local.no_prs ? false : var.allow_rebase + allow_rebase_explicit = local.no_prs ? false : var.allow_rebase_explicit + allow_squash_merge = local.no_prs ? false : var.allow_squash_merge + ignore_whitespace_conflicts = local.no_prs ? false : null + archived = var.archived + repo_template = var.repo_template + website = var.website + autodetect_manual_merge = var.autodetect_manual_merge + archive_on_destroy = true mirror = var.mirror migration_clone_address = var.migration_clone_address diff --git a/modules/gitea_instance/modules/repository/tests/repository.tftest.hcl b/modules/gitea_instance/modules/repository/tests/repository.tftest.hcl new file mode 100644 index 0000000..6e9c681 --- /dev/null +++ b/modules/gitea_instance/modules/repository/tests/repository.tftest.hcl @@ -0,0 +1,40 @@ +mock_provider "gitea" {} + +variables { + name = "example" + organisation = "unkin" +} + +run "no_pull_requests_disables_merge_settings" { + command = plan + + variables { + has_pull_requests = false + allow_merge_commits = true + allow_squash_merge = true + } + + assert { + condition = !anytrue([ + gitea_repository.this[0].allow_merge_commits, + gitea_repository.this[0].allow_rebase, + gitea_repository.this[0].allow_rebase_explicit, + gitea_repository.this[0].allow_squash_merge, + gitea_repository.this[0].ignore_whitespace_conflicts, + ]) + error_message = "merge settings must be false when the pull-request unit is disabled" + } +} + +run "pull_requests_keep_merge_settings" { + command = plan + + variables { + allow_merge_commits = true + } + + assert { + condition = gitea_repository.this[0].allow_merge_commits && gitea_repository.this[0].allow_squash_merge + error_message = "merge settings must pass through when pull requests are enabled" + } +}