add support for api /repos/{owner}/{repo}/collaborators/{collaborator}/permission (#611)

Co-authored-by: Dmitry Afanasiev <afanasiev.dmitry@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/611
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Dmitry Afanasiev <dafanasiev@noreply.gitea.io>
Co-committed-by: Dmitry Afanasiev <dafanasiev@noreply.gitea.io>
This commit is contained in:
Dmitry Afanasiev 2023-03-04 20:31:32 +08:00 committed by Lunny Xiao
parent df1269f18d
commit 846d53e967
2 changed files with 44 additions and 0 deletions

View file

@ -16,6 +16,13 @@ type ListCollaboratorsOptions struct {
ListOptions
}
// CollaboratorPermissionResult result type for CollaboratorPermission
type CollaboratorPermissionResult struct {
Permission AccessMode `json:"permission"`
Role string `json:"role_name"`
User *User `json:"user"`
}
// ListCollaborators list a repository's collaborators
func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
@ -44,6 +51,26 @@ func (c *Client) IsCollaborator(user, repo, collaborator string) (bool, *Respons
return false, resp, nil
}
// CollaboratorPermission gets collaborator permission of a repository
func (c *Client) CollaboratorPermission(user, repo, collaborator string) (*CollaboratorPermissionResult, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
return nil, nil, err
}
rv := new(CollaboratorPermissionResult)
resp, err := c.getParsedResponse("GET",
fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission", user, repo, collaborator),
nil,
nil,
rv)
if err != nil {
return nil, resp, err
}
if resp.StatusCode != 200 {
rv = nil
}
return rv, resp, nil
}
// AddCollaboratorOption options when adding a user as a collaborator of a repository
type AddCollaboratorOption struct {
Permission *AccessMode `json:"permission"`

View file

@ -34,10 +34,22 @@ func TestRepoCollaborator(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
permissonPing, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "ping")
assert.NoError(t, err)
assert.EqualValues(t, 200, resp.StatusCode)
assert.EqualValues(t, AccessModeAdmin, permissonPing.Permission)
assert.EqualValues(t, "ping", permissonPing.User.UserName)
mode = AccessModeRead
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, "pong", AddCollaboratorOption{Permission: &mode})
assert.NoError(t, err)
permissonPong, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "pong")
assert.NoError(t, err)
assert.EqualValues(t, 200, resp.StatusCode)
assert.EqualValues(t, AccessModeRead, permissonPong.Permission)
assert.EqualValues(t, "pong", permissonPong.User.UserName)
collaborators, _, err = c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
assert.NoError(t, err)
assert.Len(t, collaborators, 2)
@ -60,4 +72,9 @@ func TestRepoCollaborator(t *testing.T) {
collaborators, _, err = c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
assert.NoError(t, err)
assert.Len(t, collaborators, 1)
permissonNotExists, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "user_that_not_exists")
assert.Error(t, err)
assert.EqualValues(t, 404, resp.StatusCode)
assert.Nil(t, permissonNotExists)
}