91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
)
|
|
|
|
// User is the account projection repos and PRs carry.
|
|
type User struct {
|
|
ID int64 `json:"id"`
|
|
Login string `json:"login"`
|
|
FullName string `json:"full_name,omitempty"`
|
|
}
|
|
|
|
// Repo is the repository projection (read shape).
|
|
type Repo struct {
|
|
ID int64 `json:"id"`
|
|
Owner User `json:"owner"`
|
|
Name string `json:"name"`
|
|
FullName string `json:"full_name"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
Empty bool `json:"empty"`
|
|
DefaultBranch string `json:"default_branch"`
|
|
HTMLURL string `json:"html_url"`
|
|
CloneURL string `json:"clone_url"`
|
|
}
|
|
|
|
// RepoParams is the repo-create write shape. Owner routes the create:
|
|
// empty creates under the authenticated account (POST /user/repos), any
|
|
// other value creates under that organization (POST /orgs/{owner}/repos).
|
|
type RepoParams struct {
|
|
Owner string
|
|
Name string
|
|
Description string
|
|
Private bool
|
|
AutoInit bool
|
|
DefaultBranch string
|
|
}
|
|
|
|
type repoWrite struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
AutoInit bool `json:"auto_init"`
|
|
DefaultBranch string `json:"default_branch,omitempty"`
|
|
}
|
|
|
|
// CreateRepo creates a repository and returns it.
|
|
func (c *Client) CreateRepo(ctx context.Context, p RepoParams) (*Repo, error) {
|
|
path := "/user/repos"
|
|
if p.Owner != "" {
|
|
path = "/orgs/" + url.PathEscape(p.Owner) + "/repos"
|
|
}
|
|
var out Repo
|
|
if err := c.do(ctx, "POST", path, nil, repoWrite{
|
|
Name: p.Name,
|
|
Description: p.Description,
|
|
Private: p.Private,
|
|
AutoInit: p.AutoInit,
|
|
DefaultBranch: p.DefaultBranch,
|
|
}, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// ListRepos lists repositories. An empty owner lists the authenticated
|
|
// account's own repos (GET /user/repos); otherwise the named account's
|
|
// or organization's (GET /users/{owner}/repos).
|
|
func (c *Client) ListRepos(ctx context.Context, owner string) ([]Repo, error) {
|
|
path := "/user/repos"
|
|
if owner != "" {
|
|
path = "/users/" + url.PathEscape(owner) + "/repos"
|
|
}
|
|
var out []Repo
|
|
if err := c.do(ctx, "GET", path, nil, nil, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// GetRepo fetches one repository.
|
|
func (c *Client) GetRepo(ctx context.Context, owner, name string) (*Repo, error) {
|
|
var out Repo
|
|
if err := c.do(ctx, "GET", "/repos/"+url.PathEscape(owner)+"/"+url.PathEscape(name), nil, nil, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|