2021-08-13 13:16:25 +01:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2016-08-11 17:07:42 +01:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2016-08-11 17:07:42 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-11 19:34:16 +01:00
|
|
|
"encoding/json"
|
2016-08-11 17:07:42 +01:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListCollaboratorsOptions options for listing a repository's collaborators
|
|
|
|
type ListCollaboratorsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2016-12-13 01:59:51 +00:00
|
|
|
// ListCollaborators list a repository's collaborators
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
collaborators := make([]*User, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET",
|
2020-02-05 07:59:55 +00:00
|
|
|
fmt.Sprintf("/repos/%s/%s/collaborators?%s", user, repo, opt.getURLQuery().Encode()),
|
|
|
|
nil, nil, &collaborators)
|
2020-09-14 03:37:09 +01:00
|
|
|
return collaborators, resp, err
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsCollaborator check if a user is a collaborator of a repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) IsCollaborator(user, repo, collaborator string) (bool, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
2016-12-13 01:59:51 +00:00
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return false, resp, err
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
|
|
|
if status == 204 {
|
2020-09-14 03:37:09 +01:00
|
|
|
return true, resp, nil
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
return false, resp, nil
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// AddCollaboratorOption options when adding a user as a collaborator of a repository
|
|
|
|
type AddCollaboratorOption struct {
|
2020-09-06 14:24:45 +01:00
|
|
|
Permission *AccessMode `json:"permission"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:24:45 +01:00
|
|
|
// AccessMode represent the grade of access you have to something
|
|
|
|
type AccessMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AccessModeNone no access
|
|
|
|
AccessModeNone AccessMode = "none"
|
|
|
|
// AccessModeRead read access
|
|
|
|
AccessModeRead AccessMode = "read"
|
|
|
|
// AccessModeWrite write access
|
|
|
|
AccessModeWrite AccessMode = "write"
|
|
|
|
// AccessModeAdmin admin access
|
|
|
|
AccessModeAdmin AccessMode = "admin"
|
|
|
|
// AccessModeOwner owner
|
|
|
|
AccessModeOwner AccessMode = "owner"
|
|
|
|
)
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// Validate the AddCollaboratorOption struct
|
|
|
|
func (opt AddCollaboratorOption) Validate() error {
|
2020-09-06 14:24:45 +01:00
|
|
|
if opt.Permission != nil {
|
|
|
|
if *opt.Permission == AccessModeOwner {
|
|
|
|
*opt.Permission = AccessModeAdmin
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if *opt.Permission == AccessModeNone {
|
|
|
|
opt.Permission = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if *opt.Permission != AccessModeRead && *opt.Permission != AccessModeWrite && *opt.Permission != AccessModeAdmin {
|
|
|
|
return fmt.Errorf("permission mode invalid")
|
|
|
|
}
|
2020-06-09 11:47:55 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// AddCollaborator add some user as a collaborator of a repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollaboratorOption) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2020-06-09 11:47:55 +01:00
|
|
|
}
|
2016-08-11 17:07:42 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2016-08-11 17:07:42 +01:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2016-08-11 17:07:42 +01:00
|
|
|
}
|
2016-12-13 01:59:51 +00:00
|
|
|
|
|
|
|
// DeleteCollaborator remove a collaborator from a repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) DeleteCollaborator(user, repo, collaborator string) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("DELETE",
|
2020-02-02 15:19:11 +00:00
|
|
|
fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
2020-09-14 03:37:09 +01:00
|
|
|
return resp, err
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
2021-08-13 13:16:25 +01:00
|
|
|
|
|
|
|
// GetReviewers return all users that can be requested to review in this repo
|
|
|
|
func (c *Client) GetReviewers(user, repo string) ([]*User, *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
|
|
|
|
}
|
|
|
|
reviewers := make([]*User, 0, 5)
|
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/reviewers", user, repo), nil, nil, &reviewers)
|
|
|
|
return reviewers, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAssignees return all users that have write access and can be assigned to issues
|
|
|
|
func (c *Client) GetAssignees(user, repo string) ([]*User, *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
|
|
|
|
}
|
|
|
|
assignees := make([]*User, 0, 5)
|
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/assignees", user, repo), nil, nil, &assignees)
|
|
|
|
return assignees, resp, err
|
|
|
|
}
|