diff --git a/README.md b/README.md index f4652f7..fb883f6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Manage Gitea resources, teams, repos, and Woodpecker CI via Terraform. `config/github.com/IUCLabs/` feeds `modules/github_org` via `environments/github/iuclabs`, authenticated as a GitHub App (`kv/service/github/iuclabs/terraform`). Org membership invites are manual. +`config.yaml` (optional) manages org settings: `billing_email`, `default_repository_permission` (read), `members_can_create_repositories` (false), `public_runners` (false; lets public repos use the Default runner group). Every org also gets a read-only, non-approving `GITHUB_TOKEN`. + `team/.yaml`: ```yaml diff --git a/config/config.hcl b/config/config.hcl index 335bbd3..1b32031 100644 --- a/config/config.hcl +++ b/config/config.hcl @@ -79,11 +79,13 @@ locals { } } - github = { + github = merge({ for kind in ["team", "ruleset", "repository"] : kind => { for file_path, content in local.github_configs : "${content.organisation}/${content.name}" => content if content.kind == kind } - } + }, { + settings = { for file_path, content in local.github_configs : content.organisation => content if content.kind == "config.yaml" } + }) } diff --git a/config/github.com/IUCLabs/config.yaml b/config/github.com/IUCLabs/config.yaml new file mode 100644 index 0000000..7dc4b53 --- /dev/null +++ b/config/github.com/IUCLabs/config.yaml @@ -0,0 +1,4 @@ +billing_email: neotheo@gmail.com +default_repository_permission: read +members_can_create_repositories: false +public_runners: true diff --git a/environments/github/iuclabs/terragrunt.hcl b/environments/github/iuclabs/terragrunt.hcl index dd8f031..5255a04 100644 --- a/environments/github/iuclabs/terragrunt.hcl +++ b/environments/github/iuclabs/terragrunt.hcl @@ -28,9 +28,26 @@ provider "github" { EOF } +generate "imports" { + path = "imports.tf" + if_exists = "overwrite" + contents = < v if v.organisation == local.owner } ruleset = { for k, v in local.github.ruleset : v.name => v if v.organisation == local.owner } repository = { for k, v in local.github.repository : v.name => v if v.organisation == local.owner } + settings = lookup(local.github.settings, local.owner, null) } diff --git a/modules/github_org/main.tf b/modules/github_org/main.tf index 686a773..4e3213e 100644 --- a/modules/github_org/main.tf +++ b/modules/github_org/main.tf @@ -208,3 +208,37 @@ resource "github_repository_ruleset" "this" { depends_on = [github_repository_file.codeowners] } + +resource "github_actions_organization_workflow_permissions" "this" { + organization_slug = var.owner + default_workflow_permissions = "read" + can_approve_pull_request_reviews = false +} + +resource "github_organization_settings" "this" { + count = var.settings == null ? 0 : 1 + + billing_email = var.settings.billing_email + default_repository_permission = var.settings.default_repository_permission + members_can_create_repositories = var.settings.members_can_create_repositories + # the public/private flags default to true and override the umbrella flag + members_can_create_public_repositories = var.settings.members_can_create_repositories + members_can_create_private_repositories = var.settings.members_can_create_repositories + + lifecycle { + prevent_destroy = true + } +} + +# Free plan only has the Default runner group; it is imported and updated, never created or deleted. +resource "github_actions_runner_group" "default" { + count = var.settings == null ? 0 : 1 + + name = "Default" + visibility = "all" + allows_public_repositories = var.settings.public_runners + + lifecycle { + prevent_destroy = true + } +} diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 0d7d4fc..17c9541 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -293,3 +293,63 @@ run "empty_status_check_contexts_rejected" { expect_failures = [var.ruleset] } + +run "org_settings_unmanaged_without_config" { + command = plan + + assert { + condition = length(github_organization_settings.this) == 0 && length(github_actions_runner_group.default) == 0 + error_message = "org settings and the default runner group must stay unmanaged without config.yaml" + } + + assert { + condition = ( + github_actions_organization_workflow_permissions.this.default_workflow_permissions == "read" && + !github_actions_organization_workflow_permissions.this.can_approve_pull_request_reviews + ) + error_message = "GITHUB_TOKEN must default to read-only and never approve PRs" + } +} + +run "org_settings_from_config" { + command = plan + + variables { + settings = { billing_email = "ops@example.com", public_runners = true } + } + + assert { + condition = ( + !github_organization_settings.this[0].members_can_create_repositories && + !github_organization_settings.this[0].members_can_create_public_repositories && + !github_organization_settings.this[0].members_can_create_private_repositories && + github_organization_settings.this[0].default_repository_permission == "read" + ) + error_message = "repo creation must be blocked for every visibility, default permission read" + } + + assert { + condition = ( + github_actions_runner_group.default[0].name == "Default" && + github_actions_runner_group.default[0].allows_public_repositories + ) + error_message = "public_runners must open the Default runner group to public repositories" + } +} + +run "org_settings_allow_repo_creation" { + command = plan + + variables { + settings = { billing_email = "ops@example.com", members_can_create_repositories = true } + } + + assert { + condition = ( + github_organization_settings.this[0].members_can_create_public_repositories && + github_organization_settings.this[0].members_can_create_private_repositories && + !github_actions_runner_group.default[0].allows_public_repositories + ) + error_message = "per-visibility creation flags must follow members_can_create_repositories; public_runners defaults off" + } +} diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index 4781959..725ca84 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -109,3 +109,14 @@ variable "repository" { error_message = "Unknown rulesets referenced: ${join(", ", distinct(flatten([for name, repo in var.repository : [for rs in repo.rulesets : "${name}/${rs}" if !contains(keys(var.ruleset), rs)]])))}." } } + +variable "settings" { + description = "Organisation settings from config.yaml; null leaves them unmanaged" + type = object({ + billing_email = string + default_repository_permission = optional(string, "read") + members_can_create_repositories = optional(bool, false) + public_runners = optional(bool, false) + }) + default = null +}