From 99867c9d4bf60237193c3f732ea95d87a6768505 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 22:30:00 +0000 Subject: [PATCH] feat(repo): add avatar helpers Co-Authored-By: Virgil --- docs/api-contract.md | 2 ++ repos.go | 12 +++++++++++ repos_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/docs/api-contract.md b/docs/api-contract.md index ae87f6d..13bb8dc 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -184,6 +184,8 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | RepoService.ListUserRepos | `func (s *RepoService) ListUserRepos(ctx context.Context) ([]types.Repository, error)` | ListUserRepos returns all repositories for the authenticated user. | No direct tests. | | method | RepoService.MirrorSync | `func (s *RepoService) MirrorSync(ctx context.Context, owner, repo string) error` | MirrorSync triggers a mirror sync. | No direct tests. | | method | RepoService.RejectTransfer | `func (s *RepoService) RejectTransfer(ctx context.Context, owner, repo string) error` | RejectTransfer rejects a pending repository transfer. | No direct tests. | +| method | RepoService.DeleteAvatar | `func (s *RepoService) DeleteAvatar(ctx context.Context, owner, repo string) error` | DeleteAvatar deletes a repository avatar. | `TestRepoService_DeleteAvatar_Good` | +| method | RepoService.UpdateAvatar | `func (s *RepoService) UpdateAvatar(ctx context.Context, owner, repo string, opts *types.UpdateRepoAvatarOption) error` | UpdateAvatar updates a repository avatar. | `TestRepoService_UpdateAvatar_Good` | | method | RepoService.Transfer | `func (s *RepoService) Transfer(ctx context.Context, owner, repo string, opts map[string]any) error` | Transfer initiates a repository transfer. | No direct tests. | | method | Resource.Create | `func (r *Resource[T, C, U]) Create(ctx context.Context, params Params, body *C) (*T, error)` | Create creates a new resource. | `TestResource_Good_Create` | | method | Resource.Delete | `func (r *Resource[T, C, U]) Delete(ctx context.Context, params Params) error` | Delete removes a resource. | `TestResource_Good_Delete` | diff --git a/repos.go b/repos.go index 0c1629c..105198f 100644 --- a/repos.go +++ b/repos.go @@ -186,6 +186,18 @@ func (s *RepoService) GetNewPinAllowed(ctx context.Context, owner, repo string) return &out, nil } +// UpdateAvatar updates a repository avatar. +func (s *RepoService) UpdateAvatar(ctx context.Context, owner, repo string, opts *types.UpdateRepoAvatarOption) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/avatar", pathParams("owner", owner, "repo", repo)) + return s.client.Post(ctx, path, opts, nil) +} + +// DeleteAvatar deletes a repository avatar. +func (s *RepoService) DeleteAvatar(ctx context.Context, owner, repo string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/avatar", pathParams("owner", owner, "repo", repo)) + return s.client.Delete(ctx, path) +} + // GetSubscription returns the current user's watch state for a repository. func (s *RepoService) GetSubscription(ctx context.Context, owner, repo string) (*types.WatchInfo, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/subscription", pathParams("owner", owner, "repo", repo)) diff --git a/repos_test.go b/repos_test.go index cd63ad5..df59491 100644 --- a/repos_test.go +++ b/repos_test.go @@ -131,6 +131,55 @@ func TestRepoService_GetNewPinAllowed_Good(t *testing.T) { } } +func TestRepoService_UpdateAvatar_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/repos/core/go-forge/avatar" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + var body types.UpdateRepoAvatarOption + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Fatalf("decode body: %v", err) + } + if body.Image != "iVBORw0KGgoAAAANSUhEUg==" { + t.Fatalf("got image=%q", body.Image) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.UpdateAvatar(context.Background(), "core", "go-forge", &types.UpdateRepoAvatarOption{ + Image: "iVBORw0KGgoAAAANSUhEUg==", + }); err != nil { + t.Fatal(err) + } +} + +func TestRepoService_DeleteAvatar_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + t.Errorf("expected DELETE, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/avatar" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.DeleteAvatar(context.Background(), "core", "go-forge"); err != nil { + t.Fatal(err) + } +} + func TestRepoService_GetSubscription_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {