Lerentis / terraform-provider-gitea

Terraform Provider for your Gitea Instance
https://registry.terraform.io/providers/Lerentis/gitea/latest
MIT License
22 stars 11 forks source link

List repositories and clone urls that are associated with an org #47

Closed PatDyn closed 1 year ago

PatDyn commented 1 year ago

Is your feature request related to a problem? Please describe. Automated listing of repositories that are owned by a specified organization - along with their clone urls. Simillar to what the gitlab terraform provider allows: https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs/data-sources/projects

Describe alternatives you've considered With the current provider its not possible to do this.

Additional context It would be helpful for mirroring lots of repos from one instance of gitea/forgejo to another.

Lerentis commented 1 year ago

Hi @PatDyn , i think this is possible to implement using the gitea sdk. i'll try to come up with something to get this done. Thanks for raising this :smiley:

Lerentis commented 1 year ago

Release with this feature will be there shortly. with that you can do something like

resource "gitea_org" "org1" {
  name = "org1"
}

resource "gitea_repository" "repo1_in_org1" {
  username = gitea_org.org1.name
  name     = "repo1-in-org1"
}

data "gitea_repo" "org_repos" {
  name = each.key
  username = gitea_org.org1.name
  for_each = {
    for repo in resource.gitea_org.org1.repos : repo => repo
  }
}

output "repos" {
  value = data.gitea_repo.org_repos["repo1-in-org1"].clone_url
}

of cause you can iterate over it :smiley:

PatDyn commented 1 year ago

This looks great, thanks for looking into it!

PatDyn commented 1 year ago

So, i tried this just now: It seems, that i have to create a gitea_org first to access the repos it owns? My original thought was accessing the data of an existing org e.g. reading from a datasource. Sorry for not being clear about that. Maybe something like that would be feasible?

data "gitea_org" "org1" {}

data "gitea_repo" "org_repos" { name = each.key username = gitea_org.org1.name for_each = { for repo in data.gitea_org.org1.repos : repo => repo } }

(Notice, we are accessing the data source here)

Lerentis commented 1 year ago

You can just pass the name of the organization as a string without the need to have it managed by terraform. No need to create another data resource

PatDyn commented 1 year ago

I'm a little confused as to where to pass that string. The for expression seems to be the wrong place. This is what i tried (and failed).

data "gitea_repo" "all_repos" { name = each.key username = "some-org" for_each = { for repo in "some-org": repo => repo }
}

Which yields the following error:

│ Error: Iteration over non-iterable value
│ 
│   on gitea_datasources.tf line 5, in data "gitea_repo" "all_repos":
│    4:     for_each = {
│    5:         for repo in "some-org" : repo => repo
│    6:     }       
│ 
│ A value of type string cannot be used as the collection in a 'for' expression.