diff --git a/issues.go b/issues.go index daacbf4..098c565 100644 --- a/issues.go +++ b/issues.go @@ -118,6 +118,40 @@ func (s *IssueService) CreateComment(ctx context.Context, owner, repo string, in return &out, nil } +// ListSubscriptions returns all users subscribed to an issue. +func (s *IssueService) ListSubscriptions(ctx context.Context, owner, repo string, index int64) ([]types.User, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListAll[types.User](ctx, s.client, path, nil) +} + +// IterSubscriptions returns an iterator over all users subscribed to an issue. +func (s *IssueService) IterSubscriptions(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.User, error] { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListIter[types.User](ctx, s.client, path, nil) +} + +// CheckSubscription returns the authenticated user's subscription state for an issue. +func (s *IssueService) CheckSubscription(ctx context.Context, owner, repo string, index int64) (*types.WatchInfo, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions/check", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + var out types.WatchInfo + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + +// SubscribeUser subscribes a user to an issue. +func (s *IssueService) SubscribeUser(ctx context.Context, owner, repo string, index int64, user string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions/{user}", pathParams("owner", owner, "repo", repo, "index", int64String(index), "user", user)) + return s.client.Put(ctx, path, nil, nil) +} + +// UnsubscribeUser unsubscribes a user from an issue. +func (s *IssueService) UnsubscribeUser(ctx context.Context, owner, repo string, index int64, user string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions/{user}", pathParams("owner", owner, "repo", repo, "index", int64String(index), "user", user)) + return s.client.Delete(ctx, path) +} + // toAnySlice converts a slice of int64 to a slice of any for IssueLabelsOption. func toAnySlice(ids []int64) []any { out := make([]any, len(ids)) diff --git a/issues_test.go b/issues_test.go index 42955a3..b7ada8b 100644 --- a/issues_test.go +++ b/issues_test.go @@ -5,6 +5,7 @@ import ( json "github.com/goccy/go-json" "net/http" "net/http/httptest" + "reflect" "testing" "dappco.re/go/core/forge/types" @@ -168,6 +169,126 @@ func TestIssueService_CreateComment_Good(t *testing.T) { } } +func TestIssueService_ListSubscriptions_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/issues/1/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.User{ + {ID: 1, UserName: "alice"}, + {ID: 2, UserName: "bob"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + users, err := f.Issues.ListSubscriptions(context.Background(), "core", "go-forge", 1) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(users, []types.User{{ID: 1, UserName: "alice"}, {ID: 2, UserName: "bob"}}) { + t.Fatalf("got %#v", users) + } +} + +func TestIssueService_IterSubscriptions_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/issues/1/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.User{{ID: 1, UserName: "alice"}}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + var seen []types.User + for user, err := range f.Issues.IterSubscriptions(context.Background(), "core", "go-forge", 1) { + if err != nil { + t.Fatal(err) + } + seen = append(seen, user) + } + if !reflect.DeepEqual(seen, []types.User{{ID: 1, UserName: "alice"}}) { + t.Fatalf("got %#v", seen) + } +} + +func TestIssueService_CheckSubscription_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/issues/1/subscriptions/check" { + 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.Issues.CheckSubscription(context.Background(), "core", "go-forge", 1) + if err != nil { + t.Fatal(err) + } + if !result.Subscribed || result.Ignored { + t.Fatalf("got %#v", result) + } +} + +func TestIssueService_SubscribeUser_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/issues/1/subscriptions/alice" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusCreated) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Issues.SubscribeUser(context.Background(), "core", "go-forge", 1, "alice"); err != nil { + t.Fatal(err) + } +} + +func TestIssueService_UnsubscribeUser_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/issues/1/subscriptions/alice" { + 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.Issues.UnsubscribeUser(context.Background(), "core", "go-forge", 1, "alice"); err != nil { + t.Fatal(err) + } +} + func TestIssueService_Pin_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost {