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-02-05 07:59:55 +00:00
|
|
|
func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, error) {
|
|
|
|
opt.setDefaults()
|
|
|
|
collaborators := make([]*User, 0, opt.PageSize)
|
|
|
|
return collaborators, c.getParsedResponse("GET",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/collaborators?%s", user, repo, opt.getURLQuery().Encode()),
|
|
|
|
nil, nil, &collaborators)
|
2016-12-13 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsCollaborator check if a user is a collaborator of a repository
|
|
|
|
func (c *Client) IsCollaborator(user, repo, collaborator string) (bool, error) {
|
2020-02-02 15:19:11 +00:00
|
|
|
status, 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 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if status == 204 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// AddCollaboratorOption options when adding a user as a collaborator of a repository
|
|
|
|
type AddCollaboratorOption struct {
|
|
|
|
Permission *string `json:"permission"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// AddCollaborator add some user as a collaborator of a repository
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollaboratorOption) error {
|
2016-08-11 17:07:42 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-07 15:26:05 +01:00
|
|
|
_, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), jsonHeader, bytes.NewReader(body))
|
2016-08-11 17:07:42 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-12-13 01:59:51 +00:00
|
|
|
|
|
|
|
// DeleteCollaborator remove a collaborator from a repository
|
|
|
|
func (c *Client) DeleteCollaborator(user, repo, collaborator string) error {
|
|
|
|
_, err := c.getResponse("DELETE",
|
2020-02-02 15:19:11 +00:00
|
|
|
fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
2016-12-13 01:59:51 +00:00
|
|
|
return err
|
|
|
|
}
|