feat(forge): add missing repo and team helpers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
d553cbaa2d
commit
ddff64bc8e
6 changed files with 131 additions and 2 deletions
26
branches.go
26
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))
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
9
repos.go
9
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))
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
10
teams.go
10
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)))
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue