feat: add repo notification marking
All checks were successful
Security Scan / security (push) Successful in 10s
Test / test (push) Successful in 1m35s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 00:52:43 +00:00
parent b5792bae9b
commit 122b68c5fb
3 changed files with 96 additions and 0 deletions

View file

@ -161,6 +161,7 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r
| method | NotificationService.IterRepo | `func (s *NotificationService) IterRepo(ctx context.Context, owner, repo string) iter.Seq2[types.NotificationThread, error]` | IterRepo returns an iterator over all notifications for a specific repository. | No direct tests. |
| method | NotificationService.List | `func (s *NotificationService) List(ctx context.Context) ([]types.NotificationThread, error)` | List returns all notifications for the authenticated user. | `TestNotificationService_Good_List` |
| method | NotificationService.ListRepo | `func (s *NotificationService) ListRepo(ctx context.Context, owner, repo string) ([]types.NotificationThread, error)` | ListRepo returns all notifications for a specific repository. | `TestNotificationService_Good_ListRepo` |
| method | NotificationService.MarkRepoNotifications | `func (s *NotificationService) MarkRepoNotifications(ctx context.Context, owner, repo string, opts *NotificationRepoMarkOptions) ([]types.NotificationThread, error)` | MarkRepoNotifications marks repository notification threads as read, unread, or pinned. | `TestNotificationService_MarkRepoNotifications_Good` |
| method | NotificationService.MarkRead | `func (s *NotificationService) MarkRead(ctx context.Context) error` | MarkRead marks all notifications as read. | `TestNotificationService_Good_MarkRead` |
| method | NotificationService.MarkThreadRead | `func (s *NotificationService) MarkThreadRead(ctx context.Context, id int64) error` | MarkThreadRead marks a single notification thread as read. | `TestNotificationService_Good_MarkThreadRead` |
| method | OrgService.AddMember | `func (s *OrgService) AddMember(ctx context.Context, org, username string) error` | AddMember adds a user to an organisation. | No direct tests. |

View file

@ -3,6 +3,8 @@ package forge
import (
"context"
"iter"
"net/url"
"time"
"dappco.re/go/core/forge/types"
)
@ -18,10 +20,37 @@ type NotificationService struct {
client *Client
}
// NotificationRepoMarkOptions controls how repository notifications are marked.
type NotificationRepoMarkOptions struct {
All bool
StatusTypes []string
ToStatus string
LastReadAt *time.Time
}
func newNotificationService(c *Client) *NotificationService {
return &NotificationService{client: c}
}
func (o NotificationRepoMarkOptions) queryString() string {
values := url.Values{}
if o.All {
values.Set("all", "true")
}
for _, status := range o.StatusTypes {
if status != "" {
values.Add("status-types", status)
}
}
if o.ToStatus != "" {
values.Set("to-status", o.ToStatus)
}
if o.LastReadAt != nil {
values.Set("last_read_at", o.LastReadAt.Format(time.RFC3339))
}
return values.Encode()
}
// List returns all notifications for the authenticated user.
func (s *NotificationService) List(ctx context.Context) ([]types.NotificationThread, error) {
return ListAll[types.NotificationThread](ctx, s.client, "/api/v1/notifications", nil)
@ -53,6 +82,21 @@ func (s *NotificationService) IterRepo(ctx context.Context, owner, repo string)
return ListIter[types.NotificationThread](ctx, s.client, path, nil)
}
// MarkRepoNotifications marks repository notification threads as read, unread, or pinned.
func (s *NotificationService) MarkRepoNotifications(ctx context.Context, owner, repo string, opts *NotificationRepoMarkOptions) ([]types.NotificationThread, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/notifications", pathParams("owner", owner, "repo", repo))
if opts != nil {
if query := opts.queryString(); query != "" {
path += "?" + query
}
}
var out []types.NotificationThread
if err := s.client.Put(ctx, path, nil, &out); err != nil {
return nil, err
}
return out, nil
}
// MarkRead marks all notifications as read.
func (s *NotificationService) MarkRead(ctx context.Context) error {
return s.client.Put(ctx, "/api/v1/notifications", nil, nil)

View file

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
"dappco.re/go/core/forge/types"
)
@ -167,6 +168,56 @@ func TestNotificationService_MarkThreadRead_Good(t *testing.T) {
}
}
func TestNotificationService_MarkRepoNotifications_Good(t *testing.T) {
lastReadAt := time.Date(2026, time.April, 2, 15, 4, 5, 0, time.UTC)
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/notifications" {
t.Errorf("wrong path: %s", r.URL.Path)
}
if got := r.URL.Query()["status-types"]; len(got) != 2 || got[0] != "unread" || got[1] != "pinned" {
t.Errorf("got status-types=%v, want [unread pinned]", got)
}
if got := r.URL.Query().Get("all"); got != "true" {
t.Errorf("got all=%q, want true", got)
}
if got := r.URL.Query().Get("to-status"); got != "read" {
t.Errorf("got to-status=%q, want read", got)
}
if got := r.URL.Query().Get("last_read_at"); got != lastReadAt.Format(time.RFC3339) {
t.Errorf("got last_read_at=%q, want %q", got, lastReadAt.Format(time.RFC3339))
}
w.WriteHeader(http.StatusResetContent)
json.NewEncoder(w).Encode([]types.NotificationThread{
{ID: 7, Unread: false, Subject: &types.NotificationSubject{Title: "Pinned release"}},
{ID: 8, Unread: false, Subject: &types.NotificationSubject{Title: "New docs"}},
})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
threads, err := f.Notifications.MarkRepoNotifications(context.Background(), "core", "go-forge", &NotificationRepoMarkOptions{
All: true,
StatusTypes: []string{"unread", "pinned"},
ToStatus: "read",
LastReadAt: &lastReadAt,
})
if err != nil {
t.Fatal(err)
}
if len(threads) != 2 {
t.Fatalf("got %d threads, want 2", len(threads))
}
if threads[0].ID != 7 || threads[1].ID != 8 {
t.Fatalf("got ids=%d,%d want 7,8", threads[0].ID, threads[1].ID)
}
if threads[0].Subject.Title != "Pinned release" {
t.Errorf("got title=%q, want %q", threads[0].Subject.Title, "Pinned release")
}
}
func TestNotificationService_NotFound_Bad(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)