Apply all_repos rulesets to private GitHub repos
This commit is contained in:
@@ -40,14 +40,14 @@ maintainers: [] # users or @team: maintain access, seed initial .gith
|
||||
codeowners: [] # push access
|
||||
merge_type: squash # squash | merge | rebase
|
||||
delete_branch_on_merge: true
|
||||
rulesets: [] # opt-in rulesets from ruleset/; public repos only
|
||||
rulesets: [] # opt-in rulesets from ruleset/
|
||||
exclude_rulesets: [] # opt out of non-mandatory all_repos rulesets
|
||||
```
|
||||
|
||||
`ruleset/<name>.yaml`:
|
||||
|
||||
```yaml
|
||||
all_repos: false # apply to every public repo; private repos get no all_repos rulesets
|
||||
all_repos: false # apply to every repo
|
||||
mandatory: false # cannot be excluded
|
||||
target: branch # branch | tag
|
||||
enforcement: active
|
||||
@@ -62,4 +62,4 @@ rules:
|
||||
required_status_checks: {contexts: [ci/woodpecker], strict: true} # branch only
|
||||
```
|
||||
|
||||
Shipped rulesets: `branch-protection`, `tag-protection` (all public repos), `ref-names-branch`, `ref-names-tag` (mandatory).
|
||||
Shipped rulesets: `branch-protection`, `tag-protection` (all repos), `ref-names-branch`, `ref-names-tag` (mandatory).
|
||||
|
||||
@@ -23,9 +23,8 @@ module "repository" {
|
||||
}
|
||||
|
||||
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)]) :
|
||||
for rs in setunion(repo.rulesets, [for k, v in var.ruleset : k if v.all_repos && !contains(repo.exclude_rulesets, k)]) :
|
||||
"${repo_name}/${rs}" => { repository = repo_name, ruleset = rs }
|
||||
}]...)
|
||||
}
|
||||
@@ -114,7 +113,7 @@ resource "github_organization_settings" "this" {
|
||||
}
|
||||
}
|
||||
|
||||
# Free plan only has the Default runner group; it is imported and updated, never created or deleted.
|
||||
# The Default runner group is imported and updated, never created or deleted.
|
||||
resource "github_actions_runner_group" "default" {
|
||||
count = var.settings == null ? 0 : 1
|
||||
|
||||
|
||||
@@ -87,8 +87,8 @@ run "private_repo_defaults" {
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = length(module.ruleset) == 0 && module.repository["secret"].codeowners != null
|
||||
error_message = "private repo silently skips all_repos rulesets but still gets CODEOWNERS"
|
||||
condition = keys(module.ruleset) == ["secret/branch-protection", "secret/ref-names-tag", "secret/tag-protection"] && module.repository["secret"].codeowners != null
|
||||
error_message = "private repo gets all_repos rulesets and CODEOWNERS"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,16 +146,19 @@ run "exclusions_and_custom_rulesets" {
|
||||
}
|
||||
}
|
||||
|
||||
run "private_rulesets_rejected" {
|
||||
run "private_repo_custom_rulesets" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
secret = { name = "secret", rulesets = ["signed"] }
|
||||
secret = { name = "secret", rulesets = ["signed"], exclude_rulesets = ["branch-protection", "tag-protection"] }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
assert {
|
||||
condition = keys(module.ruleset) == ["secret/ref-names-tag", "secret/signed"]
|
||||
error_message = "private repo may list rulesets"
|
||||
}
|
||||
}
|
||||
|
||||
run "mandatory_exclusion_rejected" {
|
||||
|
||||
@@ -80,11 +80,6 @@ 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 && 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 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)]])))}."
|
||||
|
||||
Reference in New Issue
Block a user