implement repositories attribute of gitea_team
This commit is contained in:
parent
e5a6132b9d
commit
b4b13dc3ca
@ -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]
|
||||
}
|
||||
```
|
||||
|
||||
<!-- schema generated by tfplugindocs -->
|
||||
@ -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`
|
||||
|
||||
|
@ -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]
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user