Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ locals {
])
)
]))

# Extract all roles used in permissions across all repositories
base_roles = ["pull", "triage", "push", "maintain", "admin"]
all_permission_roles = distinct(flatten([
for k, repo in var.repositories :
values(coalesce(repo.permissions, {}))
]))

# Check if any role is not in base_roles
has_custom_roles = length([
for role in local.all_permission_roles :
role if !contains(local.base_roles, role)
]) > 0
}

# Fetch user data for referenced users (only if not provided)
Expand All @@ -46,6 +59,11 @@ data "github_user" "referenced_users" {
username = each.value
}

# Fetch organization custom roles (only if custom roles are detected)
data "github_organization_repository_roles" "all" {
count = local.has_custom_roles ? 1 : 0
}

# Fetch app installation data for referenced apps (only if not provided)
data "github_app" "bypass_apps" {
for_each = length(var.github_app_ids) == 0 ? toset(local.bypass_app_slugs) : toset([])
Expand All @@ -68,4 +86,10 @@ locals {
github_app_ids = length(var.github_app_ids) > 0 ? var.github_app_ids : {
for slug, app in data.github_app.bypass_apps : slug => app.id
}

# Build allowed roles list: base roles + custom roles (if fetched)
allowed_roles = local.has_custom_roles ? concat(
local.base_roles,
[for role in data.github_organization_repository_roles.all[0].roles : role.name]
) : local.base_roles
}
Binary file removed examples/complete/tfplan
Binary file not shown.
8 changes: 5 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ module "repositories" {
template = each.value.template

# Access & Permissions
permissions = each.value.permissions
deploy_keys = each.value.deploy_keys
allowed_roles = each.value.allowed_roles
permissions = each.value.permissions
deploy_keys = each.value.deploy_keys

# Automation (Global)
webhooks = each.value.webhooks
Expand All @@ -130,4 +129,7 @@ module "repositories" {
github_team_ids = local.github_team_ids
github_user_ids = local.github_user_ids
github_app_ids = local.github_app_ids

# Pre-fetched allowed roles (base + custom if detected)
allowed_roles = local.allowed_roles
}