terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

[FEAT]: data github_repositories should return better structure

Open uthark opened this issue 1 year ago • 1 comments

Describe the need

Currently it returns 3 individual list with full_names, names and repo_ids. I need to map repo name to the repo id, but I'm not sure if the returned data is consistently ordered.

Ideally, return a list of maps and each map would have repo information to use.

The use-case: I need to configure access to org level secret and resource github_actions_organization_secret_repositories requires to pass repo ids, but from the configuaration perspective we want to pass repo names.

So we need to resolve repo id from its' name. To reduce the load on gh api we'd like to use data.github_repositories, but resulting data is hard to use / docs don't make guarantee that returned list match.

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

uthark avatar Jun 27 '24 01:06 uthark

Hey! I had the same problem and decided to test if the returned lists are actually aligned:

# main.tf

data "github_repositories" "all" {
  query           = "org:Foo archived:false"
  include_repo_id = true
}

output "repos_map" {
  value = zipmap(
    data.github_repositories.all.full_names, 
    data.github_repositories.all.repo_ids
  )
}

data "github_repository" "control" {
  for_each = toset(data.github_repositories.all.full_names)

  full_name = each.value
}

and

# repositories-data.tftest.hcl

run "valid_repo_ids" {
  command = plan

  assert {
    condition = alltrue(
      [for name, id in output.repos_map : data.github_repository.control[name].repo_id == id]
    )
    error_message = "Some IDs don't match"
  }
}

then run the tests:

$ terraform test
repositories-data.tftest.hcl... in progress
  run "valid_repo_ids"... pass
repositories-data.tftest.hcl... tearing down
repositories-data.tftest.hcl... pass

Success! 1 passed, 0 failed.

(plus I did some manual checking)

So it seems that doing zipmap on the two returned lists should be okay. It would be better, of course, to have that map in the returned attribute, but I hope this helps at least as a workaround.

laughedelic avatar Jul 23 '24 11:07 laughedelic

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

github-actions[bot] avatar Apr 20 '25 02:04 github-actions[bot]