156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type FileContents struct {
|
|
Path string `json:"path"`
|
|
Sha string `json:"sha"`
|
|
Size int64 `json:"size"`
|
|
Content string `json:"content"`
|
|
Encoding string `json:"encoding"`
|
|
}
|
|
|
|
func (c *Client) GetFileContents(ctx context.Context, owner, repo, path, ref string) (*FileContents, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
|
if ref != "" {
|
|
p += "?ref=" + ref
|
|
}
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var fc FileContents
|
|
if err := json.Unmarshal(body, &fc); err != nil {
|
|
return nil, err
|
|
}
|
|
return &fc, nil
|
|
}
|
|
|
|
type Branch struct {
|
|
Name string `json:"name"`
|
|
Commit struct {
|
|
ID string `json:"id"`
|
|
URL string `json:"url"`
|
|
} `json:"commit"`
|
|
}
|
|
|
|
// BranchExists returns (true, nil) if the branch exists, (false, nil) on 404, (false, err) otherwise.
|
|
func (c *Client) BranchExists(ctx context.Context, owner, repo, branch string) (bool, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", owner, repo, branch)
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if status == 404 {
|
|
return false, nil
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (c *Client) CreateBranch(ctx context.Context, owner, repo, newBranch, oldBranch string) error {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches", owner, repo)
|
|
payload, err := json.Marshal(map[string]string{
|
|
"new_branch_name": newBranch,
|
|
"old_branch_name": oldBranch,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, status, err := c.PostJSON(ctx, p, payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return MapStatus(status, body)
|
|
}
|
|
|
|
type UpsertFileArgs struct {
|
|
Branch string `json:"branch"`
|
|
Content string `json:"content"` // already base64-encoded
|
|
Message string `json:"message"`
|
|
Sha string `json:"sha,omitempty"`
|
|
}
|
|
|
|
type FileWriteResult struct {
|
|
Content struct {
|
|
Path string `json:"path"`
|
|
Sha string `json:"sha"`
|
|
HTMLURL string `json:"html_url"`
|
|
} `json:"content"`
|
|
Commit struct {
|
|
Sha string `json:"sha"`
|
|
HTMLURL string `json:"html_url"`
|
|
} `json:"commit"`
|
|
}
|
|
|
|
func (c *Client) ListBranches(ctx context.Context, owner, repo string, page, limit int) ([]Branch, error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 {
|
|
limit = 30
|
|
}
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches?page=%d&limit=%d", owner, repo, page, limit)
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var branches []Branch
|
|
if err := json.Unmarshal(body, &branches); err != nil {
|
|
return nil, err
|
|
}
|
|
return branches, nil
|
|
}
|
|
|
|
func (c *Client) DeleteBranch(ctx context.Context, owner, repo, branch string) error {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", owner, repo, branch)
|
|
body, status, err := c.DeleteJSON(ctx, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return MapStatus(status, body)
|
|
}
|
|
|
|
// UpsertFile creates a file when args.Sha is empty (POST) or updates an existing
|
|
// file when args.Sha is set (PUT). Gitea routes both operations by HTTP method on
|
|
// the same /contents/{path} URL, and rejects PUT without a sha.
|
|
func (c *Client) UpsertFile(ctx context.Context, owner, repo, path string, args UpsertFileArgs) (*FileWriteResult, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
|
payload, err := json.Marshal(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var (
|
|
body []byte
|
|
status int
|
|
)
|
|
if args.Sha == "" {
|
|
body, status, err = c.PostJSON(ctx, p, payload)
|
|
} else {
|
|
body, status, err = c.PutJSON(ctx, p, payload)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var out FileWriteResult
|
|
if err := json.Unmarshal(body, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|