From a0ae631e2bf9bccfbd00d51ca8a1fde863e568d1 Mon Sep 17 00:00:00 2001 From: Dave Arnold Date: Wed, 19 Feb 2025 10:31:27 -0800 Subject: [PATCH] Refactor repository references to use local.github_repo for consistency and improve dependency management in branch protection and GitHub files --- action_secrets.tf | 4 ---- branch_protection.tf | 4 ++-- github_branch.tf | 4 ++-- github_files.tf | 6 +++--- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/action_secrets.tf b/action_secrets.tf index 3a97e43..b0c503c 100644 --- a/action_secrets.tf +++ b/action_secrets.tf @@ -1,7 +1,3 @@ -locals { - repo_exists = var.create_repo ? github_repository.repo[0] : data.github_repository.existing[0] -} - resource "github_actions_secret" "secret" { for_each = tomap({ for secret in var.secrets : secret.name => secret.value }) repository = local.github_repo.name diff --git a/branch_protection.tf b/branch_protection.tf index 402eae3..0a1b55f 100644 --- a/branch_protection.tf +++ b/branch_protection.tf @@ -20,7 +20,7 @@ locals { resource "github_branch_protection" "protection" { for_each = local.branch_protection_rules - repository_id = local.repo_exists.node_id + repository_id = local.github_repo.id pattern = each.value.pattern enforce_admins = each.value.enforce_admins allows_deletions = try(each.value.allows_deletions, false) @@ -48,7 +48,7 @@ resource "github_branch_protection" "protection" { } depends_on = [ - local.repo_exists, + github_repository.repo, github_repository_file.codeowners, github_repository_file.extra_files, github_repository_file.managed_extra_files diff --git a/github_branch.tf b/github_branch.tf index d656636..bb1336b 100644 --- a/github_branch.tf +++ b/github_branch.tf @@ -36,9 +36,9 @@ locals { # https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection resource "github_branch_protection" "main" { - count = var.create_repo && (var.enforce_prs && !var.github_is_private) || var.github_is_private ? 1 : 0 + count = (var.enforce_prs && !var.github_is_private) || var.github_is_private ? 1 : 0 - repository_id = github_repository.repo[0].node_id + repository_id = local.github_repo.id pattern = var.github_default_branch enforce_admins = var.github_enforce_admins_branch_protection allows_deletions = false diff --git a/github_files.tf b/github_files.tf index 6ccc171..aae2247 100644 --- a/github_files.tf +++ b/github_files.tf @@ -15,7 +15,7 @@ resource "github_repository_file" "codeowners" { commit_author = var.commit_author commit_email = var.commit_email overwrite_on_create = true - depends_on = [local.repo_exists] + depends_on = [github_repository.repo] lifecycle { ignore_changes = [ content, @@ -60,7 +60,7 @@ resource "github_repository_file" "extra_files" { commit_author = var.commit_author commit_email = var.commit_email overwrite_on_create = true - depends_on = [local.repo_exists] + depends_on = [github_repository.repo] lifecycle { ignore_changes = [ content, @@ -80,7 +80,7 @@ resource "github_repository_file" "managed_extra_files" { commit_author = var.commit_author commit_email = var.commit_email overwrite_on_create = true - depends_on = [local.repo_exists] + depends_on = [github_repository.repo] lifecycle { ignore_changes = [ branch