manage IUCLabs GitHub organisation
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
locals {
|
||||
team_membership = merge([
|
||||
for team, t in var.team : merge(
|
||||
{ for u in t.members : "${team}/${u}" => { team = team, username = u, role = "member" } },
|
||||
{ for u in t.maintainers : "${team}/${u}" => { team = team, username = u, role = "maintainer" } },
|
||||
)
|
||||
]...)
|
||||
|
||||
repository_access = {
|
||||
for name, r in var.repository : name => merge(
|
||||
{ for e in r.codeowners : e => "push" },
|
||||
{ for e in r.maintainers : e => "maintain" },
|
||||
)
|
||||
}
|
||||
|
||||
repository_ruleset = merge([
|
||||
for name, r in var.repository : {
|
||||
for rs in r.rulesets : "${name}/${rs}" => { repository = name, ruleset = var.ruleset[rs] }
|
||||
}
|
||||
]...)
|
||||
}
|
||||
|
||||
resource "github_team" "this" {
|
||||
for_each = var.team
|
||||
|
||||
name = each.value.name
|
||||
description = each.value.description
|
||||
privacy = each.value.privacy
|
||||
}
|
||||
|
||||
resource "github_team_membership" "this" {
|
||||
for_each = local.team_membership
|
||||
|
||||
team_id = github_team.this[each.value.team].id
|
||||
username = each.value.username
|
||||
role = each.value.role
|
||||
}
|
||||
|
||||
resource "github_repository" "this" {
|
||||
for_each = var.repository
|
||||
|
||||
name = each.value.name
|
||||
description = each.value.description
|
||||
visibility = each.value.private ? "private" : "public"
|
||||
has_issues = each.value.has_issues
|
||||
has_wiki = each.value.has_wiki
|
||||
has_projects = each.value.has_projects
|
||||
allow_squash_merge = each.value.allow_squash_merge
|
||||
allow_merge_commit = each.value.allow_merge_commit
|
||||
allow_rebase_merge = each.value.allow_rebase_merge
|
||||
delete_branch_on_merge = each.value.delete_branch_on_merge
|
||||
auto_init = each.value.auto_init
|
||||
}
|
||||
|
||||
resource "github_branch_default" "this" {
|
||||
for_each = var.repository
|
||||
|
||||
repository = github_repository.this[each.key].name
|
||||
branch = each.value.default_branch
|
||||
}
|
||||
|
||||
resource "github_repository_collaborators" "this" {
|
||||
for_each = { for name, access in local.repository_access : name => access if length(access) > 0 }
|
||||
|
||||
repository = github_repository.this[each.key].name
|
||||
|
||||
dynamic "user" {
|
||||
for_each = { for e, p in each.value : e => p if !startswith(e, "@") }
|
||||
content {
|
||||
username = user.key
|
||||
permission = user.value
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "team" {
|
||||
for_each = { for e, p in each.value : trimprefix(e, "@") => p if startswith(e, "@") }
|
||||
content {
|
||||
team_id = team.key
|
||||
permission = team.value
|
||||
}
|
||||
}
|
||||
|
||||
depends_on = [github_team.this]
|
||||
}
|
||||
|
||||
resource "github_repository_file" "codeowners" {
|
||||
for_each = { for name, r in var.repository : name => r if length(r.maintainers) > 0 }
|
||||
|
||||
repository = github_repository.this[each.key].name
|
||||
branch = github_branch_default.this[each.key].branch
|
||||
file = ".github/CODEOWNERS"
|
||||
content = "* ${join(" ", [for e in each.value.maintainers : startswith(e, "@") ? "@${var.owner}/${trimprefix(e, "@")}" : "@${e}"])}\n"
|
||||
commit_message = "add initial CODEOWNERS"
|
||||
overwrite_on_create = false
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [content]
|
||||
}
|
||||
}
|
||||
|
||||
resource "github_repository_ruleset" "this" {
|
||||
for_each = local.repository_ruleset
|
||||
|
||||
name = each.value.ruleset.name
|
||||
repository = github_repository.this[each.value.repository].name
|
||||
target = each.value.ruleset.target
|
||||
enforcement = each.value.ruleset.enforcement
|
||||
|
||||
dynamic "conditions" {
|
||||
for_each = each.value.ruleset.conditions[*]
|
||||
content {
|
||||
ref_name {
|
||||
include = conditions.value.ref_name.include
|
||||
exclude = conditions.value.ref_name.exclude
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rules {
|
||||
deletion = each.value.ruleset.rules.deletion
|
||||
non_fast_forward = each.value.ruleset.rules.non_fast_forward
|
||||
|
||||
dynamic "pull_request" {
|
||||
for_each = each.value.ruleset.rules.pull_request[*]
|
||||
content {
|
||||
required_approving_review_count = pull_request.value.required_approving_review_count
|
||||
dismiss_stale_reviews_on_push = pull_request.value.dismiss_stale_reviews_on_push
|
||||
require_code_owner_review = pull_request.value.require_code_owner_review
|
||||
require_last_push_approval = pull_request.value.require_last_push_approval
|
||||
required_review_thread_resolution = pull_request.value.required_review_thread_resolution
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "required_status_checks" {
|
||||
for_each = each.value.ruleset.rules.required_status_checks[*]
|
||||
content {
|
||||
strict_required_status_checks_policy = required_status_checks.value.strict
|
||||
dynamic "required_check" {
|
||||
for_each = toset(required_status_checks.value.contexts)
|
||||
content {
|
||||
context = required_check.value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
depends_on = [github_repository_file.codeowners]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
terraform {
|
||||
required_version = ">= 1.10"
|
||||
required_providers {
|
||||
github = {
|
||||
source = "integrations/github"
|
||||
version = "6.13.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
mock_provider "github" {}
|
||||
|
||||
variables {
|
||||
owner = "IUCLabs"
|
||||
team = {
|
||||
maintainers = { name = "maintainers", maintainers = ["alice"], members = ["bob"] }
|
||||
}
|
||||
ruleset = {
|
||||
default = { name = "default", rules = { deletion = true, pull_request = {} } }
|
||||
}
|
||||
}
|
||||
|
||||
run "public_repo_with_ruleset" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
".github" = { name = ".github", private = false, rulesets = ["default"], maintainers = ["@maintainers", "carol"], codeowners = ["carol", "dave"] }
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = github_repository_file.codeowners[".github"].content == "* @IUCLabs/maintainers @carol\n"
|
||||
error_message = "CODEOWNERS must list maintainers, teams qualified by owner"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = local.repository_access[".github"] == { "@maintainers" = "maintain", carol = "maintain", dave = "push" }
|
||||
error_message = "maintainers get maintain (winning over codeowners), codeowners get push"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = length(github_repository_ruleset.this) == 1 && length(github_team_membership.this) == 2
|
||||
error_message = "expected one ruleset and two team memberships"
|
||||
}
|
||||
}
|
||||
|
||||
run "private_repo_with_ruleset_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
repository = {
|
||||
secret = { name = "secret", private = true, rulesets = ["default"] }
|
||||
}
|
||||
}
|
||||
|
||||
expect_failures = [var.repository]
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
variable "owner" {
|
||||
description = "GitHub organisation that owns every resource"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "team" {
|
||||
description = "Map of teams to create, keyed by name"
|
||||
type = map(object({
|
||||
name = string
|
||||
description = optional(string, "")
|
||||
privacy = optional(string, "closed")
|
||||
maintainers = optional(list(string), [])
|
||||
members = optional(list(string), [])
|
||||
}))
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "ruleset" {
|
||||
description = "Map of reusable repository ruleset definitions, keyed by name"
|
||||
type = map(object({
|
||||
name = string
|
||||
target = optional(string, "branch")
|
||||
enforcement = optional(string, "active")
|
||||
conditions = optional(object({
|
||||
ref_name = object({
|
||||
include = list(string)
|
||||
exclude = optional(list(string), [])
|
||||
})
|
||||
}))
|
||||
rules = object({
|
||||
deletion = optional(bool, false)
|
||||
non_fast_forward = optional(bool, false)
|
||||
pull_request = optional(object({
|
||||
required_approving_review_count = optional(number, 0)
|
||||
dismiss_stale_reviews_on_push = optional(bool, false)
|
||||
require_code_owner_review = optional(bool, false)
|
||||
require_last_push_approval = optional(bool, false)
|
||||
required_review_thread_resolution = optional(bool, false)
|
||||
}))
|
||||
required_status_checks = optional(object({
|
||||
contexts = list(string)
|
||||
strict = optional(bool, false)
|
||||
}))
|
||||
})
|
||||
}))
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "repository" {
|
||||
description = "Map of repositories to create, keyed by name"
|
||||
type = map(object({
|
||||
name = string
|
||||
description = optional(string, "")
|
||||
private = optional(bool, true)
|
||||
rulesets = optional(list(string), [])
|
||||
maintainers = optional(list(string), [])
|
||||
codeowners = optional(list(string), [])
|
||||
default_branch = optional(string, "main")
|
||||
has_issues = optional(bool, true)
|
||||
has_wiki = optional(bool, false)
|
||||
has_projects = optional(bool, false)
|
||||
allow_squash_merge = optional(bool, true)
|
||||
allow_merge_commit = optional(bool, false)
|
||||
allow_rebase_merge = optional(bool, false)
|
||||
delete_branch_on_merge = optional(bool, true)
|
||||
auto_init = optional(bool, true)
|
||||
}))
|
||||
default = {}
|
||||
|
||||
validation {
|
||||
condition = alltrue([for repo in var.repository : !(repo.private && length(repo.rulesets) > 0)])
|
||||
error_message = "Rulesets are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.rulesets) > 0])}."
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user