|
1 | 1 | locals { |
2 | 2 | enabled = module.this.enabled |
| 3 | + |
| 4 | + environments = local.enabled ? { |
| 5 | + for env in var.environments : |
| 6 | + (format( |
| 7 | + "${env.tenant != null ? "%[1]s/" : ""}%[2]s-%[3]s${length(env.attributes) > 0 ? "-%[4]s" : "%[4]s"}", |
| 8 | + env.tenant, |
| 9 | + env.environment, |
| 10 | + env.stage, |
| 11 | + join("-", env.attributes) |
| 12 | + )) => env |
| 13 | + } : {} |
| 14 | + |
| 15 | + manifest_kubernetes_namespace = var.manifest_kubernetes_namespace |
| 16 | + |
| 17 | + team_slugs = toset(compact([ |
| 18 | + for permission in var.permissions : lookup(permission, "team_slug", null) |
| 19 | + ])) |
| 20 | + |
| 21 | + team_ids = [ |
| 22 | + for team in data.github_team.default : team.id |
| 23 | + ] |
| 24 | + |
| 25 | + team_permissions = { |
| 26 | + for index, id in local.team_ids : (var.permissions[index].team_slug) => { |
| 27 | + id = id |
| 28 | + permission = var.permissions[index].permission |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + empty_repo = { |
| 33 | + name = "" |
| 34 | + default_branch = "" |
| 35 | + } |
| 36 | + |
| 37 | + github_repository = try((var.create_repo ? github_repository.default : data.github_repository.default)[0], local.empty_repo) |
| 38 | +} |
| 39 | + |
| 40 | +data "github_repository" "default" { |
| 41 | + count = local.enabled && !var.create_repo ? 1 : 0 |
| 42 | + name = var.name |
| 43 | +} |
| 44 | + |
| 45 | +resource "github_repository" "default" { |
| 46 | + count = local.enabled && var.create_repo ? 1 : 0 |
| 47 | + |
| 48 | + name = module.this.name |
| 49 | + description = var.description |
| 50 | + auto_init = true # will create a 'main' branch |
| 51 | + |
| 52 | + visibility = "private" |
| 53 | + vulnerability_alerts = var.vulnerability_alerts_enabled |
| 54 | + |
| 55 | + web_commit_signoff_required = var.web_commit_signoff_required |
| 56 | +} |
| 57 | + |
| 58 | +resource "github_branch_default" "default" { |
| 59 | + count = local.enabled ? 1 : 0 |
| 60 | + |
| 61 | + repository = local.github_repository.name |
| 62 | + branch = local.github_repository.default_branch |
3 | 63 | } |
4 | 64 |
|
| 65 | +data "github_user" "automation_user" { |
| 66 | + count = local.enabled ? 1 : 0 |
5 | 67 |
|
| 68 | + username = var.github_user |
| 69 | +} |
| 70 | + |
| 71 | +resource "github_branch_protection" "default" { |
| 72 | + # This resource enforces PRs needing to be opened in order for changes to be made, except for automated commits to |
| 73 | + # the main branch. Those commits made by the automation user, which is an admin. |
| 74 | + count = local.enabled ? 1 : 0 |
| 75 | + |
| 76 | + repository_id = local.github_repository.name |
| 77 | + |
| 78 | + pattern = join("", github_branch_default.default[*].branch) |
| 79 | + enforce_admins = false # needs to be false in order to allow automation user to push |
| 80 | + allows_deletions = true |
| 81 | + |
| 82 | + dynamic "required_pull_request_reviews" { |
| 83 | + for_each = var.required_pull_request_reviews ? [0] : [] |
| 84 | + content { |
| 85 | + dismiss_stale_reviews = true |
| 86 | + restrict_dismissals = true |
| 87 | + require_code_owner_reviews = true |
| 88 | + } |
| 89 | + } |
6 | 90 |
|
| 91 | + restrict_pushes { |
| 92 | + blocks_creations = var.restrict_pushes_blocks_creations |
| 93 | + push_allowances = var.push_restrictions_enabled ? [ |
| 94 | + join("", data.github_user.automation_user[*].node_id), |
| 95 | + ] : [] |
| 96 | + } |
7 | 97 |
|
| 98 | + lifecycle { |
| 99 | + ignore_changes = [ |
| 100 | + restrict_pushes[0].push_allowances |
| 101 | + ] |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +data "github_team" "default" { |
| 106 | + for_each = local.team_slugs |
| 107 | + |
| 108 | + slug = each.value |
| 109 | +} |
8 | 110 |
|
| 111 | +resource "github_team_repository" "default" { |
| 112 | + for_each = local.team_permissions |
| 113 | + |
| 114 | + repository = local.github_repository.name |
| 115 | + team_id = each.value.id |
| 116 | + permission = each.value.permission |
| 117 | +} |
| 118 | + |
| 119 | +resource "tls_private_key" "default" { |
| 120 | + for_each = local.environments |
| 121 | + |
| 122 | + algorithm = "RSA" |
| 123 | + rsa_bits = "2048" |
| 124 | +} |
| 125 | + |
| 126 | +resource "github_repository_deploy_key" "default" { |
| 127 | + for_each = local.environments |
| 128 | + |
| 129 | + title = "Deploy key for ArgoCD environment: ${each.key} (${local.github_repository.default_branch} branch)" |
| 130 | + repository = local.github_repository.name |
| 131 | + key = tls_private_key.default[each.key].public_key_openssh |
| 132 | + read_only = true |
| 133 | +} |
0 commit comments