feat(gitea): branch list and commit-status endpoints
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// CommitInfo is the commit projection branches carry.
|
||||
type CommitInfo struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// Branch is the branch projection.
|
||||
type Branch struct {
|
||||
Name string `json:"name"`
|
||||
Commit CommitInfo `json:"commit"`
|
||||
Protected bool `json:"protected"`
|
||||
}
|
||||
|
||||
// ListBranches lists a repository's branches.
|
||||
func (c *Client) ListBranches(ctx context.Context, owner, repo string) ([]Branch, error) {
|
||||
var out []Branch
|
||||
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/branches"
|
||||
if err := c.do(ctx, "GET", path, nil, nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Commit status states (Gitea's closed set).
|
||||
const (
|
||||
StatusPending = "pending"
|
||||
StatusSuccess = "success"
|
||||
StatusError = "error"
|
||||
StatusFailure = "failure"
|
||||
)
|
||||
|
||||
// CommitStatusParams is the status-create write shape.
|
||||
type CommitStatusParams struct {
|
||||
State string // one of the Status* constants
|
||||
Context string // e.g. "ci/lint"; server defaults to "default"
|
||||
Description string
|
||||
TargetURL string `json:"target_url"`
|
||||
}
|
||||
|
||||
// CommitStatus is the status projection (read shape).
|
||||
type CommitStatus struct {
|
||||
ID int64 `json:"id"`
|
||||
State string `json:"state"`
|
||||
Context string `json:"context"`
|
||||
Description string `json:"description"`
|
||||
TargetURL string `json:"target_url"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// CombinedStatus is the worst-of rollup Gitea reports per ref.
|
||||
type CombinedStatus struct {
|
||||
State string `json:"state"`
|
||||
SHA string `json:"sha"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Statuses []CommitStatus `json:"statuses"`
|
||||
}
|
||||
|
||||
// CreateCommitStatus reports one status on a commit (sha may be any ref
|
||||
// the server resolves: full or short sha, branch, tag).
|
||||
func (c *Client) CreateCommitStatus(ctx context.Context, owner, repo, sha string, p CommitStatusParams) (*CommitStatus, error) {
|
||||
var out CommitStatus
|
||||
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/statuses/" + url.PathEscape(sha)
|
||||
if err := c.do(ctx, "POST", path, nil, p, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// ListCommitStatuses lists the latest status per context for a ref.
|
||||
func (c *Client) ListCommitStatuses(ctx context.Context, owner, repo, ref string) ([]CommitStatus, error) {
|
||||
var out []CommitStatus
|
||||
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/commits/" + url.PathEscape(ref) + "/statuses"
|
||||
if err := c.do(ctx, "GET", path, nil, nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetCombinedStatus fetches the worst-of rollup for a ref. Gitea answers
|
||||
// 404 when the ref has no statuses at all.
|
||||
func (c *Client) GetCombinedStatus(ctx context.Context, owner, repo, ref string) (*CombinedStatus, error) {
|
||||
var out CombinedStatus
|
||||
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/commits/" + url.PathEscape(ref) + "/status"
|
||||
if err := c.do(ctx, "GET", path, nil, nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user