Skip to content

Commit

Permalink
Refactor repository references to use local variable for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Arnold committed Feb 19, 2025
1 parent 09cc896 commit 30851e4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions action_secrets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ resource "github_actions_secret" "secret" {
for_each = tomap({ for secret in var.secrets : secret.name => secret.value })
secret_name = each.key
plaintext_value = each.value
repository = github_repository.repo.name
repository = local.github_repo.name
}

resource "github_actions_variable" "variable" {
for_each = tomap({ for _var in var.vars : _var.name => _var.value })
repository = github_repository.repo.name
repository = local.github_repo.name
variable_name = each.key
value = each.value
}
5 changes: 1 addition & 4 deletions collaborators.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Add a collaborator to a repository
resource "github_repository_collaborator" "collaborators" {
for_each = tomap(var.collaborators)
repository = github_repository.repo.name
repository = local.github_repo.name
username = each.key
permission = each.value
depends_on = [
github_repository.repo
]
}
7 changes: 3 additions & 4 deletions github_branch.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/team
# data "github_team" "github_codeowners_team" {
# slug = var.github_codeowners_team
Expand All @@ -7,15 +6,15 @@
# not creating main branch because its created by default when repo is created
resource "github_branch" "branch" {
count = var.github_default_branch == "main" ? 0 : 1
repository = github_repository.repo.name
repository = local.github_repo.name
branch = var.github_default_branch
}


# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default
resource "github_branch_default" "default_main_branch" {
count = var.github_default_branch == "main" ? 0 : 1
repository = github_repository.repo.name
repository = local.github_repo.name
branch = var.github_default_branch
depends_on = [
github_branch.branch
Expand All @@ -38,7 +37,7 @@ resource "github_branch_protection" "main" {
enforce_admins = var.github_enforce_admins_branch_protection
pattern = var.github_default_branch
# push_restrictions = var.github_push_restrictions
repository_id = github_repository.repo.node_id
repository_id = local.github_repo.node_id
required_pull_request_reviews {
dismiss_stale_reviews = var.github_dismiss_stale_reviews
require_code_owner_reviews = var.github_require_code_owner_reviews
Expand Down
6 changes: 3 additions & 3 deletions github_files.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file
resource "github_repository_file" "codeowners" {
count = var.create_codeowners ? 1 : 0
repository = github_repository.repo.name
repository = local.github_repo.name
branch = var.github_default_branch
file = "CODEOWNERS"
content = templatefile("${path.module}/templates/CODEOWNERS", { codeowners = local.codeowners })
Expand Down Expand Up @@ -41,7 +41,7 @@ locals {

resource "github_repository_file" "extra_files" {
for_each = tomap({ for file in local.extra_files : "${element(split("/", file.path), length(split("/", file.path)) - 1)}" => file })
repository = github_repository.repo.name
repository = local.github_repo.name
branch = var.github_default_branch
file = each.value.path
content = each.value.content
Expand All @@ -56,7 +56,7 @@ resource "github_repository_file" "extra_files" {

resource "github_repository_file" "managed_extra_files" {
for_each = tomap({ for file in var.managed_extra_files : "${element(split("/", file.path), length(split("/", file.path)) - 1)}" => file })
repository = github_repository.repo.name
repository = local.github_repo.name
branch = var.github_default_branch
file = each.value.path
content = each.value.content
Expand Down
12 changes: 9 additions & 3 deletions github_repo.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
locals {
repo_name = var.force_name ? var.name : "${var.name}-${formatdate("YYYYMMDD", timestamp())}"
github_repo = var.create_repo ? github_repository.repo[0] : data.github_repository.existing[0]
}
resource "github_repository" "repo" {
count = var.create_repo ? 1 : 0
name = local.repo_name
description = var.github_repo_description
visibility = var.github_is_private ? "private" : "public"
Expand All @@ -24,12 +26,16 @@ resource "github_repository" "repo" {
vulnerability_alerts = var.vulnerability_alerts

dynamic "template" {
# A bogus map for a conditional block
for_each = var.template_repo == null ? [] : ["*"]
content {
owner = var.template_repo_org
repository = var.template_repo
# include_all_branches = var.template_include_all_branches
}
}
}

data "github_repository" "existing" {
count = var.create_repo ? 0 : 1
name = local.repo_name
full_name = var.repo_org != null ? "${var.repo_org}/${local.repo_name}" : local.repo_name
}
10 changes: 1 addition & 9 deletions github_team_access.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ locals {
github_teams = { for obj in local.github_org_teams : "${obj.slug}" => obj.id }
}

# data "github_team" "nit_admin" {
# slug = "nit"
# }

# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository
resource "github_team_repository" "admin" {
for_each = toset(var.admin_teams)
team_id = lookup(local.github_teams, each.value)
repository = github_repository.repo.name
repository = local.github_repo.name
permission = "admin"
lifecycle {
ignore_changes = [
team_id
]
}
depends_on = [
github_repository.repo
]
}
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
output "github_repo" {
value = github_repository.repo
value = local.github_repo
}

output "ssh_clone_url" {
description = "URL that can be provided to git clone to clone the repository via SSH"
value = github_repository.repo.ssh_clone_url
value = local.github_repo.ssh_clone_url
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ variable "homepage_url" {
default = null
}

variable "create_repo" {
description = "Whether to create a new repository or lookup an existing one"
type = bool
default = true
}

variable "security_and_analysis" {
description = <<EOT
Security and Analysis Configuration
Expand Down

0 comments on commit 30851e4

Please sign in to comment.