forked from mage-os/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
163 lines (145 loc) · 4.45 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
terraform {
backend "gcs" {
bucket = "mage-os-tf-state"
prefix = "terraform/state"
}
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
}
provider "github" {
owner = var.organization_name
app_auth {
id = var.github_app_id
installation_id = var.github_app_installation_id
pem_file = var.github_app_pem_file
}
}
data "github_user" "mage-os-ci" {
username = "mage-os-ci"
}
# Test change
# Using exclusively github_team_membership properly sends invitation, but
# doesn't handle revoking organization membership. Someone can be a part of
# organization, but not belong to any team. Stating membership explicitly also
# makes transitions between administrators and normal members possible in
# single apply.
resource "github_membership" "users" {
for_each = toset(distinct(concat(
flatten([for team in var.teams : team.members]),
var.administrators,
)))
username = each.key
role = contains(var.administrators, each.key) ? "admin" : "member"
}
resource "github_team" "teams" {
for_each = var.teams
name = each.key
description = each.value.description
privacy = "closed"
}
resource "github_team_membership" "members" {
for_each = { for i in flatten([
for team_name, team in var.teams : [
for member in team.members : {
team = team_name
user = member
}
]
]) : "${i.user}_${i.team}" => i }
team_id = github_team.teams[each.value.team].id
username = each.value.user
# Administrators are automatically assigned maintainer role. Do it explicitly
# to avoid state discrepancy.
role = contains(
var.administrators, each.value.user
) ? "maintainer" : "member"
}
resource "github_repository" "mirrors" {
for_each = var.mirror_repositories
name = each.key
description = each.value.description
has_issues = false
has_projects = false
has_wiki = false
lifecycle {
# Never try to replace repository in order to change its configuration.
prevent_destroy = true
}
}
resource "github_branch_protection" "mirrors" {
for_each = var.mirror_repositories
repository_id = github_repository.mirrors[each.key].node_id
pattern = "*"
enforce_admins = true
push_restrictions = [data.github_user.mage-os-ci.node_id]
allows_force_pushes = false
}
resource "github_repository" "repositories" {
for_each = var.repositories
name = each.key
description = each.value.description
has_issues = true
has_projects = true
has_wiki = false
auto_init = true
lifecycle {
# Never try to replace repository in order to change its configuration.
prevent_destroy = true
}
}
resource "github_branch_protection" "repositories" {
for_each = var.repositories
repository_id = github_repository.repositories[each.key].node_id
pattern = "*"
push_restrictions = []
required_status_checks {
strict = true
contexts = []
}
required_pull_request_reviews {
require_code_owner_reviews = true
required_approving_review_count = 2
dismiss_stale_reviews = true
restrict_dismissals = true
dismissal_restrictions = [
github_team.teams["tech-lead"].node_id,
]
}
}
resource "github_team_repository" "teams" {
for_each = { for i in flatten([
for repository_name, repository in var.repositories : [
for team in try(repository.teams, []) : {
repository = repository_name
team = team
}
]
]) : "${i.repository}_${i.team}" => i }
team_id = github_team.teams[each.value.team].name
repository = github_repository.repositories[each.value.repository].name
permission = "push"
}
resource "github_team_repository" "tech-lead" {
for_each = var.repositories
team_id = github_team.teams["tech-lead"].id
repository = github_repository.repositories[each.key].name
permission = "maintain"
}
resource "github_repository_file" "codeowners" {
for_each = var.repositories
repository = github_repository.repositories[each.key].name
branch = github_repository.repositories[each.key].default_branch
file = "CODEOWNERS"
content = "* ${join(
" ",
formatlist("@${var.organization_name}/%s", try(each.value.teams, []))
)}"
commit_message = "Managed by Terraform"
commit_author = "Mage-OS CI Bot"
commit_email = "[email protected]"
overwrite_on_create = true
}