2019-06-02 12:00:48 +01:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-26 20:21:25 +01:00
|
|
|
"net/url"
|
2019-06-02 12:00:48 +01:00
|
|
|
)
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// Team represents a team in an organization
|
|
|
|
type Team struct {
|
2021-07-01 16:10:21 +01:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Organization *Organization `json:"organization"`
|
|
|
|
Permission AccessMode `json:"permission"`
|
|
|
|
CanCreateOrgRepo bool `json:"can_create_org_repo"`
|
|
|
|
IncludesAllRepositories bool `json:"includes_all_repositories"`
|
|
|
|
Units []RepoUnitType `json:"units"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
2019-06-02 12:00:48 +01:00
|
|
|
|
2021-07-01 16:10:21 +01:00
|
|
|
// RepoUnitType represent all unit types of a repo gitea currently offer
|
|
|
|
type RepoUnitType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RepoUnitCode represent file view of a repository
|
|
|
|
RepoUnitCode RepoUnitType = "repo.code"
|
|
|
|
// RepoUnitIssues represent issues of a repository
|
|
|
|
RepoUnitIssues RepoUnitType = "repo.issues"
|
|
|
|
// RepoUnitPulls represent pulls of a repository
|
|
|
|
RepoUnitPulls RepoUnitType = "repo.pulls"
|
|
|
|
// RepoUnitExtIssues represent external issues of a repository
|
|
|
|
RepoUnitExtIssues RepoUnitType = "repo.ext_issues"
|
|
|
|
// RepoUnitWiki represent wiki of a repository
|
|
|
|
RepoUnitWiki RepoUnitType = "repo.wiki"
|
|
|
|
// RepoUnitExtWiki represent external wiki of a repository
|
|
|
|
RepoUnitExtWiki RepoUnitType = "repo.ext_wiki"
|
|
|
|
// RepoUnitReleases represent releases of a repository
|
|
|
|
RepoUnitReleases RepoUnitType = "repo.releases"
|
|
|
|
// RepoUnitProjects represent projects of a repository
|
|
|
|
RepoUnitProjects RepoUnitType = "repo.projects"
|
|
|
|
)
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListTeamsOptions options for listing teams
|
|
|
|
type ListTeamsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:00:48 +01:00
|
|
|
// ListOrgTeams lists all teams of an organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListOrgTeams(org string, opt ListTeamsOptions) ([]*Team, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
teams := make([]*Team, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, opt.getURLQuery().Encode()), nil, nil, &teams)
|
|
|
|
return teams, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMyTeams lists all the teams of the current user
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListMyTeams(opt *ListTeamsOptions) ([]*Team, *Response, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
teams := make([]*Team, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", opt.getURLQuery().Encode()), nil, nil, &teams)
|
|
|
|
return teams, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeam gets a team by ID
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetTeam(id int64) (*Team, *Response, error) {
|
2019-06-02 12:00:48 +01:00
|
|
|
t := new(Team)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d", id), nil, nil, t)
|
|
|
|
return t, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:21:25 +01:00
|
|
|
// SearchTeamsOptions options for searching teams.
|
|
|
|
type SearchTeamsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
Query string
|
|
|
|
IncludeDescription bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o SearchTeamsOptions) getURLQuery() url.Values {
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Add("page", fmt.Sprintf("%d", o.Page))
|
|
|
|
query.Add("limit", fmt.Sprintf("%d", o.PageSize))
|
|
|
|
query.Add("q", o.Query)
|
|
|
|
query.Add("include_desc", fmt.Sprintf("%t", o.IncludeDescription))
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeamSearchResults is the JSON struct that is returned from Team search API.
|
|
|
|
type TeamSearchResults struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Data []*Team `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchOrgTeams search for teams in a org.
|
|
|
|
func (c *Client) SearchOrgTeams(org string, opt *SearchTeamsOptions) ([]*Team, *Response, error) {
|
|
|
|
responseBody := TeamSearchResults{}
|
|
|
|
opt.setDefaults()
|
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams/search?%s", org, opt.getURLQuery().Encode()), nil, nil, &responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if !responseBody.OK {
|
|
|
|
return nil, resp, fmt.Errorf("gitea error: %v", responseBody.Error)
|
|
|
|
}
|
|
|
|
return responseBody.Data, resp, err
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateTeamOption options for creating a team
|
|
|
|
type CreateTeamOption struct {
|
2021-07-01 16:10:21 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Permission AccessMode `json:"permission"`
|
|
|
|
CanCreateOrgRepo bool `json:"can_create_org_repo"`
|
|
|
|
IncludesAllRepositories bool `json:"includes_all_repositories"`
|
|
|
|
Units []RepoUnitType `json:"units"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:24:45 +01:00
|
|
|
// Validate the CreateTeamOption struct
|
|
|
|
func (opt CreateTeamOption) Validate() error {
|
|
|
|
if opt.Permission == AccessModeOwner {
|
|
|
|
opt.Permission = AccessModeAdmin
|
|
|
|
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
|
|
|
return fmt.Errorf("permission mode invalid")
|
|
|
|
}
|
|
|
|
if len(opt.Name) == 0 {
|
|
|
|
return fmt.Errorf("name required")
|
|
|
|
}
|
|
|
|
if len(opt.Name) > 30 {
|
|
|
|
return fmt.Errorf("name to long")
|
|
|
|
}
|
|
|
|
if len(opt.Description) > 255 {
|
|
|
|
return fmt.Errorf("description to long")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:00:48 +01:00
|
|
|
// CreateTeam creates a team for an organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) CreateTeam(org string, opt CreateTeamOption) (*Team, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-09-06 14:24:45 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2020-09-06 14:24:45 +01:00
|
|
|
}
|
2019-06-02 12:00:48 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
t := new(Team)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/teams", org), jsonHeader, bytes.NewReader(body), t)
|
|
|
|
return t, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditTeamOption options for editing a team
|
|
|
|
type EditTeamOption struct {
|
2021-07-01 16:10:21 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
Permission AccessMode `json:"permission"`
|
|
|
|
CanCreateOrgRepo *bool `json:"can_create_org_repo"`
|
|
|
|
IncludesAllRepositories *bool `json:"includes_all_repositories"`
|
|
|
|
Units []RepoUnitType `json:"units"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:24:45 +01:00
|
|
|
// Validate the EditTeamOption struct
|
|
|
|
func (opt EditTeamOption) Validate() error {
|
|
|
|
if opt.Permission == AccessModeOwner {
|
|
|
|
opt.Permission = AccessModeAdmin
|
|
|
|
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
|
|
|
return fmt.Errorf("permission mode invalid")
|
|
|
|
}
|
|
|
|
if len(opt.Name) == 0 {
|
|
|
|
return fmt.Errorf("name required")
|
|
|
|
}
|
|
|
|
if len(opt.Name) > 30 {
|
|
|
|
return fmt.Errorf("name to long")
|
|
|
|
}
|
|
|
|
if opt.Description != nil && len(*opt.Description) > 255 {
|
|
|
|
return fmt.Errorf("description to long")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:00:48 +01:00
|
|
|
// EditTeam edits a team of an organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) EditTeam(id int64, opt EditTeamOption) (*Response, error) {
|
2020-09-06 14:24:45 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2020-09-06 14:24:45 +01:00
|
|
|
}
|
2019-06-02 12:00:48 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/teams/%d", id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTeam deletes a team of an organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) DeleteTeam(id int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d", id), nil, nil)
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListTeamMembersOptions options for listing team's members
|
|
|
|
type ListTeamMembersOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:00:48 +01:00
|
|
|
// ListTeamMembers lists all members of a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListTeamMembers(id int64, opt ListTeamMembersOptions) ([]*User, *Response, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
members := make([]*User, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, opt.getURLQuery().Encode()), nil, nil, &members)
|
|
|
|
return members, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamMember gets a member of a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetTeamMember(id int64, user string) (*User, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-06-02 12:00:48 +01:00
|
|
|
m := new(User)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil, m)
|
|
|
|
return m, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTeamMember adds a member to a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) AddTeamMember(id int64, user string) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTeamMember removes a member from a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) RemoveTeamMember(id int64, user string) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListTeamRepositoriesOptions options for listing team's repositories
|
|
|
|
type ListTeamRepositoriesOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:00:48 +01:00
|
|
|
// ListTeamRepositories lists all repositories of a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListTeamRepositories(id int64, opt ListTeamRepositoriesOptions) ([]*Repository, *Response, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
repos := make([]*Repository, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, opt.getURLQuery().Encode()), nil, nil, &repos)
|
|
|
|
return repos, resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTeamRepository adds a repository to a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) AddTeamRepository(id int64, org, repo string) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTeamRepository removes a repository from a team
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) RemoveTeamRepository(id int64, org, repo string) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
|
|
|
return resp, err
|
2019-06-02 12:00:48 +01:00
|
|
|
}
|