diff --git a/issues.go b/issues.go index 968218e..d0fa21e 100644 --- a/issues.go +++ b/issues.go @@ -235,6 +235,44 @@ func (s *IssueService) CreateComment(ctx context.Context, owner, repo string, in return &out, nil } +// ListAttachments returns all attachments on an issue. +func (s *IssueService) ListAttachments(ctx context.Context, owner, repo string, index int64) ([]types.Attachment, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/assets", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListAll[types.Attachment](ctx, s.client, path, nil) +} + +// IterAttachments returns an iterator over all attachments on an issue. +func (s *IssueService) IterAttachments(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.Attachment, error] { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/assets", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListIter[types.Attachment](ctx, s.client, path, nil) +} + +// GetAttachment returns a single attachment on an issue. +func (s *IssueService) GetAttachment(ctx context.Context, owner, repo string, index, attachmentID int64) (*types.Attachment, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/assets/{attachment_id}", pathParams("owner", owner, "repo", repo, "index", int64String(index), "attachment_id", int64String(attachmentID))) + var out types.Attachment + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + +// EditAttachment updates an issue attachment. +func (s *IssueService) EditAttachment(ctx context.Context, owner, repo string, index, attachmentID int64, opts *types.EditAttachmentOptions) (*types.Attachment, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/assets/{attachment_id}", pathParams("owner", owner, "repo", repo, "index", int64String(index), "attachment_id", int64String(attachmentID))) + var out types.Attachment + if err := s.client.Patch(ctx, path, opts, &out); err != nil { + return nil, err + } + return &out, nil +} + +// DeleteAttachment removes an issue attachment. +func (s *IssueService) DeleteAttachment(ctx context.Context, owner, repo string, index, attachmentID int64) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/assets/{attachment_id}", pathParams("owner", owner, "repo", repo, "index", int64String(index), "attachment_id", int64String(attachmentID))) + return s.client.Delete(ctx, path) +} + // ListTimeline returns all comments and events on an issue. func (s *IssueService) ListTimeline(ctx context.Context, owner, repo string, index int64, since, before *time.Time) ([]types.TimelineComment, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/timeline", pathParams("owner", owner, "repo", repo, "index", int64String(index))) diff --git a/issues_test.go b/issues_test.go index 487864d..429737d 100644 --- a/issues_test.go +++ b/issues_test.go @@ -318,6 +318,135 @@ func TestIssueService_CreateComment_Good(t *testing.T) { } } +func TestIssueService_ListAttachments_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/assets" { + 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.Attachment{ + {ID: 4, Name: "design.png"}, + {ID: 5, Name: "notes.txt"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + attachments, err := f.Issues.ListAttachments(context.Background(), "core", "go-forge", 1) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(attachments, []types.Attachment{{ID: 4, Name: "design.png"}, {ID: 5, Name: "notes.txt"}}) { + t.Fatalf("got %#v", attachments) + } +} + +func TestIssueService_IterAttachments_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/assets" { + 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.Attachment{{ID: 4, Name: "design.png"}}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + var seen []types.Attachment + for attachment, err := range f.Issues.IterAttachments(context.Background(), "core", "go-forge", 1) { + if err != nil { + t.Fatal(err) + } + seen = append(seen, attachment) + } + if !reflect.DeepEqual(seen, []types.Attachment{{ID: 4, Name: "design.png"}}) { + t.Fatalf("got %#v", seen) + } +} + +func TestIssueService_GetAttachment_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/assets/4" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + json.NewEncoder(w).Encode(types.Attachment{ID: 4, Name: "design.png"}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + attachment, err := f.Issues.GetAttachment(context.Background(), "core", "go-forge", 1, 4) + if err != nil { + t.Fatal(err) + } + if attachment.Name != "design.png" { + t.Fatalf("got name=%q", attachment.Name) + } +} + +func TestIssueService_EditAttachment_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPatch { + t.Errorf("expected PATCH, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1/assets/4" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + var body types.EditAttachmentOptions + json.NewDecoder(r.Body).Decode(&body) + if body.Name != "updated.png" { + t.Fatalf("got body=%#v", body) + } + json.NewEncoder(w).Encode(types.Attachment{ID: 4, Name: body.Name}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + attachment, err := f.Issues.EditAttachment(context.Background(), "core", "go-forge", 1, 4, &types.EditAttachmentOptions{Name: "updated.png"}) + if err != nil { + t.Fatal(err) + } + if attachment.Name != "updated.png" { + t.Fatalf("got name=%q", attachment.Name) + } +} + +func TestIssueService_DeleteAttachment_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/assets/4" { + 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.DeleteAttachment(context.Background(), "core", "go-forge", 1, 4); err != nil { + t.Fatal(err) + } +} + func TestIssueService_ListTimeline_Good(t *testing.T) { since := time.Date(2026, time.March, 1, 12, 30, 0, 0, time.UTC) before := time.Date(2026, time.March, 2, 12, 30, 0, 0, time.UTC)