From ddff64bc8eee485103e2ff5d67db284145436ae6 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 08:22:44 +0000 Subject: [PATCH] feat(forge): add missing repo and team helpers Co-Authored-By: Virgil --- branches.go | 26 ++++++++++++++++++++++++-- branches_test.go | 27 +++++++++++++++++++++++++++ repos.go | 9 +++++++++ repos_test.go | 30 ++++++++++++++++++++++++++++++ teams.go | 10 ++++++++++ teams_test.go | 31 +++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 2 deletions(-) diff --git a/branches.go b/branches.go index 305fadc..e7d4320 100644 --- a/branches.go +++ b/branches.go @@ -14,12 +14,12 @@ import ( // 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{}] + Resource[types.Branch, types.CreateBranchRepoOption, types.UpdateBranchRepoOption] } func newBranchService(c *Client) *BranchService { return &BranchService{ - Resource: *NewResource[types.Branch, types.CreateBranchRepoOption, struct{}]( + Resource: *NewResource[types.Branch, types.CreateBranchRepoOption, types.UpdateBranchRepoOption]( c, "/api/v1/repos/{owner}/{repo}/branches/{branch}", ), } @@ -46,6 +46,28 @@ func (s *BranchService) CreateBranch(ctx context.Context, owner, repo string, op return &out, nil } +// 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) +} + // 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)) diff --git a/branches_test.go b/branches_test.go index ca1f99e..650507d 100644 --- a/branches_test.go +++ b/branches_test.go @@ -61,6 +61,33 @@ func TestBranchService_Get_Good(t *testing.T) { } } +func TestBranchService_UpdateBranch_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPatch { + t.Errorf("expected PATCH, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/branches/main" { + t.Errorf("wrong path: %s", r.URL.Path) + } + var opts types.UpdateBranchRepoOption + if err := json.NewDecoder(r.Body).Decode(&opts); err != nil { + t.Fatal(err) + } + if opts.Name != "develop" { + t.Errorf("got name=%q, want %q", opts.Name, "develop") + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Branches.UpdateBranch(context.Background(), "core", "go-forge", "main", &types.UpdateBranchRepoOption{ + Name: "develop", + }); err != nil { + t.Fatal(err) + } +} + func TestBranchService_CreateProtection_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { diff --git a/repos.go b/repos.go index 08f9fee..5de4411 100644 --- a/repos.go +++ b/repos.go @@ -136,6 +136,15 @@ func (s *RepoService) Migrate(ctx context.Context, opts *types.MigrateRepoOption return &out, nil } +// CreateCurrentUserRepo creates a repository for the authenticated user. +func (s *RepoService) CreateCurrentUserRepo(ctx context.Context, opts *types.CreateRepoOption) (*types.Repository, error) { + var out types.Repository + if err := s.client.Post(ctx, "/api/v1/user/repos", opts, &out); err != nil { + return nil, err + } + return &out, nil +} + // CreateOrgRepo creates a repository in an organisation. func (s *RepoService) CreateOrgRepo(ctx context.Context, org string, opts *types.CreateRepoOption) (*types.Repository, error) { path := ResolvePath("/api/v1/orgs/{org}/repos", pathParams("org", org)) diff --git a/repos_test.go b/repos_test.go index f813e2d..3c6c22b 100644 --- a/repos_test.go +++ b/repos_test.go @@ -2183,6 +2183,36 @@ func TestRepoService_PathParamsAreEscaped_Good(t *testing.T) { } }) + t.Run("CreateCurrentUserRepo", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Errorf("expected POST, got %s", r.Method) + } + if r.URL.EscapedPath() != "/api/v1/user/repos" { + t.Errorf("got path %q, want %q", r.URL.EscapedPath(), "/api/v1/user/repos") + http.NotFound(w, r) + return + } + var opts types.CreateRepoOption + if err := json.NewDecoder(r.Body).Decode(&opts); err != nil { + t.Fatalf("decode body: %v", err) + } + if opts.Name != "go-forge" || !opts.Private { + t.Fatalf("got %#v", opts) + } + json.NewEncoder(w).Encode(types.Repository{Name: opts.Name}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if _, err := f.Repos.CreateCurrentUserRepo(context.Background(), &types.CreateRepoOption{ + Name: "go-forge", + Private: true, + }); err != nil { + t.Fatal(err) + } + }) + t.Run("Fork", func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { want := "/api/v1/repos/acme%20org/my%2Frepo/forks" diff --git a/teams.go b/teams.go index 0baa9fe..88e414c 100644 --- a/teams.go +++ b/teams.go @@ -25,6 +25,16 @@ func newTeamService(c *Client) *TeamService { } } +// CreateOrgTeam creates a team within an organisation. +func (s *TeamService) CreateOrgTeam(ctx context.Context, org string, opts *types.CreateTeamOption) (*types.Team, error) { + path := ResolvePath("/api/v1/orgs/{org}/teams", pathParams("org", org)) + var out types.Team + if err := s.client.Post(ctx, path, opts, &out); err != nil { + return nil, err + } + return &out, nil +} + // ListMembers returns all members of a team. func (s *TeamService) ListMembers(ctx context.Context, teamID int64) ([]types.User, error) { path := ResolvePath("/api/v1/teams/{id}/members", pathParams("id", int64String(teamID))) diff --git a/teams_test.go b/teams_test.go index b18dbc8..0572411 100644 --- a/teams_test.go +++ b/teams_test.go @@ -32,6 +32,37 @@ func TestTeamService_Get_Good(t *testing.T) { } } +func TestTeamService_CreateOrgTeam_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Errorf("expected POST, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/teams" { + t.Errorf("wrong path: %s", r.URL.Path) + } + var opts types.CreateTeamOption + if err := json.NewDecoder(r.Body).Decode(&opts); err != nil { + t.Fatal(err) + } + if opts.Name != "platform" { + t.Errorf("got name=%q, want %q", opts.Name, "platform") + } + json.NewEncoder(w).Encode(types.Team{ID: 7, Name: opts.Name}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + team, err := f.Teams.CreateOrgTeam(context.Background(), "core", &types.CreateTeamOption{ + Name: "platform", + }) + if err != nil { + t.Fatal(err) + } + if team.ID != 7 || team.Name != "platform" { + t.Fatalf("got %#v", team) + } +} + func TestTeamService_ListMembers_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {