This repository has been archived on 2026-11-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
unkin-agent bab6233613
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful
reject private on forks and document fork_from public-upstream constraint
2026-11-19 22:28:01 +11:00

184 lines
10 KiB
Terraform

variable "owner" {
description = "GitHub organisation that owns every resource"
type = string
}
variable "team" {
description = "Map of teams to create, keyed by name"
type = map(object({
name = string
description = optional(string, "")
privacy = optional(string, "closed")
maintainers = optional(list(string), [])
members = optional(list(string), [])
}))
default = {}
}
variable "ruleset" {
description = "Map of repository ruleset definitions, keyed by name"
type = map(object({
name = string
all_repos = optional(bool, false)
mandatory = optional(bool, false)
target = optional(string, "branch")
enforcement = optional(string, "active")
bypass_actors = optional(list(object({
actor_type = string
actor_id = optional(number)
team = optional(string)
bypass_mode = optional(string, "always")
})), [])
conditions = optional(object({
ref_name = object({
include = list(string)
exclude = optional(list(string), [])
})
}))
rules = object({
creation = optional(bool, false)
deletion = optional(bool, false)
non_fast_forward = optional(bool, false)
pull_request = optional(object({
required_approving_review_count = optional(number, 0)
dismiss_stale_reviews_on_push = optional(bool, false)
require_code_owner_review = optional(bool, false)
require_last_push_approval = optional(bool, false)
required_review_thread_resolution = optional(bool, false)
}))
required_status_checks = optional(object({
contexts = list(string)
strict = optional(bool, false)
}))
})
}))
default = {}
validation {
condition = alltrue([for rs in var.ruleset : rs.target == "branch" || (rs.rules.pull_request == null && rs.rules.required_status_checks == null)])
error_message = "pull_request and required_status_checks are branch-only rules, not allowed on tag rulesets: ${join(", ", [for k, rs in var.ruleset : k if rs.target != "branch" && (rs.rules.pull_request != null || rs.rules.required_status_checks != null)])}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : contains(["RepositoryRole", "Team", "Integration", "OrganizationAdmin", "DeployKey", "EnterpriseOwner", "User"], a.actor_type)]]))
error_message = "bypass_actors actor_type must be RepositoryRole, Team, Integration, OrganizationAdmin, DeployKey, EnterpriseOwner or User: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if !contains(["RepositoryRole", "Team", "Integration", "OrganizationAdmin", "DeployKey", "EnterpriseOwner", "User"], a.actor_type)]]))}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : contains(["always", "pull_request", "exempt"], a.bypass_mode)]]))
error_message = "bypass_actors bypass_mode must be always, pull_request or exempt: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}/${a.bypass_mode}" if !contains(["always", "pull_request", "exempt"], a.bypass_mode)]]))}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : a.actor_id != null if contains(["RepositoryRole", "Integration", "User"], a.actor_type)]]))
error_message = "bypass_actors actor_id is required for RepositoryRole, Integration and User: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if contains(["RepositoryRole", "Integration", "User"], a.actor_type) && a.actor_id == null]]))}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : a.actor_id == null if !contains(["RepositoryRole", "Integration", "User"], a.actor_type)]]))
error_message = "bypass_actors actor_id must not be set for Team (use team), OrganizationAdmin, EnterpriseOwner or DeployKey: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if !contains(["RepositoryRole", "Integration", "User"], a.actor_type) && a.actor_id != null]]))}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : (a.actor_type == "Team") == (a.team != null)]]))
error_message = "bypass_actors team must be set exactly when actor_type is Team: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if(a.actor_type == "Team") != (a.team != null)]]))}."
}
validation {
condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : contains(keys(var.team), a.team) if a.team != null]]))
error_message = "Unknown bypass teams referenced: ${join(", ", distinct(flatten([for name, rs in var.ruleset : [for a in rs.bypass_actors : "${name}/${a.team}" if a.team != null && !contains(keys(var.team), coalesce(a.team, "-"))]])))}."
}
validation {
condition = alltrue([for rs in var.ruleset : rs.rules.required_status_checks == null || length(rs.rules.required_status_checks.contexts) > 0])
error_message = "required_status_checks.contexts must not be empty: ${join(", ", [for k, rs in var.ruleset : k if rs.rules.required_status_checks != null && length(rs.rules.required_status_checks.contexts) == 0])}."
}
}
variable "repository" {
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, "")
private = optional(bool)
default_branch = optional(string, "main")
maintainers = optional(list(string), [])
codeowners = optional(list(string), [])
merge_type = optional(string, "squash")
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 \"<owner>/<repo>\" 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 == 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 {
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)])}."
}
validation {
condition = alltrue(flatten([for repo in var.repository : [for rs in concat(repo.rulesets, repo.exclude_rulesets) : contains(keys(var.ruleset), rs)]]))
error_message = "Unknown rulesets referenced: ${join(", ", distinct(flatten([for name, repo in var.repository : [for rs in concat(repo.rulesets, repo.exclude_rulesets) : "${name}/${rs}" if !contains(keys(var.ruleset), rs)]])))}."
}
validation {
condition = alltrue(flatten([for repo in var.repository : [for rs in repo.exclude_rulesets : !try(var.ruleset[rs].mandatory, false)]]))
error_message = "Mandatory rulesets cannot be excluded: ${join(", ", flatten([for name, repo in var.repository : [for rs in repo.exclude_rulesets : "${name}/${rs}" if try(var.ruleset[rs].mandatory, false)]]))}."
}
validation {
condition = alltrue([for repo in var.repository : length(setintersection(repo.rulesets, repo.exclude_rulesets)) == 0])
error_message = "Rulesets both applied and excluded: ${join(", ", flatten([for name, repo in var.repository : [for rs in setintersection(repo.rulesets, repo.exclude_rulesets) : "${name}/${rs}"]]))}."
}
validation {
condition = alltrue(flatten([for repo in var.repository : [for e in concat(repo.maintainers, repo.codeowners) : contains(keys(var.team), trimprefix(e, "@")) if startswith(e, "@")]]))
error_message = "Unknown teams referenced: ${join(", ", distinct(flatten([for name, repo in var.repository : [for e in concat(repo.maintainers, repo.codeowners) : "${name}/${e}" if startswith(e, "@") && !contains(keys(var.team), trimprefix(e, "@"))]])))}."
}
}
variable "settings" {
description = "Organisation settings from config.yaml; null leaves them unmanaged"
type = object({
name = optional(string)
description = optional(string)
blog = optional(string)
location = optional(string)
email = optional(string)
twitter_username = optional(string)
company = optional(string)
default_repository_permission = optional(string, "read")
members_can_create_repositories = optional(bool, false)
public_runners = optional(bool, false)
})
default = null
validation {
condition = var.settings == null || contains(["read", "none"], try(var.settings.default_repository_permission, ""))
error_message = "default_repository_permission must be read or none."
}
}
variable "billing_email" {
description = "Organisation billing email, from TF_VAR_billing_email; required when settings is set"
type = string
default = null
sensitive = true
validation {
condition = var.settings == null || var.billing_email != null
error_message = "billing_email must be set when settings is set."
}
}