From 483bda1033f19b6dd2aba718857602c2534b1b00 Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Wed, 19 Jul 2023 20:52:36 +0200 Subject: [PATCH] added possibility to list all repos of an organisation --- Makefile | 2 +- README.md | 2 +- docs/index.md | 2 +- docs/resources/org.md | 1 + examples/main.tf | 12 ++++++++ examples/provider.tf | 2 +- examples/provider/provider.tf | 2 +- gitea/resource_gitea_organisation.go | 46 +++++++++++++++++++++++++--- 8 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index d772ded..6adfe2a 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ KERNEL?=$$(uname -s | tr '[:upper:]' '[:lower:]') GOFMT ?= gofmt -s -VERSION = 0.14.1 +VERSION = 0.15.0 test: fmt-check go test -i $(TEST) || exit 1 diff --git a/README.md b/README.md index f03e97f..3aaf66f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.14.1" + version = "0.15.0" } } } diff --git a/docs/index.md b/docs/index.md index 429d2dd..fe27d91 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.14.1" + version = "0.15.0" } } } diff --git a/docs/resources/org.md b/docs/resources/org.md index d09f7c0..f646213 100644 --- a/docs/resources/org.md +++ b/docs/resources/org.md @@ -46,5 +46,6 @@ resource "gitea_repository" "org_repo" { - `avatar_url` (String) - `id` (String) The ID of this resource. +- `repos` (List of String) List of all Repositories that are part of this organisation diff --git a/examples/main.tf b/examples/main.tf index 87a0af3..3154f8c 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -123,3 +123,15 @@ output "token" { value = resource.gitea_token.test_token.token sensitive = true } + +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 +} diff --git a/examples/provider.tf b/examples/provider.tf index bb0c738..a392128 100644 --- a/examples/provider.tf +++ b/examples/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "terraform.local/lerentis/gitea" - version = "0.14.1" + version = "0.15.0" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 09e0f43..c0f2a4b 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.14.1" + version = "0.15.0" } } } diff --git a/gitea/resource_gitea_organisation.go b/gitea/resource_gitea_organisation.go index ba1552b..f6e308e 100644 --- a/gitea/resource_gitea_organisation.go +++ b/gitea/resource_gitea_organisation.go @@ -16,6 +16,7 @@ const ( orgLocation string = "location" orgVisibility string = "visibility" RepoAdminChangeTeamAccess string = "repo_admin_change_team_access" + orgRepos string = "org_repos" ) // might come in handy if we want to stick to numeric IDs @@ -48,6 +49,32 @@ func searchOrgByClientId(c *gitea.Client, id int64) (res *gitea.Organization, er } } +func getAllOrgRepos(c *gitea.Client, orgName string) (repos []string, err error) { + page := 1 + + for { + repoBuffer, _, err := c.ListOrgRepos(orgName, gitea.ListOrgReposOptions{ + ListOptions: gitea.ListOptions{ + Page: page, + PageSize: 50, + }, + }) + if err != nil { + return nil, err + } + + if len(repoBuffer) == 0 { + return repos, nil + } + + for _, repo := range repoBuffer { + repos = append(repos, repo.Name) + } + + page += 1 + } +} + func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) { client := meta.(*gitea.Client) @@ -62,7 +89,8 @@ func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) { return nil } - err = setOrgResourceData(org, d) + repos, _ := getAllOrgRepos(client, org.UserName) + err = setOrgResourceData(org, d, &repos) return } @@ -85,7 +113,8 @@ func resourceOrgCreate(d *schema.ResourceData, meta interface{}) (err error) { return } - err = setOrgResourceData(org, d) + repos, _ := getAllOrgRepos(client, org.UserName) + err = setOrgResourceData(org, d, &repos) return } @@ -118,7 +147,8 @@ func resourceOrgUpdate(d *schema.ResourceData, meta interface{}) (err error) { org, resp, err = client.GetOrg(d.Get(orgName).(string)) - err = setOrgResourceData(org, d) + repos, _ := getAllOrgRepos(client, org.UserName) + err = setOrgResourceData(org, d, &repos) return } @@ -141,7 +171,7 @@ func resourceOrgDelete(d *schema.ResourceData, meta interface{}) (err error) { return } -func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData) (err error) { +func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData, repos *[]string) (err error) { d.SetId(fmt.Sprintf("%d", org.ID)) d.Set("name", org.UserName) d.Set("full_name", org.FullName) @@ -150,6 +180,7 @@ func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData) (err er d.Set("website", org.Website) d.Set("location", org.Location) d.Set("visibility", org.Visibility) + d.Set("repos", repos) return } @@ -211,6 +242,13 @@ func resourceGiteaOrg() *schema.Resource { Default: "public", Description: "Flag is this organisation should be publicly visible or not.", }, + "repos": { + Type: schema.TypeList, + Required: false, + Computed: true, + Description: "List of all Repositories that are part of this organisation", + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, Description: "`gitea_org` manages a gitea organisation.\n\n" + "Organisations are a way to group repositories and abstract permission management in a gitea instance.",