diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index 64b58a7..01ff6cf 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -297,7 +297,72 @@ run "bypass_team_without_slug_rejected" { variables { ruleset = { - guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "Team", actor_id = 1 }] } + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "Team" }] } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "bypass_unknown_actor_type_rejected" { + command = plan + + variables { + ruleset = { + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "Bot" }] } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "bypass_unknown_mode_rejected" { + command = plan + + variables { + ruleset = { + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "OrganizationAdmin", bypass_mode = "sometimes" }] } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "bypass_actor_id_missing_rejected" { + command = plan + + variables { + ruleset = { + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "User" }] } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "bypass_actor_id_on_idless_actor_rejected" { + command = plan + + variables { + ruleset = { + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "DeployKey", actor_id = 1 }] } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + +run "bypass_team_with_actor_id_rejected" { + command = plan + + variables { + ruleset = { + guarded = { name = "guarded", rules = {}, bypass_actors = [{ actor_type = "Team", team = "maintainers", actor_id = 1 }] } } repository = {} } diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index 0ea439b..aa2cdcd 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -59,6 +59,26 @@ variable "ruleset" { error_message = "pull_request and required_status_checks are branch-only rules, not allowed on tag rulesets: ${join(", ", [for k, rs in var.ruleset : k if rs.target != "branch" && (rs.rules.pull_request != null || rs.rules.required_status_checks != null)])}." } + validation { + condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : contains(["RepositoryRole", "Team", "Integration", "OrganizationAdmin", "DeployKey", "EnterpriseOwner", "User"], a.actor_type)]])) + error_message = "bypass_actors actor_type must be RepositoryRole, Team, Integration, OrganizationAdmin, DeployKey, EnterpriseOwner or User: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if !contains(["RepositoryRole", "Team", "Integration", "OrganizationAdmin", "DeployKey", "EnterpriseOwner", "User"], a.actor_type)]]))}." + } + + validation { + condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : contains(["always", "pull_request", "exempt"], a.bypass_mode)]])) + error_message = "bypass_actors bypass_mode must be always, pull_request or exempt: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}/${a.bypass_mode}" if !contains(["always", "pull_request", "exempt"], a.bypass_mode)]]))}." + } + + validation { + condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : a.actor_id != null if contains(["RepositoryRole", "Integration", "User"], a.actor_type)]])) + error_message = "bypass_actors actor_id is required for RepositoryRole, Integration and User: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if contains(["RepositoryRole", "Integration", "User"], a.actor_type) && a.actor_id == null]]))}." + } + + validation { + condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : a.actor_id == null if !contains(["RepositoryRole", "Integration", "User"], a.actor_type)]])) + error_message = "bypass_actors actor_id must not be set for Team (use team), OrganizationAdmin, EnterpriseOwner or DeployKey: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if !contains(["RepositoryRole", "Integration", "User"], a.actor_type) && a.actor_id != null]]))}." + } + validation { condition = alltrue(flatten([for rs in var.ruleset : [for a in rs.bypass_actors : (a.actor_type == "Team") == (a.team != null)]])) error_message = "bypass_actors team must be set exactly when actor_type is Team: ${join(", ", flatten([for k, rs in var.ruleset : [for a in rs.bypass_actors : "${k}/${a.actor_type}" if(a.actor_type == "Team") != (a.team != null)]]))}."