From 8e238c79dedbc5331ecb36c1b579b4739b659cd9 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 08:53:33 +0000 Subject: [PATCH] feat(repo): add subscription watch helpers Co-Authored-By: Virgil --- repos.go | 26 ++++++++++++++++++ repos_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/repos.go b/repos.go index d78f407..a3e39bf 100644 --- a/repos.go +++ b/repos.go @@ -91,6 +91,32 @@ func (s *RepoService) GetNewPinAllowed(ctx context.Context, owner, repo string) return &out, nil } +// 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)) + var out types.WatchInfo + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + +// Watch subscribes the current user to repository notifications. +func (s *RepoService) Watch(ctx context.Context, owner, repo string) (*types.WatchInfo, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/subscription", pathParams("owner", owner, "repo", repo)) + var out types.WatchInfo + if err := s.client.Put(ctx, path, nil, &out); err != nil { + return nil, err + } + return &out, nil +} + +// Unwatch unsubscribes the current user from repository notifications. +func (s *RepoService) Unwatch(ctx context.Context, owner, repo string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/subscription", pathParams("owner", owner, "repo", repo)) + return s.client.Delete(ctx, path) +} + // Fork forks a repository. If org is non-empty, forks into that organisation. func (s *RepoService) Fork(ctx context.Context, owner, repo, org string) (*types.Repository, error) { body := map[string]string{} diff --git a/repos_test.go b/repos_test.go index cb37136..5119c78 100644 --- a/repos_test.go +++ b/repos_test.go @@ -91,6 +91,80 @@ func TestRepoService_GetNewPinAllowed_Good(t *testing.T) { } } +func TestRepoService_GetSubscription_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/subscription" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + json.NewEncoder(w).Encode(types.WatchInfo{ + Subscribed: true, + Ignored: false, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + result, err := f.Repos.GetSubscription(context.Background(), "core", "go-forge") + if err != nil { + t.Fatal(err) + } + if !result.Subscribed || result.Ignored { + t.Fatalf("got %#v", result) + } +} + +func TestRepoService_Watch_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPut { + t.Errorf("expected PUT, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/subscription" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + json.NewEncoder(w).Encode(types.WatchInfo{ + Subscribed: true, + Ignored: false, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + result, err := f.Repos.Watch(context.Background(), "core", "go-forge") + if err != nil { + t.Fatal(err) + } + if !result.Subscribed || result.Ignored { + t.Fatalf("got %#v", result) + } +} + +func TestRepoService_Unwatch_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/subscription" { + 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.Unwatch(context.Background(), "core", "go-forge"); err != nil { + t.Fatal(err) + } +} + func TestRepoService_PathParamsAreEscaped_Good(t *testing.T) { owner := "acme org" repo := "my/repo"