diff --git a/README.md b/README.md index b0151dd..f4652f7 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,12 @@ codeowners: [] # push access, for adding to CODEOWNERS later merge_type: squash # squash | merge | rebase delete_branch_on_merge: true branch_protection: !private # generated `branch-protection` ruleset on the default branch +tag_protection: !private # generated `tag-protection` ruleset: refs/tags/v* undeletable, immutable required_checks: [] # generated `required-checks` ruleset (strict) rulesets: [] # custom rulesets from ruleset/ ``` -`ruleset/.yaml` (custom extras; `branch-protection` and `required-checks` are reserved): +`ruleset/.yaml` (custom extras; `branch-protection`, `required-checks` and `tag-protection` are reserved): ```yaml target: branch @@ -44,4 +45,4 @@ rules: non_fast_forward: true ``` -Private repositories cannot use `branch_protection`, `required_checks` or `rulesets` on the free plan; plan fails if they do. +Private repositories cannot use `branch_protection`, `tag_protection`, `required_checks` or `rulesets` on the free plan; plan fails if they do. diff --git a/modules/github_org/main.tf b/modules/github_org/main.tf index 09ce5fc..51fefd9 100644 --- a/modules/github_org/main.tf +++ b/modules/github_org/main.tf @@ -58,6 +58,23 @@ locals { } }) } if length(r.required_checks) > 0 + }, + { + for name, r in var.repository : "${name}/tag-protection" => { + repository = name + 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 + } + } + } if coalesce(r.tag_protection, !r.private) }], )...) } diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index f839c1c..993406e 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -34,8 +34,19 @@ run "public_repo_defaults" { } assert { - condition = keys(github_repository_ruleset.this) == [".github/branch-protection"] && length(github_team_membership.this) == 2 - error_message = "public repo must get only the generated branch-protection ruleset" + condition = keys(github_repository_ruleset.this) == [".github/branch-protection", ".github/tag-protection"] && length(github_team_membership.this) == 2 + error_message = "public repo must get only the generated branch-protection and tag-protection rulesets" + } + + assert { + condition = ( + github_repository_ruleset.this[".github/tag-protection"].target == "tag" && + github_repository_ruleset.this[".github/tag-protection"].enforcement == "active" && + github_repository_ruleset.this[".github/tag-protection"].conditions[0].ref_name[0].include == tolist(["refs/tags/v*"]) && + github_repository_ruleset.this[".github/tag-protection"].rules[0].deletion && + github_repository_ruleset.this[".github/tag-protection"].rules[0].non_fast_forward + ) + error_message = "tag-protection ruleset must make refs/tags/v* undeletable and immutable" } assert { @@ -99,13 +110,13 @@ run "generated_and_custom_rulesets" { variables { repository = { - checked = { name = "checked", private = false, branch_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] } + checked = { name = "checked", private = false, branch_protection = false, tag_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] } } } assert { condition = toset(keys(github_repository_ruleset.this)) == toset(["checked/required-checks", "checked/signed"]) - error_message = "required_checks without branch_protection yields only required-checks plus custom rulesets" + error_message = "required_checks with protections off yields only required-checks plus custom rulesets" } assert { @@ -131,6 +142,46 @@ run "private_branch_protection_rejected" { expect_failures = [var.repository] } +run "public_tag_protection_only" { + command = plan + + variables { + repository = { + tags = { name = "tags", private = false, branch_protection = false } + } + } + + assert { + condition = keys(github_repository_ruleset.this) == ["tags/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 diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index 14ce930..f9b80f7 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -46,8 +46,8 @@ variable "ruleset" { default = {} validation { - condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks"])) == 0 - error_message = "Ruleset names branch-protection and required-checks are reserved for generated rulesets." + condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection"])) == 0 + error_message = "Ruleset names branch-protection, required-checks and tag-protection are reserved for generated rulesets." } } @@ -63,6 +63,7 @@ variable "repository" { 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), []) })) @@ -78,6 +79,11 @@ variable "repository" { 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])}."