74 lines
3.1 KiB
Go
74 lines
3.1 KiB
Go
package forge
|
|
|
|
import (
|
|
"context"
|
|
"iter"
|
|
|
|
"dappco.re/go/core/forge/types"
|
|
)
|
|
|
|
// BranchService handles branch operations within a repository.
|
|
//
|
|
// Usage:
|
|
//
|
|
// f := forge.NewForge("https://forge.lthn.ai", "token")
|
|
// _, err := f.Branches.ListBranchProtections(ctx, "core", "go-forge")
|
|
type BranchService struct {
|
|
Resource[types.Branch, types.CreateBranchRepoOption, struct{}]
|
|
}
|
|
|
|
func newBranchService(c *Client) *BranchService {
|
|
return &BranchService{
|
|
Resource: *NewResource[types.Branch, types.CreateBranchRepoOption, struct{}](
|
|
c, "/api/v1/repos/{owner}/{repo}/branches/{branch}",
|
|
),
|
|
}
|
|
}
|
|
|
|
// ListBranchProtections returns all branch protections for a repository.
|
|
func (s *BranchService) ListBranchProtections(ctx context.Context, owner, repo string) ([]types.BranchProtection, error) {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
|
return ListAll[types.BranchProtection](ctx, s.client, path, nil)
|
|
}
|
|
|
|
// IterBranchProtections returns an iterator over all branch protections for a repository.
|
|
func (s *BranchService) IterBranchProtections(ctx context.Context, owner, repo string) iter.Seq2[types.BranchProtection, error] {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
|
return ListIter[types.BranchProtection](ctx, s.client, path, nil)
|
|
}
|
|
|
|
// GetBranchProtection returns a single branch protection by name.
|
|
func (s *BranchService) GetBranchProtection(ctx context.Context, owner, repo, name string) (*types.BranchProtection, error) {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
|
var out types.BranchProtection
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// CreateBranchProtection creates a new branch protection rule.
|
|
func (s *BranchService) CreateBranchProtection(ctx context.Context, owner, repo string, opts *types.CreateBranchProtectionOption) (*types.BranchProtection, error) {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
|
var out types.BranchProtection
|
|
if err := s.client.Post(ctx, path, opts, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// EditBranchProtection updates an existing branch protection rule.
|
|
func (s *BranchService) EditBranchProtection(ctx context.Context, owner, repo, name string, opts *types.EditBranchProtectionOption) (*types.BranchProtection, error) {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
|
var out types.BranchProtection
|
|
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// DeleteBranchProtection deletes a branch protection rule.
|
|
func (s *BranchService) DeleteBranchProtection(ctx context.Context, owner, repo, name string) error {
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
|
return s.client.Delete(ctx, path)
|
|
}
|