Skip to content

Commit

Permalink
fix: simplify template repository lookup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Arnold committed Apr 4, 2025
1 parent 5215a98 commit 792ad10
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
8 changes: 4 additions & 4 deletions action_secrets.tf
Original file line number Diff line number Diff line change
@@ -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]
}
2 changes: 1 addition & 1 deletion collaborators.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
10 changes: 7 additions & 3 deletions github_files.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion github_team_access.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 12 additions & 21 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 792ad10

Please sign in to comment.