forgejo-sdk/gitea/repo.go

97 lines
3.4 KiB
Go
Raw Normal View History

2014-11-14 22:07:41 +00:00
// Copyright 2014 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.
2015-08-26 14:34:27 +01:00
package gitea
2014-11-14 22:07:41 +00:00
2014-12-13 01:29:51 +00:00
import (
"bytes"
"encoding/json"
"fmt"
"code.gitea.io/gitea/modules/structs"
2014-12-13 01:29:51 +00:00
)
// Repository is equal to structs.Repository
type Repository = structs.Repository
2014-11-14 22:07:41 +00:00
// ListMyRepos lists all repositories for the authenticated user that has access to.
func (c *Client) ListMyRepos() ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
2015-12-17 07:15:29 +00:00
return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
2014-11-14 22:07:41 +00:00
}
2014-12-13 01:29:51 +00:00
2016-11-10 09:44:00 +00:00
// ListUserRepos list all repositories of one user by user's name
func (c *Client) ListUserRepos(user string) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos)
}
2016-11-10 09:44:00 +00:00
// ListOrgRepos list all repositories of one organization by organization's name
func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
2014-11-14 22:07:41 +00:00
}
2014-12-13 01:29:51 +00:00
// CreateRepo creates a repository for authenticated user.
func (c *Client) CreateRepo(opt structs.CreateRepoOption) (*Repository, error) {
2014-12-13 01:29:51 +00:00
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
2016-07-17 01:46:54 +01:00
return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
2014-12-13 01:29:51 +00:00
}
// CreateOrgRepo creates an organization repository for authenticated user.
func (c *Client) CreateOrgRepo(org string, opt structs.CreateRepoOption) (*Repository, error) {
2014-12-13 01:29:51 +00:00
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
2016-07-17 01:46:54 +01:00
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
2014-12-13 01:29:51 +00:00
}
2015-10-09 02:00:46 +01:00
2015-10-22 22:42:42 +01:00
// GetRepo returns information of a repository of given owner.
func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
repo := new(Repository)
2015-12-17 07:15:29 +00:00
return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
2015-10-22 22:42:42 +01:00
}
// EditRepo edit the properties of a repository
func (c *Client) EditRepo(owner, reponame string, opt structs.EditRepoOption) (*Repository, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s", owner, reponame), jsonHeader, bytes.NewReader(body), repo)
}
2015-10-09 02:00:46 +01:00
// DeleteRepo deletes a repository of user or organization.
func (c *Client) DeleteRepo(owner, repo string) error {
2015-10-09 03:34:54 +01:00
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
return err
2015-10-09 02:00:46 +01:00
}
2015-10-28 16:04:48 +00:00
// MigrateRepo migrates a repository from other Git hosting sources for the
// authenticated user.
//
// To migrate a repository for a organization, the authenticated user must be a
// owner of the specified organization.
func (c *Client) MigrateRepo(opt structs.MigrateRepoOption) (*Repository, error) {
2015-10-28 16:04:48 +00:00
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
2016-07-17 01:46:54 +01:00
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
2014-12-13 01:29:51 +00:00
}
// MirrorSync adds a mirrored repository to the mirror sync queue.
func (c *Client) MirrorSync(owner, repo string) error {
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)
return err
}