feat(repo): add subscription watch helpers
All checks were successful
Security Scan / security (push) Successful in 12s
Test / test (push) Successful in 55s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 08:53:33 +00:00
parent 4ef2c273e5
commit 8e238c79de
2 changed files with 100 additions and 0 deletions

View file

@ -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{}

View file

@ -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"