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
terraform-git/modules/github_org/tests/github_org.tftest.hcl
T
unkin-agent 4fa1112ef1
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful
Block + ref components on public GitHub repos
2026-11-10 23:04:55 +11:00

332 lines
8.8 KiB
HCL

mock_provider "github" {
mock_resource "github_team" {
defaults = { slug = "maintainers-slug" }
}
}
variables {
owner = "IUCLabs"
team = {
maintainers = { name = "maintainers", maintainers = ["alice"], members = ["bob"] }
}
ruleset = {
signed = { name = "signed", rules = { deletion = true, pull_request = {} } }
}
}
run "public_repo_defaults" {
command = plan
variables {
repository = {
".github" = { name = ".github", private = false, maintainers = ["@maintainers", "carol"], codeowners = ["carol", "dave"] }
}
}
assert {
condition = module.repository[".github"].codeowners.content == "* @IUCLabs/maintainers-slug @carol\n"
error_message = "CODEOWNERS must list maintainers, teams by owner-qualified slug"
}
assert {
condition = module.repository[".github"].access == { "@maintainers" = "maintain", carol = "maintain", dave = "push" }
error_message = "maintainers get maintain (winning over codeowners), codeowners get push"
}
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"
}
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
)
error_message = "tag-protection ruleset must make refs/tags/v* undeletable and immutable"
}
assert {
condition = (
module.repository[".github"].repository.allow_squash_merge &&
!module.repository[".github"].repository.allow_merge_commit &&
!module.repository[".github"].repository.allow_rebase_merge
)
error_message = "merge_type defaults to squash only"
}
}
run "private_repo_defaults" {
command = plan
variables {
repository = {
secret = { name = "secret", maintainers = ["@maintainers"] }
}
}
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"
}
}
run "merge_type_rebase" {
command = plan
variables {
repository = {
r = { name = "r", merge_type = "rebase" }
m = { name = "m", merge_type = "merge" }
}
}
assert {
condition = (
module.repository["r"].repository.allow_rebase_merge && !module.repository["r"].repository.allow_squash_merge && !module.repository["r"].repository.allow_merge_commit &&
module.repository["m"].repository.allow_merge_commit && !module.repository["m"].repository.allow_squash_merge && !module.repository["m"].repository.allow_rebase_merge
)
error_message = "merge_type must enable exactly the matching allow_* flag"
}
}
run "invalid_merge_type_rejected" {
command = plan
variables {
repository = {
bad = { name = "bad", merge_type = "fast-forward" }
}
}
expect_failures = [var.repository]
}
run "generated_and_custom_rulesets" {
command = plan
variables {
repository = {
checked = { name = "checked", private = false, branch_protection = false, tag_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] }
}
}
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"
}
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"
}
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" {
command = plan
variables {
repository = {
secret = { name = "secret", rulesets = ["signed"] }
}
}
expect_failures = [var.repository]
}
run "reserved_ruleset_name_rejected" {
command = plan
variables {
ruleset = {
branch-protection = { name = "branch-protection", rules = { deletion = true } }
}
repository = {}
}
expect_failures = [var.ruleset]
}
run "reserved_ref_names_rejected" {
command = plan
variables {
ruleset = {
ref-names-tag = { name = "ref-names-tag", target = "tag", rules = { deletion = true } }
}
repository = {}
}
expect_failures = [var.ruleset]
}
run "unknown_ruleset_rejected" {
command = plan
variables {
repository = {
public = { name = "public", private = false, rulesets = ["missing"] }
}
}
expect_failures = [var.repository]
}
run "unknown_team_rejected" {
command = plan
variables {
repository = {
public = { name = "public", codeowners = ["@missing"] }
}
}
expect_failures = [var.repository]
}
run "non_main_default_branch_renamed" {
command = plan
variables {
repository = {
legacy = { name = "legacy", default_branch = "master", maintainers = ["carol"] }
}
}
assert {
condition = (
module.repository["legacy"].branch_default.branch == "master" &&
module.repository["legacy"].branch_default.rename &&
module.repository["legacy"].branch_default.wait_for_rename
)
error_message = "default_branch must rename the auto-init main and wait for it to propagate"
}
assert {
condition = module.repository["legacy"].codeowners.branch == "master"
error_message = "CODEOWNERS must be committed to the renamed default branch"
}
}
run "tag_ruleset_pull_request_rejected" {
command = plan
variables {
ruleset = {
tagpr = { name = "tagpr", target = "tag", rules = { pull_request = {} } }
}
repository = {}
}
expect_failures = [var.ruleset]
}
run "tag_ruleset_status_checks_rejected" {
command = plan
variables {
ruleset = {
tagci = { name = "tagci", target = "tag", rules = { required_status_checks = { contexts = ["ci"] } } }
}
repository = {}
}
expect_failures = [var.ruleset]
}
run "empty_status_check_contexts_rejected" {
command = plan
variables {
ruleset = {
noci = { name = "noci", rules = { required_status_checks = { contexts = [] } } }
}
repository = {}
}
expect_failures = [var.ruleset]
}