split github_org into team, repository and ruleset submodules
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
locals {
|
||||
access = merge(
|
||||
{ for e in var.config.codeowners : e => "push" },
|
||||
{ for e in var.config.maintainers : e => "maintain" },
|
||||
)
|
||||
|
||||
default_branch_rules = {
|
||||
target = "branch"
|
||||
enforcement = "active"
|
||||
conditions = { ref_name = { include = ["~DEFAULT_BRANCH"], exclude = [] } }
|
||||
}
|
||||
|
||||
generated_rulesets = { for k, v in {
|
||||
branch-protection = {
|
||||
enabled = coalesce(var.config.branch_protection, !var.config.private)
|
||||
ruleset = merge(local.default_branch_rules, {
|
||||
name = "branch-protection"
|
||||
rules = {
|
||||
deletion = true
|
||||
non_fast_forward = true
|
||||
pull_request = {
|
||||
required_approving_review_count = 0
|
||||
dismiss_stale_reviews_on_push = true
|
||||
require_code_owner_review = false
|
||||
require_last_push_approval = false
|
||||
required_review_thread_resolution = false
|
||||
}
|
||||
required_status_checks = null
|
||||
}
|
||||
})
|
||||
}
|
||||
required-checks = {
|
||||
enabled = length(var.config.required_checks) > 0
|
||||
ruleset = merge(local.default_branch_rules, {
|
||||
name = "required-checks"
|
||||
rules = {
|
||||
deletion = false
|
||||
non_fast_forward = false
|
||||
pull_request = null
|
||||
required_status_checks = { contexts = var.config.required_checks, strict = true }
|
||||
}
|
||||
})
|
||||
}
|
||||
tag-protection = {
|
||||
enabled = coalesce(var.config.tag_protection, !var.config.private)
|
||||
ruleset = {
|
||||
name = "tag-protection"
|
||||
target = "tag"
|
||||
enforcement = "active"
|
||||
conditions = { ref_name = { include = ["refs/tags/v*"], exclude = [] } }
|
||||
rules = {
|
||||
deletion = true
|
||||
non_fast_forward = true
|
||||
pull_request = null
|
||||
required_status_checks = null
|
||||
}
|
||||
}
|
||||
}
|
||||
} : k => v.ruleset if v.enabled }
|
||||
}
|
||||
|
||||
resource "github_repository" "this" {
|
||||
name = var.config.name
|
||||
description = var.config.description
|
||||
visibility = var.config.private ? "private" : "public"
|
||||
has_issues = true
|
||||
has_wiki = false
|
||||
has_projects = false
|
||||
allow_squash_merge = var.config.merge_type == "squash"
|
||||
allow_merge_commit = var.config.merge_type == "merge"
|
||||
allow_rebase_merge = var.config.merge_type == "rebase"
|
||||
delete_branch_on_merge = var.config.delete_branch_on_merge
|
||||
auto_init = true
|
||||
archive_on_destroy = true
|
||||
}
|
||||
|
||||
resource "github_branch_default" "this" {
|
||||
repository = github_repository.this.name
|
||||
branch = var.config.default_branch
|
||||
# auto_init only creates main; rename it instead of pointing at a missing branch
|
||||
rename = true
|
||||
wait_for_rename = true
|
||||
}
|
||||
|
||||
resource "github_repository_collaborators" "this" {
|
||||
count = length(local.access) > 0 ? 1 : 0
|
||||
|
||||
repository = github_repository.this.name
|
||||
|
||||
dynamic "user" {
|
||||
for_each = { for e, p in local.access : e => p if !startswith(e, "@") }
|
||||
content {
|
||||
username = user.key
|
||||
permission = user.value
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "team" {
|
||||
for_each = { for e, p in local.access : trimprefix(e, "@") => p if startswith(e, "@") }
|
||||
content {
|
||||
team_id = var.team_slugs[team.key]
|
||||
permission = team.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "github_repository_file" "codeowners" {
|
||||
count = length(var.config.maintainers) > 0 ? 1 : 0
|
||||
|
||||
repository = github_repository.this.name
|
||||
branch = github_branch_default.this.branch
|
||||
file = ".github/CODEOWNERS"
|
||||
content = "* ${join(" ", [for e in var.config.maintainers : startswith(e, "@") ? "@${var.owner}/${var.team_slugs[trimprefix(e, "@")]}" : "@${e}"])}\n"
|
||||
commit_message = "add initial CODEOWNERS"
|
||||
overwrite_on_create = false
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [content]
|
||||
}
|
||||
}
|
||||
|
||||
module "ruleset" {
|
||||
source = "../ruleset"
|
||||
|
||||
for_each = merge(var.rulesets, local.generated_rulesets)
|
||||
|
||||
repository = github_repository.this.name
|
||||
ruleset = each.value
|
||||
|
||||
depends_on = [github_repository_file.codeowners]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
output "repository" {
|
||||
value = github_repository.this
|
||||
}
|
||||
|
||||
output "branch_default" {
|
||||
value = github_branch_default.this
|
||||
}
|
||||
|
||||
output "codeowners" {
|
||||
value = one(github_repository_file.codeowners)
|
||||
}
|
||||
|
||||
output "access" {
|
||||
value = local.access
|
||||
}
|
||||
|
||||
output "rulesets" {
|
||||
value = { for k, m in module.ruleset : k => m.ruleset }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
terraform {
|
||||
required_version = ">= 1.10"
|
||||
required_providers {
|
||||
github = {
|
||||
source = "integrations/github"
|
||||
version = "6.13.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
variable "owner" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "config" {
|
||||
type = object({
|
||||
name = string
|
||||
description = string
|
||||
private = bool
|
||||
default_branch = string
|
||||
maintainers = list(string)
|
||||
codeowners = list(string)
|
||||
merge_type = string
|
||||
delete_branch_on_merge = bool
|
||||
branch_protection = bool
|
||||
tag_protection = bool
|
||||
required_checks = list(string)
|
||||
})
|
||||
}
|
||||
|
||||
variable "rulesets" {
|
||||
description = "Custom ruleset definitions this repository references, keyed by name"
|
||||
type = any
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "team_slugs" {
|
||||
description = "Slugs of the teams this repository references, keyed by team name"
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
resource "github_repository_ruleset" "this" {
|
||||
name = var.ruleset.name
|
||||
repository = var.repository
|
||||
target = var.ruleset.target
|
||||
enforcement = var.ruleset.enforcement
|
||||
|
||||
dynamic "conditions" {
|
||||
for_each = var.ruleset.conditions[*]
|
||||
content {
|
||||
ref_name {
|
||||
include = conditions.value.ref_name.include
|
||||
exclude = conditions.value.ref_name.exclude
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rules {
|
||||
deletion = var.ruleset.rules.deletion
|
||||
non_fast_forward = var.ruleset.rules.non_fast_forward
|
||||
|
||||
dynamic "pull_request" {
|
||||
for_each = var.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 = var.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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
output "ruleset" {
|
||||
value = github_repository_ruleset.this
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
terraform {
|
||||
required_version = ">= 1.10"
|
||||
required_providers {
|
||||
github = {
|
||||
source = "integrations/github"
|
||||
version = "6.13.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
variable "repository" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "ruleset" {
|
||||
type = object({
|
||||
name = string
|
||||
target = string
|
||||
enforcement = string
|
||||
conditions = object({
|
||||
ref_name = object({
|
||||
include = list(string)
|
||||
exclude = list(string)
|
||||
})
|
||||
})
|
||||
rules = object({
|
||||
deletion = bool
|
||||
non_fast_forward = bool
|
||||
pull_request = object({
|
||||
required_approving_review_count = number
|
||||
dismiss_stale_reviews_on_push = bool
|
||||
require_code_owner_review = bool
|
||||
require_last_push_approval = bool
|
||||
required_review_thread_resolution = bool
|
||||
})
|
||||
required_status_checks = object({
|
||||
contexts = list(string)
|
||||
strict = bool
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
resource "github_team" "this" {
|
||||
name = var.name
|
||||
description = var.description
|
||||
privacy = var.privacy
|
||||
}
|
||||
|
||||
resource "github_team_membership" "this" {
|
||||
for_each = merge(
|
||||
{ for u in var.members : u => "member" },
|
||||
{ for u in var.maintainers : u => "maintainer" },
|
||||
)
|
||||
|
||||
team_id = github_team.this.id
|
||||
username = each.key
|
||||
role = each.value
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
output "id" {
|
||||
value = github_team.this.id
|
||||
}
|
||||
|
||||
output "slug" {
|
||||
value = github_team.this.slug
|
||||
}
|
||||
|
||||
output "membership" {
|
||||
value = github_team_membership.this
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
terraform {
|
||||
required_version = ">= 1.10"
|
||||
required_providers {
|
||||
github = {
|
||||
source = "integrations/github"
|
||||
version = "6.13.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
variable "name" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "description" {
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "privacy" {
|
||||
type = string
|
||||
default = "closed"
|
||||
}
|
||||
|
||||
variable "maintainers" {
|
||||
type = list(string)
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "members" {
|
||||
type = list(string)
|
||||
default = []
|
||||
}
|
||||
Reference in New Issue
Block a user