2026-02-21 15:59:43 +00:00
|
|
|
package forge
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
"iter"
|
2026-02-21 15:59:43 +00:00
|
|
|
|
2026-03-22 01:51:29 +00:00
|
|
|
"dappco.re/go/core/forge/types"
|
2026-02-21 15:59:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BranchService handles branch operations within a repository.
|
2026-03-26 18:00:20 +00:00
|
|
|
//
|
|
|
|
|
// Usage:
|
|
|
|
|
//
|
|
|
|
|
// f := forge.NewForge("https://forge.lthn.ai", "token")
|
|
|
|
|
// _, err := f.Branches.ListBranchProtections(ctx, "core", "go-forge")
|
2026-02-21 15:59:43 +00:00
|
|
|
type BranchService struct {
|
2026-04-02 08:22:44 +00:00
|
|
|
Resource[types.Branch, types.CreateBranchRepoOption, types.UpdateBranchRepoOption]
|
2026-02-21 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newBranchService(c *Client) *BranchService {
|
|
|
|
|
return &BranchService{
|
2026-04-02 08:22:44 +00:00
|
|
|
Resource: *NewResource[types.Branch, types.CreateBranchRepoOption, types.UpdateBranchRepoOption](
|
2026-02-21 15:59:43 +00:00
|
|
|
c, "/api/v1/repos/{owner}/{repo}/branches/{branch}",
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 06:57:47 +00:00
|
|
|
// ListBranches returns all branches for a repository.
|
|
|
|
|
func (s *BranchService) ListBranches(ctx context.Context, owner, repo string) ([]types.Branch, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branches", pathParams("owner", owner, "repo", repo))
|
|
|
|
|
return ListAll[types.Branch](ctx, s.client, path, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IterBranches returns an iterator over all branches for a repository.
|
|
|
|
|
func (s *BranchService) IterBranches(ctx context.Context, owner, repo string) iter.Seq2[types.Branch, error] {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branches", pathParams("owner", owner, "repo", repo))
|
|
|
|
|
return ListIter[types.Branch](ctx, s.client, path, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateBranch creates a new branch in a repository.
|
|
|
|
|
func (s *BranchService) CreateBranch(ctx context.Context, owner, repo string, opts *types.CreateBranchRepoOption) (*types.Branch, error) {
|
|
|
|
|
var out types.Branch
|
|
|
|
|
if err := s.client.Post(ctx, ResolvePath("/api/v1/repos/{owner}/{repo}/branches", pathParams("owner", owner, "repo", repo)), opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 08:22:44 +00:00
|
|
|
// GetBranch returns a single branch by name.
|
|
|
|
|
func (s *BranchService) GetBranch(ctx context.Context, owner, repo, branch string) (*types.Branch, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branches/{branch}", pathParams("owner", owner, "repo", repo, "branch", branch))
|
|
|
|
|
var out types.Branch
|
|
|
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateBranch renames a branch in a repository.
|
|
|
|
|
func (s *BranchService) UpdateBranch(ctx context.Context, owner, repo, branch string, opts *types.UpdateBranchRepoOption) error {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branches/{branch}", pathParams("owner", owner, "repo", repo, "branch", branch))
|
|
|
|
|
return s.client.Patch(ctx, path, opts, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteBranch removes a branch from a repository.
|
|
|
|
|
func (s *BranchService) DeleteBranch(ctx context.Context, owner, repo, branch string) error {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branches/{branch}", pathParams("owner", owner, "repo", repo, "branch", branch))
|
|
|
|
|
return s.client.Delete(ctx, path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 15:59:43 +00:00
|
|
|
// ListBranchProtections returns all branch protections for a repository.
|
|
|
|
|
func (s *BranchService) ListBranchProtections(ctx context.Context, owner, repo string) ([]types.BranchProtection, error) {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
2026-02-21 15:59:43 +00:00
|
|
|
return ListAll[types.BranchProtection](ctx, s.client, path, nil)
|
|
|
|
|
}
|
|
|
|
|
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
// 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] {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
return ListIter[types.BranchProtection](ctx, s.client, path, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 15:59:43 +00:00
|
|
|
// GetBranchProtection returns a single branch protection by name.
|
|
|
|
|
func (s *BranchService) GetBranchProtection(ctx context.Context, owner, repo, name string) (*types.BranchProtection, error) {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
2026-02-21 15:59:43 +00:00
|
|
|
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) {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections", pathParams("owner", owner, "repo", repo))
|
2026-02-21 15:59:43 +00:00
|
|
|
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) {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
2026-02-21 15:59:43 +00:00
|
|
|
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 {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/branch_protections/{name}", pathParams("owner", owner, "repo", repo, "name", name))
|
2026-02-21 15:59:43 +00:00
|
|
|
return s.client.Delete(ctx, path)
|
|
|
|
|
}
|