diff --git a/action_secrets.tf b/action_secrets.tf index b0c503c..9da352e 100644 --- a/action_secrets.tf +++ b/action_secrets.tf @@ -1,15 +1,15 @@ resource "github_actions_secret" "secret" { for_each = tomap({ for secret in var.secrets : secret.name => secret.value }) - repository = local.github_repo.name + repository = var.create_repo ? github_repository.repo[0].name : data.github_repository.existing[0].name secret_name = each.key plaintext_value = each.value - depends_on = [local.github_repo] + depends_on = [github_repository.repo, data.github_repository.existing] } resource "github_actions_variable" "variable" { for_each = tomap({ for _var in var.vars : _var.name => _var.value }) - repository = local.github_repo.name + repository = var.create_repo ? github_repository.repo[0].name : data.github_repository.existing[0].name variable_name = each.key value = each.value - depends_on = [local.github_repo] + depends_on = [github_repository.repo, data.github_repository.existing] } diff --git a/collaborators.tf b/collaborators.tf index ae4a1f2..dfd6720 100644 --- a/collaborators.tf +++ b/collaborators.tf @@ -17,7 +17,7 @@ data "github_user" "collaborators" { # Add a collaborator to a repository resource "github_repository_collaborator" "collaborators" { for_each = tomap(var.collaborators) - repository = local.github_repo.name + repository = var.create_repo ? github_repository.repo[0].name : data.github_repository.existing[0].name username = each.key permission = local.permission_map[each.value] diff --git a/github_files.tf b/github_files.tf index dd4f401..fedea52 100644 --- a/github_files.tf +++ b/github_files.tf @@ -26,13 +26,17 @@ resource "github_repository_file" "codeowners" { } } +locals { + lookup_sha = var.template_repo == null ? 0 : var.template_repo_org == var.repo_org ? 1 : 0 +} + data "github_repository" "template_repo" { - count = var.template_repo == null && var.template_repo_org == var.repo_org ? 0 : 1 + count = local.lookup_sha full_name = "${var.template_repo_org == null ? "" : var.template_repo_org}/${var.template_repo == null ? "" : var.template_repo}" } data "github_ref" "ref" { - count = var.template_repo == null && var.template_repo_org == var.repo_org ? 0 : 1 + count = local.lookup_sha owner = var.template_repo_org repository = var.template_repo ref = "heads/${element(data.github_repository.template_repo, 0).default_branch}" @@ -41,7 +45,7 @@ data "github_ref" "ref" { locals { extra_files = concat( var.extra_files, - var.template_repo == null && var.template_repo_org == var.repo_org ? [] : [ + local.lookup_sha == 1 ? [] : [ { path = ".TEMPLATE_SHA", content = data.github_ref.ref[0].sha diff --git a/github_team_access.tf b/github_team_access.tf index 21e14bc..9f9ab9a 100644 --- a/github_team_access.tf +++ b/github_team_access.tf @@ -24,7 +24,7 @@ locals { resource "github_team_repository" "admin" { for_each = { for team in var.admin_teams : team => data.github_team.admin_teams[team].id } team_id = each.value - repository = local.github_repo.name + repository = var.create_repo ? github_repository.repo[0].name : data.github_repository.existing[0].name permission = "admin" lifecycle { diff --git a/outputs.tf b/outputs.tf index 78c4f3d..7c11c8c 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,68 +1,59 @@ output "github_repo" { description = "All attributes of the GitHub repository" - value = local.github_repo + value = var.create_repo ? github_repository.repo[0] : data.github_repository.existing[0] } output "ssh_clone_url" { description = "URL that can be provided to git clone to clone the repository via SSH" - value = local.github_repo.ssh_clone_url + value = var.create_repo ? github_repository.repo[0].ssh_clone_url : data.github_repository.existing[0].ssh_clone_url } output "node_id" { description = "Node ID of the repository, used for GraphQL API access" - value = local.github_repo.node_id + value = var.create_repo ? github_repository.repo[0].node_id : data.github_repository.existing[0].node_id } output "full_name" { description = "Full name of the repository in org/repo format" - value = local.github_repo.full_name + value = var.create_repo ? github_repository.repo[0].full_name : data.github_repository.existing[0].full_name } output "repo_id" { description = "Repository ID" - value = local.github_repo.repo_id + value = var.create_repo ? github_repository.repo[0].repo_id : data.github_repository.existing[0].repo_id } output "html_url" { description = "URL to the repository on GitHub" - value = local.github_repo.html_url + value = var.create_repo ? github_repository.repo[0].html_url : data.github_repository.existing[0].html_url } output "http_clone_url" { description = "URL that can be provided to git clone to clone the repository via HTTPS" - value = local.github_repo.http_clone_url + value = var.create_repo ? github_repository.repo[0].http_clone_url : data.github_repository.existing[0].http_clone_url } output "git_clone_url" { description = "URL that can be provided to git clone to clone the repository anonymously via the git protocol" - value = local.github_repo.git_clone_url + value = var.create_repo ? github_repository.repo[0].git_clone_url : data.github_repository.existing[0].git_clone_url } output "visibility" { description = "Whether the repository is private or public" - value = local.github_repo.visibility + value = var.create_repo ? github_repository.repo[0].visibility : data.github_repository.existing[0].visibility } output "default_branch" { description = "Default branch of the repository" - value = local.github_repo.default_branch + value = var.create_repo ? github_repository.repo[0].default_branch : data.github_repository.existing[0].default_branch } output "topics" { description = "List of topics applied to the repository" - value = local.github_repo.topics + value = var.create_repo ? github_repository.repo[0].topics : data.github_repository.existing[0].topics } output "template" { description = "Template repository this repository was created from" - value = local.github_repo.template + value = var.create_repo ? lookup(github_repository.repo[0], "template", null) : lookup(data.github_repository.existing[0], "template", null) } - - -output "generated_deploy_keys" { - description = "Generated private keys for deploy keys with create=true" - value = { - for k, v in tls_private_key.deploy_key : k => v.private_key_pem - } - sensitive = true -} \ No newline at end of file