Define all GitHub rulesets as ruleset/ files
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful

This commit is contained in:
2026-11-14 19:25:07 +11:00
parent a82e91d941
commit 5a786be0e6
11 changed files with 176 additions and 238 deletions
+56 -106
View File
@@ -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" {