diff --git a/docs/api-contract.md b/docs/api-contract.md index 1689e11..7d785f5 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -188,6 +188,7 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | PullService.UndismissReview | `func (s *PullService) UndismissReview(ctx context.Context, owner, repo string, index, reviewID int64) error` | UndismissReview undismisses a pull request review. | No direct tests. | | method | PullService.Update | `func (s *PullService) Update(ctx context.Context, owner, repo string, index int64) error` | Update updates a pull request branch with the base branch. | No direct tests. | | method | ReleaseService.DeleteAsset | `func (s *ReleaseService) DeleteAsset(ctx context.Context, owner, repo string, releaseID, assetID int64) error` | DeleteAsset deletes a single asset from a release. | No direct tests. | +| method | ReleaseService.EditAsset | `func (s *ReleaseService) EditAsset(ctx context.Context, owner, repo string, releaseID, attachmentID int64, opts *types.EditAttachmentOptions) (*types.Attachment, error)` | EditAsset updates a release asset. | `TestReleaseService_EditAttachment_Good` | | method | ReleaseService.DeleteByTag | `func (s *ReleaseService) DeleteByTag(ctx context.Context, owner, repo, tag string) error` | DeleteByTag deletes a release by its tag name. | No direct tests. | | method | ReleaseService.GetAsset | `func (s *ReleaseService) GetAsset(ctx context.Context, owner, repo string, releaseID, assetID int64) (*types.Attachment, error)` | GetAsset returns a single asset for a release. | No direct tests. | | method | ReleaseService.GetByTag | `func (s *ReleaseService) GetByTag(ctx context.Context, owner, repo, tag string) (*types.Release, error)` | GetByTag returns a release by its tag name. | `TestReleaseService_Good_GetByTag` | diff --git a/releases.go b/releases.go index 4d833fd..66e5ef4 100644 --- a/releases.go +++ b/releases.go @@ -96,11 +96,26 @@ func (s *ReleaseService) CreateAttachment(ctx context.Context, owner, repo strin return &out, nil } +// EditAttachment updates a release attachment. +func (s *ReleaseService) EditAttachment(ctx context.Context, owner, repo string, releaseID, attachmentID int64, opts *types.EditAttachmentOptions) (*types.Attachment, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/releases/{id}/assets/{attachment_id}", pathParams("owner", owner, "repo", repo, "id", int64String(releaseID), "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 +} + // CreateAsset uploads a new asset to a release. func (s *ReleaseService) CreateAsset(ctx context.Context, owner, repo string, releaseID int64, opts *ReleaseAttachmentUploadOptions, filename string, content io.Reader) (*types.Attachment, error) { return s.CreateAttachment(ctx, owner, repo, releaseID, opts, filename, content) } +// EditAsset updates a release asset. +func (s *ReleaseService) EditAsset(ctx context.Context, owner, repo string, releaseID, attachmentID int64, opts *types.EditAttachmentOptions) (*types.Attachment, error) { + return s.EditAttachment(ctx, owner, repo, releaseID, attachmentID, opts) +} + // IterAssets returns an iterator over all assets for a release. func (s *ReleaseService) IterAssets(ctx context.Context, owner, repo string, releaseID int64) iter.Seq2[types.Attachment, error] { path := ResolvePath("/api/v1/repos/{owner}/{repo}/releases/{releaseID}/assets", pathParams("owner", owner, "repo", repo, "releaseID", int64String(releaseID))) diff --git a/releases_test.go b/releases_test.go index 1779aa2..cfe8380 100644 --- a/releases_test.go +++ b/releases_test.go @@ -247,3 +247,32 @@ func TestReleaseService_CreateAttachmentExternalURL_Good(t *testing.T) { t.Fatalf("got name=%q", attachment.Name) } } + +func TestReleaseService_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/releases/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 != "release-notes.pdf" { + 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.Releases.EditAttachment(context.Background(), "core", "go-forge", 1, 4, &types.EditAttachmentOptions{Name: "release-notes.pdf"}) + if err != nil { + t.Fatal(err) + } + if attachment.Name != "release-notes.pdf" { + t.Fatalf("got name=%q", attachment.Name) + } +}