414 lines
12 KiB
HCL
414 lines
12 KiB
HCL
mock_provider "github" {
|
|
mock_resource "github_team" {
|
|
defaults = { slug = "maintainers-slug" }
|
|
}
|
|
}
|
|
|
|
variables {
|
|
owner = "IUCLabs"
|
|
billing_email = "billing@example.com"
|
|
team = {
|
|
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 } } }
|
|
}
|
|
}
|
|
|
|
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.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.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 = "ruleset definition must be applied to the paired repository unchanged"
|
|
}
|
|
|
|
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.ruleset) == 0 && module.repository["secret"].codeowners != null
|
|
error_message = "private repo silently skips all_repos 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 "exclusions_and_custom_rulesets" {
|
|
command = plan
|
|
|
|
variables {
|
|
repository = {
|
|
checked = { name = "checked", private = false, exclude_rulesets = ["branch-protection", "tag-protection"], rulesets = ["signed", "checks"] }
|
|
}
|
|
}
|
|
|
|
assert {
|
|
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.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"
|
|
}
|
|
}
|
|
|
|
run "private_rulesets_rejected" {
|
|
command = plan
|
|
|
|
variables {
|
|
repository = {
|
|
secret = { name = "secret", rulesets = ["signed"] }
|
|
}
|
|
}
|
|
|
|
expect_failures = [var.repository]
|
|
}
|
|
|
|
run "mandatory_exclusion_rejected" {
|
|
command = plan
|
|
|
|
variables {
|
|
repository = {
|
|
public = { name = "public", private = false, exclude_rulesets = ["ref-names-tag"] }
|
|
}
|
|
}
|
|
|
|
expect_failures = [var.repository]
|
|
}
|
|
|
|
run "applied_and_excluded_rejected" {
|
|
command = plan
|
|
|
|
variables {
|
|
repository = {
|
|
public = { name = "public", private = false, rulesets = ["signed"], exclude_rulesets = ["signed"] }
|
|
}
|
|
}
|
|
|
|
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" {
|
|
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]
|
|
}
|
|
|
|
run "org_settings_unmanaged_without_config" {
|
|
command = plan
|
|
|
|
assert {
|
|
condition = (
|
|
length(github_organization_settings.this) == 0 &&
|
|
length(github_actions_runner_group.default) == 0 &&
|
|
length(github_actions_organization_workflow_permissions.this) == 0
|
|
)
|
|
error_message = "org settings, workflow permissions and the default runner group must stay unmanaged without config.yaml"
|
|
}
|
|
}
|
|
|
|
run "org_settings_pinned" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = { name = "IUCLabs", description = "Labs" }
|
|
}
|
|
|
|
assert {
|
|
condition = (
|
|
github_actions_organization_workflow_permissions.this[0].default_workflow_permissions == "read" &&
|
|
!github_actions_organization_workflow_permissions.this[0].can_approve_pull_request_reviews
|
|
)
|
|
error_message = "GITHUB_TOKEN must default to read-only and never approve PRs"
|
|
}
|
|
|
|
assert {
|
|
condition = (
|
|
github_organization_settings.this[0].name == "IUCLabs" &&
|
|
github_organization_settings.this[0].description == "Labs" &&
|
|
github_organization_settings.this[0].blog == null
|
|
)
|
|
error_message = "profile fields must come from config, unset ones stay null"
|
|
}
|
|
|
|
assert {
|
|
condition = alltrue([for f in [
|
|
"members_can_create_pages", "members_can_create_public_pages", "members_can_create_private_pages",
|
|
"members_can_fork_private_repositories", "members_can_create_internal_repositories", "has_repository_projects", "web_commit_signoff_required",
|
|
"advanced_security_enabled_for_new_repositories", "dependabot_alerts_enabled_for_new_repositories",
|
|
"dependabot_security_updates_enabled_for_new_repositories", "dependency_graph_enabled_for_new_repositories",
|
|
"secret_scanning_enabled_for_new_repositories", "secret_scanning_push_protection_enabled_for_new_repositories",
|
|
] : github_organization_settings.this[0][f] == false])
|
|
error_message = "pages, private forks, internal repos, repo projects, signoff and legacy security toggles must be pinned false"
|
|
}
|
|
|
|
assert {
|
|
condition = github_organization_settings.this[0].has_organization_projects == true
|
|
error_message = "organization projects must stay enabled"
|
|
}
|
|
}
|
|
|
|
run "org_settings_from_config" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = { public_runners = true }
|
|
}
|
|
|
|
assert {
|
|
condition = (
|
|
!github_organization_settings.this[0].members_can_create_repositories &&
|
|
!github_organization_settings.this[0].members_can_create_public_repositories &&
|
|
!github_organization_settings.this[0].members_can_create_private_repositories &&
|
|
github_organization_settings.this[0].default_repository_permission == "read"
|
|
)
|
|
error_message = "repo creation must be blocked for every visibility, default permission read"
|
|
}
|
|
|
|
assert {
|
|
condition = (
|
|
github_actions_runner_group.default[0].name == "Default" &&
|
|
github_actions_runner_group.default[0].allows_public_repositories
|
|
)
|
|
error_message = "public_runners must open the Default runner group to public repositories"
|
|
}
|
|
}
|
|
|
|
run "org_settings_allow_repo_creation" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = { members_can_create_repositories = true }
|
|
}
|
|
|
|
assert {
|
|
condition = (
|
|
github_organization_settings.this[0].members_can_create_public_repositories &&
|
|
github_organization_settings.this[0].members_can_create_private_repositories &&
|
|
!github_actions_runner_group.default[0].allows_public_repositories
|
|
)
|
|
error_message = "per-visibility creation flags must follow members_can_create_repositories; public_runners defaults off"
|
|
}
|
|
}
|
|
|
|
run "default_repository_permission_none_accepted" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = { default_repository_permission = "none" }
|
|
}
|
|
|
|
assert {
|
|
condition = github_organization_settings.this[0].default_repository_permission == "none"
|
|
error_message = "default_repository_permission none must be accepted"
|
|
}
|
|
}
|
|
|
|
run "invalid_default_repository_permission_rejected" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = { default_repository_permission = "write" }
|
|
}
|
|
|
|
expect_failures = [var.settings]
|
|
}
|
|
|
|
run "settings_without_billing_email_rejected" {
|
|
command = plan
|
|
|
|
variables {
|
|
settings = {}
|
|
billing_email = null
|
|
}
|
|
|
|
expect_failures = [var.billing_email]
|
|
}
|