Define all GitHub rulesets as ruleset/ files
This commit is contained in:
@@ -15,14 +15,53 @@ module "repository" {
|
||||
|
||||
for_each = var.repository
|
||||
|
||||
owner = var.owner
|
||||
config = each.value
|
||||
rulesets = { for rs in each.value.rulesets : rs => var.ruleset[rs] }
|
||||
owner = var.owner
|
||||
config = each.value
|
||||
team_slugs = {
|
||||
for e in concat(each.value.maintainers, each.value.codeowners) : trimprefix(e, "@") => module.team[trimprefix(e, "@")].slug if startswith(e, "@")
|
||||
}
|
||||
}
|
||||
|
||||
locals {
|
||||
# rulesets are not enforced on private repositories on the GitHub free plan
|
||||
ruleset_pairs = merge([for repo_name, repo in var.repository : {
|
||||
for rs in setunion(repo.rulesets, repo.private ? [] : [for k, v in var.ruleset : k if v.all_repos && !contains(repo.exclude_rulesets, k)]) :
|
||||
"${repo_name}/${rs}" => { repository = repo_name, ruleset = rs }
|
||||
}]...)
|
||||
}
|
||||
|
||||
module "ruleset" {
|
||||
source = "./modules/ruleset"
|
||||
|
||||
for_each = local.ruleset_pairs
|
||||
|
||||
repository = module.repository[each.value.repository].repository.name
|
||||
ruleset = var.ruleset[each.value.ruleset]
|
||||
|
||||
# CODEOWNERS must exist before pull_request rules land
|
||||
depends_on = [module.repository]
|
||||
}
|
||||
|
||||
moved {
|
||||
from = module.repository[".github"].module.ruleset["branch-protection"].github_repository_ruleset.this
|
||||
to = module.ruleset[".github/branch-protection"].github_repository_ruleset.this
|
||||
}
|
||||
|
||||
moved {
|
||||
from = module.repository[".github"].module.ruleset["tag-protection"].github_repository_ruleset.this
|
||||
to = module.ruleset[".github/tag-protection"].github_repository_ruleset.this
|
||||
}
|
||||
|
||||
moved {
|
||||
from = module.repository[".github"].module.ruleset["ref-names-branch"].github_repository_ruleset.this
|
||||
to = module.ruleset[".github/ref-names-branch"].github_repository_ruleset.this
|
||||
}
|
||||
|
||||
moved {
|
||||
from = module.repository[".github"].module.ruleset["ref-names-tag"].github_repository_ruleset.this
|
||||
to = module.ruleset[".github/ref-names-tag"].github_repository_ruleset.this
|
||||
}
|
||||
|
||||
resource "github_actions_organization_workflow_permissions" "this" {
|
||||
count = var.settings == null ? 0 : 1
|
||||
|
||||
|
||||
@@ -3,75 +3,6 @@ locals {
|
||||
{ for e in var.config.codeowners : e => "push" },
|
||||
{ for e in var.config.maintainers : e => "maintain" },
|
||||
)
|
||||
|
||||
default_branch_rules = {
|
||||
target = "branch"
|
||||
enforcement = "active"
|
||||
conditions = { ref_name = { include = ["~DEFAULT_BRANCH"], exclude = [] } }
|
||||
}
|
||||
|
||||
generated_rulesets = { for k, v in {
|
||||
branch-protection = {
|
||||
enabled = coalesce(var.config.branch_protection, !var.config.private)
|
||||
ruleset = merge(local.default_branch_rules, {
|
||||
name = "branch-protection"
|
||||
rules = {
|
||||
deletion = true
|
||||
non_fast_forward = true
|
||||
pull_request = {
|
||||
required_approving_review_count = 0
|
||||
dismiss_stale_reviews_on_push = true
|
||||
require_code_owner_review = false
|
||||
require_last_push_approval = false
|
||||
required_review_thread_resolution = false
|
||||
}
|
||||
required_status_checks = null
|
||||
}
|
||||
})
|
||||
}
|
||||
required-checks = {
|
||||
enabled = length(var.config.required_checks) > 0
|
||||
ruleset = merge(local.default_branch_rules, {
|
||||
name = "required-checks"
|
||||
rules = {
|
||||
deletion = false
|
||||
non_fast_forward = false
|
||||
pull_request = null
|
||||
required_status_checks = { contexts = var.config.required_checks, strict = true }
|
||||
}
|
||||
})
|
||||
}
|
||||
tag-protection = {
|
||||
enabled = coalesce(var.config.tag_protection, !var.config.private)
|
||||
ruleset = {
|
||||
name = "tag-protection"
|
||||
target = "tag"
|
||||
enforcement = "active"
|
||||
conditions = { ref_name = { include = ["refs/tags/v*"], exclude = [] } }
|
||||
rules = {
|
||||
deletion = true
|
||||
non_fast_forward = true
|
||||
pull_request = null
|
||||
required_status_checks = null
|
||||
}
|
||||
}
|
||||
}
|
||||
} : k => v.ruleset if v.enabled }
|
||||
|
||||
# a "+" ref component becomes a wildcard in Vault policy paths templated from the GitHub OIDC ref claim
|
||||
ref_name_rulesets = var.config.private ? {} : { for t, prefix in { branch = "refs/heads", tag = "refs/tags" } : "ref-names-${t}" => {
|
||||
name = "ref-names-${t}"
|
||||
target = t
|
||||
enforcement = "active"
|
||||
conditions = { ref_name = { include = ["${prefix}/**/+", "${prefix}/**/+/**/*"], exclude = [] } }
|
||||
rules = {
|
||||
creation = true
|
||||
deletion = false
|
||||
non_fast_forward = false
|
||||
pull_request = null
|
||||
required_status_checks = null
|
||||
}
|
||||
} }
|
||||
}
|
||||
|
||||
resource "github_repository" "this" {
|
||||
@@ -133,14 +64,3 @@ resource "github_repository_file" "codeowners" {
|
||||
ignore_changes = [content]
|
||||
}
|
||||
}
|
||||
|
||||
module "ruleset" {
|
||||
source = "../ruleset"
|
||||
|
||||
for_each = merge(var.rulesets, local.generated_rulesets, local.ref_name_rulesets)
|
||||
|
||||
repository = github_repository.this.name
|
||||
ruleset = each.value
|
||||
|
||||
depends_on = [github_repository_file.codeowners]
|
||||
}
|
||||
|
||||
@@ -13,7 +13,3 @@ output "codeowners" {
|
||||
output "access" {
|
||||
value = local.access
|
||||
}
|
||||
|
||||
output "rulesets" {
|
||||
value = { for k, m in module.ruleset : k => m.ruleset }
|
||||
}
|
||||
|
||||
@@ -12,18 +12,9 @@ variable "config" {
|
||||
codeowners = list(string)
|
||||
merge_type = string
|
||||
delete_branch_on_merge = bool
|
||||
branch_protection = bool
|
||||
tag_protection = bool
|
||||
required_checks = list(string)
|
||||
})
|
||||
}
|
||||
|
||||
variable "rulesets" {
|
||||
description = "Custom ruleset definitions this repository references, keyed by name"
|
||||
type = any
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "team_slugs" {
|
||||
description = "Slugs of the teams this repository references, keyed by team name"
|
||||
type = map(string)
|
||||
|
||||
@@ -11,7 +11,23 @@ variables {
|
||||
maintainers = { name = "maintainers", maintainers = ["alice"], members = ["bob"] }
|
||||
}
|
||||
ruleset = {
|
||||
branch-protection = {
|
||||
name = "branch-protection", all_repos = true
|
||||
conditions = { ref_name = { include = ["~DEFAULT_BRANCH"] } }
|
||||
rules = { deletion = true, non_fast_forward = true, pull_request = { dismiss_stale_reviews_on_push = true } }
|
||||
}
|
||||
tag-protection = {
|
||||
name = "tag-protection", all_repos = true, target = "tag"
|
||||
conditions = { ref_name = { include = ["refs/tags/v*"] } }
|
||||
rules = { deletion = true, non_fast_forward = true }
|
||||
}
|
||||
ref-names-tag = {
|
||||
name = "ref-names-tag", all_repos = true, mandatory = true, target = "tag"
|
||||
conditions = { ref_name = { include = ["refs/tags/**/+", "refs/tags/**/+/**/*"] } }
|
||||
rules = { creation = true }
|
||||
}
|
||||
signed = { name = "signed", rules = { deletion = true, pull_request = {} } }
|
||||
checks = { name = "checks", rules = { required_status_checks = { contexts = ["ci/woodpecker"], strict = true } } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,30 +51,20 @@ run "public_repo_defaults" {
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = keys(module.repository[".github"].rulesets) == ["branch-protection", "ref-names-branch", "ref-names-tag", "tag-protection"] && length(module.team["maintainers"].membership) == 2
|
||||
error_message = "public repo must get only the generated branch-protection, ref-names and tag-protection rulesets"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = alltrue([for t, prefix in { branch = "refs/heads", tag = "refs/tags" } : (
|
||||
module.repository[".github"].rulesets["ref-names-${t}"].target == t &&
|
||||
module.repository[".github"].rulesets["ref-names-${t}"].enforcement == "active" &&
|
||||
module.repository[".github"].rulesets["ref-names-${t}"].conditions[0].ref_name[0].include == tolist(["${prefix}/**/+", "${prefix}/**/+/**/*"]) &&
|
||||
module.repository[".github"].rulesets["ref-names-${t}"].rules[0].creation &&
|
||||
!module.repository[".github"].rulesets["ref-names-${t}"].rules[0].deletion
|
||||
)])
|
||||
error_message = "ref-names rulesets must block creating branches and tags with a + path component"
|
||||
condition = keys(module.ruleset) == [".github/branch-protection", ".github/ref-names-tag", ".github/tag-protection"] && length(module.team["maintainers"].membership) == 2
|
||||
error_message = "public repo must get exactly the all_repos rulesets, keyed <repo>/<ruleset>"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
module.repository[".github"].rulesets["tag-protection"].target == "tag" &&
|
||||
module.repository[".github"].rulesets["tag-protection"].enforcement == "active" &&
|
||||
module.repository[".github"].rulesets["tag-protection"].conditions[0].ref_name[0].include == tolist(["refs/tags/v*"]) &&
|
||||
module.repository[".github"].rulesets["tag-protection"].rules[0].deletion &&
|
||||
module.repository[".github"].rulesets["tag-protection"].rules[0].non_fast_forward
|
||||
module.ruleset[".github/ref-names-tag"].ruleset.name == "ref-names-tag" &&
|
||||
module.ruleset[".github/ref-names-tag"].ruleset.repository == ".github" &&
|
||||
module.ruleset[".github/ref-names-tag"].ruleset.target == "tag" &&
|
||||
module.ruleset[".github/ref-names-tag"].ruleset.conditions[0].ref_name[0].include == tolist(["refs/tags/**/+", "refs/tags/**/+/**/*"]) &&
|
||||
module.ruleset[".github/ref-names-tag"].ruleset.rules[0].creation &&
|
||||
!module.ruleset[".github/ref-names-tag"].ruleset.rules[0].deletion
|
||||
)
|
||||
error_message = "tag-protection ruleset must make refs/tags/v* undeletable and immutable"
|
||||
error_message = "ruleset definition must be applied to the paired repository unchanged"
|
||||
}
|
||||
|
||||
assert {
|
||||
@@ -81,8 +87,8 @@ run "private_repo_defaults" {
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = length(module.repository["secret"].rulesets) == 0 && module.repository["secret"].codeowners != null
|
||||
error_message = "private repo defaults to no rulesets but still gets CODEOWNERS"
|
||||
condition = length(module.ruleset) == 0 && module.repository["secret"].codeowners != null
|
||||
error_message = "private repo silently skips all_repos rulesets but still gets CODEOWNERS"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,93 +123,27 @@ run "invalid_merge_type_rejected" {
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "generated_and_custom_rulesets" {
|
||||
run "exclusions_and_custom_rulesets" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
checked = { name = "checked", private = false, branch_protection = false, tag_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] }
|
||||
checked = { name = "checked", private = false, exclude_rulesets = ["branch-protection", "tag-protection"], rulesets = ["signed", "checks"] }
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = toset(keys(module.repository["checked"].rulesets)) == toset(["required-checks", "ref-names-branch", "ref-names-tag", "signed"])
|
||||
error_message = "required_checks with protections off yields only required-checks, ref-names plus custom rulesets"
|
||||
condition = keys(module.ruleset) == ["checked/checks", "checked/ref-names-tag", "checked/signed"]
|
||||
error_message = "exclude_rulesets drops non-mandatory all_repos rulesets; rulesets adds custom ones"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.repository["checked"].rulesets["required-checks"].rules[0].required_status_checks[0].strict_required_status_checks_policy
|
||||
error_message = "required-checks ruleset must be strict"
|
||||
condition = (
|
||||
module.ruleset["checked/checks"].ruleset.rules[0].required_status_checks[0].strict_required_status_checks_policy &&
|
||||
length(module.ruleset["checked/checks"].ruleset.rules[0].pull_request) == 0
|
||||
)
|
||||
error_message = "required status checks come from a custom ruleset"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = length(module.repository["checked"].rulesets["required-checks"].rules[0].pull_request) == 0
|
||||
error_message = "required-checks ruleset must not carry pull_request rules"
|
||||
}
|
||||
}
|
||||
|
||||
run "private_branch_protection_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
secret = { name = "secret", branch_protection = true }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "public_tag_protection_only" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
tags = { name = "tags", private = false, branch_protection = false }
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = keys(module.repository["tags"].rulesets) == ["ref-names-branch", "ref-names-tag", "tag-protection"]
|
||||
error_message = "tag_protection defaults on for public repos independently of branch_protection"
|
||||
}
|
||||
}
|
||||
|
||||
run "private_tag_protection_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
secret = { name = "secret", tag_protection = true }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "reserved_tag_protection_name_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
ruleset = {
|
||||
tag-protection = { name = "tag-protection", target = "tag", rules = { deletion = true } }
|
||||
}
|
||||
repository = {}
|
||||
}
|
||||
|
||||
expect_failures = [var.ruleset]
|
||||
}
|
||||
|
||||
run "private_required_checks_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
secret = { name = "secret", required_checks = ["ci/woodpecker"] }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "private_rulesets_rejected" {
|
||||
@@ -218,30 +158,40 @@ run "private_rulesets_rejected" {
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "reserved_ruleset_name_rejected" {
|
||||
run "mandatory_exclusion_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
ruleset = {
|
||||
branch-protection = { name = "branch-protection", rules = { deletion = true } }
|
||||
repository = {
|
||||
public = { name = "public", private = false, exclude_rulesets = ["ref-names-tag"] }
|
||||
}
|
||||
repository = {}
|
||||
}
|
||||
|
||||
expect_failures = [var.ruleset]
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "reserved_ref_names_rejected" {
|
||||
run "applied_and_excluded_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
ruleset = {
|
||||
ref-names-tag = { name = "ref-names-tag", target = "tag", rules = { deletion = true } }
|
||||
repository = {
|
||||
public = { name = "public", private = false, rulesets = ["signed"], exclude_rulesets = ["signed"] }
|
||||
}
|
||||
repository = {}
|
||||
}
|
||||
|
||||
expect_failures = [var.ruleset]
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "unknown_excluded_ruleset_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
public = { name = "public", private = false, exclude_rulesets = ["missing"] }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
|
||||
run "unknown_ruleset_rejected" {
|
||||
|
||||
@@ -16,9 +16,11 @@ variable "team" {
|
||||
}
|
||||
|
||||
variable "ruleset" {
|
||||
description = "Map of reusable repository ruleset definitions, keyed by name"
|
||||
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")
|
||||
conditions = optional(object({
|
||||
@@ -28,6 +30,7 @@ variable "ruleset" {
|
||||
})
|
||||
}))
|
||||
rules = object({
|
||||
creation = optional(bool, false)
|
||||
deletion = optional(bool, false)
|
||||
non_fast_forward = optional(bool, false)
|
||||
pull_request = optional(object({
|
||||
@@ -45,11 +48,6 @@ variable "ruleset" {
|
||||
}))
|
||||
default = {}
|
||||
|
||||
validation {
|
||||
condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection", "ref-names-branch", "ref-names-tag"])) == 0
|
||||
error_message = "Ruleset names branch-protection, required-checks, tag-protection, ref-names-branch and ref-names-tag are reserved for generated rulesets."
|
||||
}
|
||||
|
||||
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)])}."
|
||||
@@ -72,10 +70,8 @@ variable "repository" {
|
||||
codeowners = optional(list(string), [])
|
||||
merge_type = optional(string, "squash")
|
||||
delete_branch_on_merge = optional(bool, true)
|
||||
branch_protection = optional(bool)
|
||||
tag_protection = optional(bool)
|
||||
required_checks = optional(list(string), [])
|
||||
rulesets = optional(list(string), [])
|
||||
exclude_rulesets = optional(list(string), [])
|
||||
}))
|
||||
default = {}
|
||||
|
||||
@@ -84,29 +80,24 @@ variable "repository" {
|
||||
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([for repo in var.repository : !(repo.private && coalesce(repo.branch_protection, false))])
|
||||
error_message = "branch_protection is not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && coalesce(repo.branch_protection, false)])}."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = alltrue([for repo in var.repository : !(repo.private && coalesce(repo.tag_protection, false))])
|
||||
error_message = "tag_protection is not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && coalesce(repo.tag_protection, false)])}."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = alltrue([for repo in var.repository : !(repo.private && length(repo.required_checks) > 0)])
|
||||
error_message = "required_checks are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.required_checks) > 0])}."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = alltrue([for repo in var.repository : !(repo.private && length(repo.rulesets) > 0)])
|
||||
error_message = "rulesets are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.rulesets) > 0])}."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = alltrue(flatten([for repo in var.repository : [for rs in repo.rulesets : contains(keys(var.ruleset), rs)]]))
|
||||
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)]])))}."
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user