diff --git a/notifications.go b/notifications.go index d053bbc..a272354 100644 --- a/notifications.go +++ b/notifications.go @@ -32,6 +32,15 @@ func (s *NotificationService) Iter(ctx context.Context) iter.Seq2[types.Notifica return ListIter[types.NotificationThread](ctx, s.client, "/api/v1/notifications", nil) } +// NewAvailable returns the count of unread notifications for the authenticated user. +func (s *NotificationService) NewAvailable(ctx context.Context) (*types.NotificationCount, error) { + var out types.NotificationCount + if err := s.client.Get(ctx, "/api/v1/notifications/new", &out); err != nil { + return nil, err + } + return &out, nil +} + // ListRepo returns all notifications for a specific repository. func (s *NotificationService) ListRepo(ctx context.Context, owner, repo string) ([]types.NotificationThread, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/notifications", pathParams("owner", owner, "repo", repo)) diff --git a/notifications_test.go b/notifications_test.go index 69f1fd2..4d74b57 100644 --- a/notifications_test.go +++ b/notifications_test.go @@ -73,6 +73,28 @@ func TestNotificationService_ListRepo_Good(t *testing.T) { } } +func TestNotificationService_NewAvailable_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/notifications/new" { + t.Errorf("wrong path: %s", r.URL.Path) + } + json.NewEncoder(w).Encode(types.NotificationCount{New: 3}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + count, err := f.Notifications.NewAvailable(context.Background()) + if err != nil { + t.Fatal(err) + } + if count.New != 3 { + t.Fatalf("got new=%d, want 3", count.New) + } +} + func TestNotificationService_GetThread_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {