Add Repo Team Management Functions (#537)
close #526 * [x] functions * [x] tests Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/537 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
63e97e127c
commit
b81847d03d
4 changed files with 213 additions and 0 deletions
25
gitea/org_team_test.go
Normal file
25
gitea/org_team_test.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func createTestOrgTeams(t *testing.T, c *Client, org, name string, accessMode AccessMode, units []RepoUnitType) (*Team, error) {
|
||||
team, _, e := c.CreateTeam(org, CreateTeamOption{
|
||||
Name: name,
|
||||
Description: name + "'s team desc",
|
||||
Permission: accessMode,
|
||||
CanCreateOrgRepo: false,
|
||||
IncludesAllRepositories: false,
|
||||
Units: units,
|
||||
})
|
||||
assert.NoError(t, e)
|
||||
assert.NotNil(t, team)
|
||||
return team, e
|
||||
}
|
49
gitea/org_test.go
Normal file
49
gitea/org_test.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func createTestOrgRepo(t *testing.T, c *Client, name string) (func(), *Repository, error) {
|
||||
_, _, err := c.GetOrg(name)
|
||||
if err == nil {
|
||||
_, _ = c.DeleteOrg(name)
|
||||
}
|
||||
_, _, err = c.CreateOrg(CreateOrgOption{
|
||||
Name: name,
|
||||
Visibility: VisibleTypePublic,
|
||||
RepoAdminChangeTeamAccess: true,
|
||||
})
|
||||
if !assert.NoError(t, err) {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
_, _, err = c.GetRepo(name, name)
|
||||
if err == nil {
|
||||
_, _ = c.DeleteRepo(name, name)
|
||||
}
|
||||
|
||||
repo, _, err := c.CreateOrgRepo(name, CreateRepoOption{
|
||||
Name: name,
|
||||
Description: "A test Repo: " + name,
|
||||
AutoInit: true,
|
||||
Gitignores: "C,C++",
|
||||
License: "MIT",
|
||||
Readme: "Default",
|
||||
IssueLabels: "Default",
|
||||
Private: false,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, repo)
|
||||
|
||||
return func() {
|
||||
_, _ = c.DeleteRepo(name, name)
|
||||
_, _ = c.DeleteOrg(name)
|
||||
}, repo, err
|
||||
}
|
65
gitea/repo_team.go
Normal file
65
gitea/repo_team.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetRepoTeams return teams from a repository
|
||||
func (c *Client) GetRepoTeams(user, repo string) ([]*Team, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
teams := make([]*Team, 0, 5)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/teams", user, repo), nil, nil, &teams)
|
||||
return teams, resp, err
|
||||
}
|
||||
|
||||
// AddRepoTeam add a team to a repository
|
||||
func (c *Client) AddRepoTeam(user, repo, team string) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := escapeValidatePathSegments(&user, &repo, &team); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// RemoveRepoTeam delete a team from a repository
|
||||
func (c *Client) RemoveRepoTeam(user, repo, team string) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := escapeValidatePathSegments(&user, &repo, &team); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// CheckRepoTeam check if team is assigned to repo by name and return it.
|
||||
// If not assigned, it will return nil.
|
||||
func (c *Client) CheckRepoTeam(user, repo, team string) (*Team, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := escapeValidatePathSegments(&user, &repo, &team); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
t := new(Team)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil, &t)
|
||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||
// if not found it's not an error, it indicates it's not assigned
|
||||
return nil, resp, nil
|
||||
}
|
||||
return t, resp, err
|
||||
}
|
74
gitea/repo_team_test.go
Normal file
74
gitea/repo_team_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRepoTeamManagement(t *testing.T) {
|
||||
log.Println("== TestRepoTeamManagement ==")
|
||||
c := newTestClient()
|
||||
|
||||
// prepare for test
|
||||
clean, repo, err := createTestOrgRepo(t, c, "RepoTeamManagement")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer clean()
|
||||
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "Admins", AccessModeAdmin, []RepoUnitType{RepoUnitCode, RepoUnitIssues, RepoUnitPulls, RepoUnitReleases}); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "CodeManager", AccessModeWrite, []RepoUnitType{RepoUnitCode}); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "IssueManager", AccessModeWrite, []RepoUnitType{RepoUnitIssues, RepoUnitPulls}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// test
|
||||
teams, _, err := c.GetRepoTeams(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
if !assert.Len(t, teams, 1) {
|
||||
return
|
||||
}
|
||||
assert.EqualValues(t, AccessModeOwner, teams[0].Permission)
|
||||
|
||||
team, _, err := c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, team)
|
||||
|
||||
resp, err := c.AddRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 204, resp.StatusCode)
|
||||
resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "CodeManager")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 204, resp.StatusCode)
|
||||
resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 204, resp.StatusCode)
|
||||
|
||||
team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
|
||||
assert.NoError(t, err)
|
||||
if assert.NotNil(t, team) {
|
||||
assert.EqualValues(t, "Admins", team.Name)
|
||||
assert.EqualValues(t, AccessModeAdmin, team.Permission)
|
||||
}
|
||||
|
||||
teams, _, err = c.GetRepoTeams(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, teams, 4)
|
||||
|
||||
resp, err = c.RemoveRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 204, resp.StatusCode)
|
||||
|
||||
team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, team)
|
||||
}
|
Loading…
Reference in a new issue