Compare commits

..

1 Commits

Author SHA1 Message Date
Tobias Trabelsi
9d12fb5fc6 potential fix for #35
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-15 15:17:32 +02:00
8 changed files with 11 additions and 144 deletions

View File

@ -5,7 +5,7 @@ KERNEL?=$$(uname -s | tr '[:upper:]' '[:lower:]')
GOFMT ?= gofmt -s GOFMT ?= gofmt -s
VERSION = 0.13.0 VERSION = 0.12.3
test: fmt-check test: fmt-check
go test -i $(TEST) || exit 1 go test -i $(TEST) || exit 1

View File

@ -17,7 +17,7 @@ terraform {
required_providers { required_providers {
gitea = { gitea = {
source = "Lerentis/gitea" source = "Lerentis/gitea"
version = "0.13.0" version = "0.12.3"
} }
} }
} }

View File

@ -17,7 +17,7 @@ terraform {
required_providers { required_providers {
gitea = { gitea = {
source = "Lerentis/gitea" source = "Lerentis/gitea"
version = "0.13.0" version = "0.12.3"
} }
} }
} }

View File

@ -34,26 +34,6 @@ resource "gitea_team" "test_team" {
permission = "write" permission = "write"
members = [gitea_user.test.username] 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]
}
``` ```
<!-- schema generated by tfplugindocs --> <!-- schema generated by tfplugindocs -->
@ -72,7 +52,6 @@ resource "gitea_team" "test_team_restricted" {
- `members` (List of String) List of Users that should be part of this team - `members` (List of String) List of Users that should be part of this team
- `permission` (String) Permissions associated with this Team - `permission` (String) Permissions associated with this Team
Can be `none`, `read`, `write`, `admin` or `owner` 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. - `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` Can be `repo.code`, `repo.issues`, `repo.ext_issues`, `repo.wiki`, `repo.pulls`, `repo.releases`, `repo.projects` and/or `repo.ext_wiki`

View File

@ -2,7 +2,7 @@ terraform {
required_providers { required_providers {
gitea = { gitea = {
source = "terraform.local/lerentis/gitea" source = "terraform.local/lerentis/gitea"
version = "0.13.0" version = "0.12.3"
} }
} }
} }

View File

@ -2,7 +2,7 @@ terraform {
required_providers { required_providers {
gitea = { gitea = {
source = "Lerentis/gitea" source = "Lerentis/gitea"
version = "0.13.0" version = "0.12.3"
} }
} }
} }

View File

@ -19,23 +19,3 @@ resource "gitea_team" "test_team" {
permission = "write" permission = "write"
members = [gitea_user.test.username] 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]
}

View File

@ -1,7 +1,6 @@
package gitea package gitea
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -19,7 +18,6 @@ const (
TeamIncludeAllReposFlag string = "include_all_repositories" TeamIncludeAllReposFlag string = "include_all_repositories"
TeamUnits string = "units" TeamUnits string = "units"
TeamMembers string = "members" TeamMembers string = "members"
TeamRepositories string = "repositories"
) )
func resourceTeamRead(d *schema.ResourceData, meta interface{}) (err error) { func resourceTeamRead(d *schema.ResourceData, meta interface{}) (err error) {
@ -41,7 +39,7 @@ func resourceTeamRead(d *schema.ResourceData, meta interface{}) (err error) {
} }
} }
err = setTeamResourceData(team, d, meta) err = setTeamResourceData(team, d)
return return
} }
@ -77,14 +75,12 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) {
units = append(units, gitea.RepoUnitProjects) units = append(units, gitea.RepoUnitProjects)
} }
includeAllRepos := d.Get(TeamIncludeAllReposFlag).(bool)
opts := gitea.CreateTeamOption{ opts := gitea.CreateTeamOption{
Name: d.Get(TeamName).(string), Name: d.Get(TeamName).(string),
Description: d.Get(TeamDescription).(string), Description: d.Get(TeamDescription).(string),
Permission: gitea.AccessMode(d.Get(TeamPermissions).(string)), Permission: gitea.AccessMode(d.Get(TeamPermissions).(string)),
CanCreateOrgRepo: d.Get(TeamCreateRepoFlag).(bool), CanCreateOrgRepo: d.Get(TeamCreateRepoFlag).(bool),
IncludesAllRepositories: includeAllRepos, IncludesAllRepositories: d.Get(TeamIncludeAllReposFlag).(bool),
Units: units, Units: units,
} }
@ -105,14 +101,7 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) {
} }
} }
if !includeAllRepos { err = setTeamResourceData(team, d)
err = setTeamRepositories(team, d, meta, false)
if err != nil {
return err
}
}
err = setTeamResourceData(team, d, meta)
return return
} }
@ -192,16 +181,9 @@ func resourceTeamUpdate(d *schema.ResourceData, meta interface{}) (err error) {
} }
} }
if !includeAllRepos {
err = setTeamRepositories(team, d, meta, true)
if err != nil {
return err
}
}
team, _, _ = client.GetTeam(id) team, _, _ = client.GetTeam(id)
err = setTeamResourceData(team, d, meta) err = setTeamResourceData(team, d)
return return
} }
@ -226,13 +208,7 @@ func resourceTeamDelete(d *schema.ResourceData, meta interface{}) (err error) {
return return
} }
func setTeamResourceData(team *gitea.Team, d *schema.ResourceData, meta interface{}) (err error) { func setTeamResourceData(team *gitea.Team, d *schema.ResourceData) (err error) {
client := meta.(*gitea.Client)
if err := client.CheckServerVersionConstraint(">= 1.19.4"); err != nil {
d.Set(TeamOrg, d.Get(TeamOrg).(string))
} else {
d.Set(TeamOrg, team.Organization.UserName)
}
d.SetId(fmt.Sprintf("%d", team.ID)) d.SetId(fmt.Sprintf("%d", team.ID))
d.Set(TeamCreateRepoFlag, team.CanCreateOrgRepo) d.Set(TeamCreateRepoFlag, team.CanCreateOrgRepo)
d.Set(TeamDescription, team.Description) d.Set(TeamDescription, team.Description)
@ -240,8 +216,8 @@ func setTeamResourceData(team *gitea.Team, d *schema.ResourceData, meta interfac
d.Set(TeamPermissions, string(team.Permission)) d.Set(TeamPermissions, string(team.Permission))
d.Set(TeamIncludeAllReposFlag, team.IncludesAllRepositories) d.Set(TeamIncludeAllReposFlag, team.IncludesAllRepositories)
d.Set(TeamUnits, d.Get(TeamUnits).(string)) d.Set(TeamUnits, d.Get(TeamUnits).(string))
d.Set(TeamOrg, team.Organization.UserName)
d.Set(TeamMembers, d.Get(TeamMembers)) d.Set(TeamMembers, d.Get(TeamMembers))
d.Set(TeamRepositories, d.Get(TeamRepositories))
return return
} }
@ -314,75 +290,7 @@ func resourceGiteaTeam() *schema.Resource {
Computed: true, Computed: true,
Description: "List of Users that should be part of this team", 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.", 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 errors.New(fmt.Sprintf("[ERROR] Error listeng team repositories: %s", err))
}
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 errors.New(fmt.Sprintf("[ERROR] Error removing team repository %q: %s", exr.Name, err))
}
}
}
page += 1
}
}
for repo, flag := range repositories {
if flag {
_, err = client.AddTeamRepository(team.ID, org, repo)
if err != nil {
return errors.New(fmt.Sprintf("[ERROR] Error adding team repository %q: %s", repo, err))
}
}
}
return
}