From 896c84a71c0e4478b8fcbdacf5657d01a74c5826 Mon Sep 17 00:00:00 2001 From: David Arnold <10138997+djaboxx@users.noreply.github.com> Date: Sat, 12 Apr 2025 12:27:44 -0700 Subject: [PATCH] Error catch (#27) * fix: update extra_files logic to handle empty ref cases and improve repository access safety * fix: update default_branch output to use variable for new repositories * fix: update ref logic to handle empty template repository cases and improve default branch output --------- Co-authored-by: Dave Arnold --- github_files.tf | 2 +- outputs.tf | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/github_files.tf b/github_files.tf index 37aacbf..d9abe44 100644 --- a/github_files.tf +++ b/github_files.tf @@ -39,7 +39,7 @@ data "github_ref" "ref" { count = local.lookup_sha owner = var.template_repo_org repository = var.template_repo - ref = "heads/${element(data.github_repository.template_repo, 0).default_branch}" + ref = "heads/${local.lookup_sha > 0 && length(data.github_repository.template_repo) > 0 ? coalesce(lookup(data.github_repository.template_repo[0], "default_branch", "main"), "main") : "main"}" } locals { diff --git a/outputs.tf b/outputs.tf index 2b04b55..d3ce572 100644 --- a/outputs.tf +++ b/outputs.tf @@ -45,7 +45,9 @@ output "visibility" { output "default_branch" { description = "Default branch of the repository" - value = var.create_repo ? var.github_default_branch : data.github_repository.existing[0].default_branch + value = var.create_repo ? var.github_default_branch : ( + length(data.github_repository.existing) > 0 ? lookup(data.github_repository.existing[0], "default_branch", "main") : "main" + ) } # resource "github_branch_default" "default_main_branch" {