From b4b13dc3ca36c97f92e7a606664cba19783d0d36 Mon Sep 17 00:00:00 2001 From: YAMADA Yutaka Date: Mon, 15 May 2023 03:22:29 +0000 Subject: [PATCH 1/4] implement repositories attribute of gitea_team --- docs/resources/team.md | 21 ++++++ examples/resources/gitea_team/resource.tf | 20 ++++++ gitea/resource_gitea_team.go | 88 ++++++++++++++++++++++- 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/docs/resources/team.md b/docs/resources/team.md index 6434ca7..9d891d2 100644 --- a/docs/resources/team.md +++ b/docs/resources/team.md @@ -34,6 +34,26 @@ resource "gitea_team" "test_team" { permission = "write" members = [gitea_user.test.username] } + + +resource "gitea_repository" "test" { + username = gitea_org.test_org.name + name = "test" + private = true + issue_labels = "Default" + license = "MIT" + gitignores = "Go" +} + +resource "gitea_team" "test_team_restricted" { + name = "Restricted Devs" + organisation = gitea_org.test_org.name + description = "Restricted Devs of Test Org" + permission = "write" + members = [gitea_user.test.username] + include_all_repositories = false + repositories = [gitea_repository.test.name] +} ``` @@ -52,6 +72,7 @@ resource "gitea_team" "test_team" { - `members` (List of String) List of Users that should be part of this team - `permission` (String) Permissions associated with this Team Can be `none`, `read`, `write`, `admin` or `owner` +- `repositories` (List of String) List of Repositories that should be part of this team - `units` (String) List of types of Repositories that should be allowed to be created from Team members. Can be `repo.code`, `repo.issues`, `repo.ext_issues`, `repo.wiki`, `repo.pulls`, `repo.releases`, `repo.projects` and/or `repo.ext_wiki` diff --git a/examples/resources/gitea_team/resource.tf b/examples/resources/gitea_team/resource.tf index 7accce0..256f8ce 100644 --- a/examples/resources/gitea_team/resource.tf +++ b/examples/resources/gitea_team/resource.tf @@ -19,3 +19,23 @@ resource "gitea_team" "test_team" { permission = "write" members = [gitea_user.test.username] } + + +resource "gitea_repository" "test" { + username = gitea_org.test_org.name + name = "test" + private = true + issue_labels = "Default" + license = "MIT" + gitignores = "Go" +} + +resource "gitea_team" "test_team_restricted" { + name = "Restricted Devs" + organisation = gitea_org.test_org.name + description = "Restricted Devs of Test Org" + permission = "write" + members = [gitea_user.test.username] + include_all_repositories = false + repositories = [gitea_repository.test.name] +} diff --git a/gitea/resource_gitea_team.go b/gitea/resource_gitea_team.go index 15d4203..a7abd36 100644 --- a/gitea/resource_gitea_team.go +++ b/gitea/resource_gitea_team.go @@ -18,6 +18,7 @@ const ( TeamIncludeAllReposFlag string = "include_all_repositories" TeamUnits string = "units" TeamMembers string = "members" + TeamRepositories string = "repositories" ) func resourceTeamRead(d *schema.ResourceData, meta interface{}) (err error) { @@ -75,12 +76,14 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) { units = append(units, gitea.RepoUnitProjects) } + includeAllRepos := d.Get(TeamIncludeAllReposFlag).(bool) + opts := gitea.CreateTeamOption{ Name: d.Get(TeamName).(string), Description: d.Get(TeamDescription).(string), Permission: gitea.AccessMode(d.Get(TeamPermissions).(string)), CanCreateOrgRepo: d.Get(TeamCreateRepoFlag).(bool), - IncludesAllRepositories: d.Get(TeamIncludeAllReposFlag).(bool), + IncludesAllRepositories: includeAllRepos, Units: units, } @@ -101,6 +104,13 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) { } } + if !includeAllRepos { + err = setTeamRepositories(team, d, meta, false) + if err != nil { + return + } + } + err = setTeamResourceData(team, d) return @@ -181,6 +191,13 @@ func resourceTeamUpdate(d *schema.ResourceData, meta interface{}) (err error) { } } + if !includeAllRepos { + err = setTeamRepositories(team, d, meta, true) + if err != nil { + return + } + } + team, _, _ = client.GetTeam(id) err = setTeamResourceData(team, d) @@ -218,6 +235,7 @@ func setTeamResourceData(team *gitea.Team, d *schema.ResourceData) (err error) { d.Set(TeamUnits, d.Get(TeamUnits).(string)) d.Set(TeamOrg, d.Get(TeamOrg).(string)) d.Set(TeamMembers, d.Get(TeamMembers)) + d.Set(TeamRepositories, d.Get(TeamRepositories)) return } @@ -290,7 +308,75 @@ func resourceGiteaTeam() *schema.Resource { Computed: true, Description: "List of Users that should be part of this team", }, + "repositories": { + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Required: false, + Computed: true, + Description: "List of Repositories that should be part of this team", + }, }, Description: "`gitea_team` manages Team that are part of an organisation.", } } + +func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interface{}, update bool) (err error) { + client := meta.(*gitea.Client) + + org := d.Get(TeamOrg).(string) + + repositories := make(map[string]bool) + for _, repo := range d.Get(TeamRepositories).([]interface{}) { + if repo != "" { + repositories[repo.(string)] = true + } + } + + if update { + page := 1 + + for { + var existingRepositories []*gitea.Repository + existingRepositories, _, err = client.ListTeamRepositories(team.ID, gitea.ListTeamRepositoriesOptions{ + ListOptions: gitea.ListOptions{ + Page: page, + PageSize: 50, + }, + }) + if err != nil { + return + } + if len(existingRepositories) == 0 { + break + } + + for _, exr := range existingRepositories { + _, exists := repositories[exr.Name] + if exists { + repositories[exr.Name] = false + } else { + _, err = client.RemoveTeamRepository(team.ID, org, exr.Name) + if err != nil { + return + } + } + } + + page += 1 + } + } + + for repo, flag := range repositories { + if flag { + _, err = client.AddTeamRepository(team.ID, org, repo) + if err != nil { + return + } + } + } + + return +} From 9e34636c296753004f2d9f5fec77dd09bfc86255 Mon Sep 17 00:00:00 2001 From: YAMADA Yutaka Date: Tue, 16 May 2023 07:58:50 +0000 Subject: [PATCH 2/4] Bump the version from 0.12.3 to 0.13.0 --- Makefile | 2 +- README.md | 2 +- docs/index.md | 2 +- examples/provider.tf | 2 +- examples/provider/provider.tf | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index a876a51..486f45c 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ KERNEL?=$$(uname -s | tr '[:upper:]' '[:lower:]') GOFMT ?= gofmt -s -VERSION = 0.12.3 +VERSION = 0.13.0 test: fmt-check go test -i $(TEST) || exit 1 diff --git a/README.md b/README.md index 2a5bffe..17d15ca 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.3" + version = "0.13.0" } } } diff --git a/docs/index.md b/docs/index.md index 419f534..4a70051 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.3" + version = "0.13.0" } } } diff --git a/examples/provider.tf b/examples/provider.tf index 1c7dae3..7a3b3e0 100644 --- a/examples/provider.tf +++ b/examples/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "terraform.local/lerentis/gitea" - version = "0.12.3" + version = "0.13.0" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 8362174..fd96593 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.3" + version = "0.13.0" } } } From 322ad2d3c4ed0daefad503b67b46934c6b296f42 Mon Sep 17 00:00:00 2001 From: YAMADA Yutaka Date: Wed, 17 May 2023 00:48:25 +0000 Subject: [PATCH 3/4] add log output on error --- gitea/resource_gitea_team.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gitea/resource_gitea_team.go b/gitea/resource_gitea_team.go index a7abd36..5ee390d 100644 --- a/gitea/resource_gitea_team.go +++ b/gitea/resource_gitea_team.go @@ -2,6 +2,7 @@ package gitea import ( "fmt" + "log" "strconv" "strings" @@ -347,6 +348,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac }, }) if err != nil { + log.Printf("[ERROR] Error listeng team repositories: %s", err) return } if len(existingRepositories) == 0 { @@ -360,6 +362,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac } else { _, err = client.RemoveTeamRepository(team.ID, org, exr.Name) if err != nil { + log.Printf("[ERROR] Error removing team repository %q: %s", exr.Name, err) return } } @@ -373,6 +376,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac if flag { _, err = client.AddTeamRepository(team.ID, org, repo) if err != nil { + log.Printf("[ERROR] Error adding team repository %q: %s", repo, err) return } } From 19062e27f0b57bdb56d4fe716af763dec3501c04 Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Wed, 17 May 2023 09:34:11 +0200 Subject: [PATCH 4/4] transparent errors --- gitea/resource_gitea_team.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/gitea/resource_gitea_team.go b/gitea/resource_gitea_team.go index 5ee390d..dbcb434 100644 --- a/gitea/resource_gitea_team.go +++ b/gitea/resource_gitea_team.go @@ -1,8 +1,8 @@ package gitea import ( + "errors" "fmt" - "log" "strconv" "strings" @@ -108,7 +108,7 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) { if !includeAllRepos { err = setTeamRepositories(team, d, meta, false) if err != nil { - return + return err } } @@ -195,7 +195,7 @@ func resourceTeamUpdate(d *schema.ResourceData, meta interface{}) (err error) { if !includeAllRepos { err = setTeamRepositories(team, d, meta, true) if err != nil { - return + return err } } @@ -348,8 +348,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac }, }) if err != nil { - log.Printf("[ERROR] Error listeng team repositories: %s", err) - return + return errors.New(fmt.Sprintf("[ERROR] Error listeng team repositories: %s", err)) } if len(existingRepositories) == 0 { break @@ -362,8 +361,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac } else { _, err = client.RemoveTeamRepository(team.ID, org, exr.Name) if err != nil { - log.Printf("[ERROR] Error removing team repository %q: %s", exr.Name, err) - return + return errors.New(fmt.Sprintf("[ERROR] Error removing team repository %q: %s", exr.Name, err)) } } } @@ -376,8 +374,7 @@ func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interfac if flag { _, err = client.AddTeamRepository(team.ID, org, repo) if err != nil { - log.Printf("[ERROR] Error adding team repository %q: %s", repo, err) - return + return errors.New(fmt.Sprintf("[ERROR] Error adding team repository %q: %s", repo, err)) } } }