Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Defer release edit api.Client migration
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cd6661ad-7e77-46d0-b0e7-0a296f4e252b
  • Loading branch information
williammartin and Copilot committed Aug 5, 2026
commit 55ad01b17dda26e5a6d22d41cfba0f581800231e
3 changes: 1 addition & 2 deletions pkg/cmd/release/edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ func Test_editRun(t *testing.T) {
defer fakeHTTP.Verify(t)
shared.StubFetchRelease(t, fakeHTTP, "OWNER", "REPO", "v1.2.3", `{
"id": 12345,
"tag_name": "v1.2.3",
"url": "https://api.github.com/repos/OWNER/REPO/releases/12345"
"tag_name": "v1.2.3"
}`)
if tt.httpStubs != nil {
tt.httpStubs(t, fakeHTTP)
Expand Down
78 changes: 24 additions & 54 deletions pkg/cmd/release/edit/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,51 @@ import (
"strconv"

"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/safeurl"
"github.com/cli/cli/v2/pkg/cmd/release/shared"
"github.com/shurcooL/githubv4"
)

// responseTracker is a http.RoundTripper that records the status code of each
// response and wraps the body to catch non-EOF read errors. editRelease uses it
// to restore the phase-dependent return shape (nil pointer for pre-decode errors,
// non-nil for any post-response outcome) without bypassing the shared REST client.
type responseTracker struct {
transport http.RoundTripper
statusCode int
bodyReadErr error
}

func (t *responseTracker) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.transport.RoundTrip(req)
if resp != nil {
t.statusCode = resp.StatusCode
t.bodyReadErr = nil
if resp.Body != nil {
resp.Body = &bodyReadTracker{ReadCloser: resp.Body, tracker: t}
}
}
return resp, err
}

// bodyReadTracker wraps a response body and records the most recent non-EOF read error.
type bodyReadTracker struct {
io.ReadCloser
tracker *responseTracker
}

func (b *bodyReadTracker) Read(p []byte) (int, error) {
n, err := b.ReadCloser.Read(p)
if err != nil && err != io.EOF {
b.tracker.bodyReadErr = err
}
return n, err
}

func editRelease(httpClient *http.Client, repo ghrepo.Interface, releaseID int64, params map[string]interface{}) (*shared.Release, error) {
bodyBytes, err := json.Marshal(params)
if err != nil {
return nil, err
}

path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "releases", strconv.FormatInt(releaseID, 10))
url, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(repo.RepoHost()), "repos", repo.RepoOwner(), repo.RepoName(), "releases", strconv.FormatInt(releaseID, 10))
if err != nil {
return nil, err
}

transport := httpClient.Transport
if transport == nil {
transport = http.DefaultTransport
req, err := http.NewRequest("PATCH", url.String(), bytes.NewBuffer(bodyBytes))
if err != nil {
return nil, err
}
tracker := &responseTracker{transport: transport}
clone := *httpClient
clone.Transport = tracker

newRelease := &shared.Release{}
req.Header.Set("Content-Type", "application/json; charset=utf-8")

// TODO(api-client-rollout)
// This line of code is part of a mechanical roll out of the api client.
// As a follow up, consider whether the api client can be injected to this call site, rather than constructed
err = api.NewClientFromHTTP(&clone).REST(repo.RepoHost(), "PATCH", path.String(), bytes.NewReader(bodyBytes), newRelease)
// Return nil only when there is an actual error AND no 2xx response was decoded.
// status 0 + nil error (impossible in practice) falls through to non-nil release.
if err != nil && (tracker.statusCode == 0 || tracker.statusCode < 200 || tracker.statusCode >= 300 || tracker.bodyReadErr != nil) {
// This has been deferred from moving to api.Client because its return shape depends on the response status code, which api.Client.REST does not expose on success.
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if tracker.statusCode == http.StatusNoContent || tracker.statusCode == http.StatusResetContent {
return newRelease, json.Unmarshal(nil, newRelease)
defer resp.Body.Close()

success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
return nil, api.HandleHTTPError(resp)
}

b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return newRelease, err

var newRelease shared.Release
err = json.Unmarshal(b, &newRelease)
return &newRelease, err
}

func remoteTagExists(httpClient *http.Client, repo ghrepo.Interface, tagName string) (bool, error) {
Expand Down