repo_create: POST /user/repos or /orgs/{org}/repos, is_org flag routes
repo_update: PATCH /repos/{owner}/{repo}, confirm required when private=false
repo_mirror_push: add/list/delete push mirrors, password never returned
153 lines
3.8 KiB
Go
153 lines
3.8 KiB
Go
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
type Repo struct {
|
|
Name string `json:"name"`
|
|
FullName string `json:"full_name"`
|
|
DefaultBranch string `json:"default_branch"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
CloneURL string `json:"clone_url"`
|
|
HTMLURL string `json:"html_url"`
|
|
Template bool `json:"template"`
|
|
}
|
|
|
|
func (c *Client) ListRepos(ctx context.Context, owner string, page, limit int) ([]Repo, error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 {
|
|
limit = 30
|
|
}
|
|
path := fmt.Sprintf("/api/v1/users/%s/repos?page=%d&limit=%d", owner, page, limit)
|
|
body, status, err := c.GetJSON(ctx, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var repos []Repo
|
|
if err := json.Unmarshal(body, &repos); err != nil {
|
|
return nil, err
|
|
}
|
|
return repos, nil
|
|
}
|
|
|
|
type repoSearchEnvelope struct {
|
|
Data []Repo `json:"data"`
|
|
OK bool `json:"ok"`
|
|
}
|
|
|
|
func (c *Client) SearchRepos(ctx context.Context, q, owner string, page, limit int) ([]Repo, error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 {
|
|
limit = 30
|
|
}
|
|
path := fmt.Sprintf("/api/v1/repos/search?q=%s&page=%d&limit=%d",
|
|
url.QueryEscape(q), page, limit)
|
|
if owner != "" {
|
|
path += "&owner=" + url.QueryEscape(owner)
|
|
}
|
|
body, status, err := c.GetJSON(ctx, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var env repoSearchEnvelope
|
|
if err := json.Unmarshal(body, &env); err != nil {
|
|
return nil, err
|
|
}
|
|
return env.Data, nil
|
|
}
|
|
|
|
type CreateRepoArgs struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Private bool `json:"private,omitempty"`
|
|
AutoInit bool `json:"auto_init,omitempty"`
|
|
DefaultBranch string `json:"default_branch,omitempty"`
|
|
// Org, when non-empty, creates the repo under the named organisation.
|
|
// Uses POST /api/v1/orgs/{org}/repos instead of /api/v1/user/repos.
|
|
Org string `json:"-"`
|
|
}
|
|
|
|
func (c *Client) CreateRepo(ctx context.Context, args CreateRepoArgs) (*Repo, error) {
|
|
var path string
|
|
if args.Org != "" {
|
|
path = fmt.Sprintf("/api/v1/orgs/%s/repos", args.Org)
|
|
} else {
|
|
path = "/api/v1/user/repos"
|
|
}
|
|
body, err := json.Marshal(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, status, err := c.PostJSON(ctx, path, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
var r Repo
|
|
if err := json.Unmarshal(resp, &r); err != nil {
|
|
return nil, err
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
// UpdateRepoArgs uses pointers so omitempty can distinguish "not set" from false/zero.
|
|
type UpdateRepoArgs struct {
|
|
Description *string `json:"description,omitempty"`
|
|
Private *bool `json:"private,omitempty"`
|
|
Website *string `json:"website,omitempty"`
|
|
DefaultBranch *string `json:"default_branch,omitempty"`
|
|
}
|
|
|
|
func (c *Client) UpdateRepo(ctx context.Context, owner, name string, args UpdateRepoArgs) (*Repo, error) {
|
|
path := fmt.Sprintf("/api/v1/repos/%s/%s", owner, name)
|
|
body, err := json.Marshal(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, status, err := c.PatchJSON(ctx, path, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
var r Repo
|
|
if err := json.Unmarshal(resp, &r); err != nil {
|
|
return nil, err
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
func (c *Client) GetRepo(ctx context.Context, owner, name string) (*Repo, error) {
|
|
path := fmt.Sprintf("/api/v1/repos/%s/%s", owner, name)
|
|
body, status, err := c.GetJSON(ctx, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var r Repo
|
|
if err := json.Unmarshal(body, &r); err != nil {
|
|
return nil, err
|
|
}
|
|
return &r, nil
|
|
}
|