Skip to content

fix: DownloadReleaseAsset handles renamed repository #3392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions github/repos_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,10 @@ func (s *RepositoriesService) GetReleaseAsset(ctx context.Context, owner, repo s
// of the io.ReadCloser. Exactly one of rc and redirectURL will be zero.
//
// followRedirectsClient can be passed to download the asset from a redirected
// location. Passing http.DefaultClient is recommended unless special circumstances
// exist, but it's possible to pass any http.Client. If nil is passed the
// redirectURL will be returned instead.
// location. Specifying any http.Client is possible, but passing http.DefaultClient
// is recommended, except when the specified repository is private, in which case
// it's necessary to pass an http.Client that performs authenticated requests.
// If nil is passed the redirectURL will be returned instead.
//
// GitHub API docs: https://docs.github.com/rest/releases/assets#get-a-release-asset
//
Expand Down Expand Up @@ -387,7 +388,7 @@ func (s *RepositoriesService) downloadReleaseAssetFromURL(ctx context.Context, f
return nil, err
}
req = withContext(ctx, req)
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept", defaultMediaType)
resp, err := followRedirectsClient.Do(req)
if err != nil {
return nil, err
Expand Down
46 changes: 44 additions & 2 deletions github/repos_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
})
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", "*/*")
testHeader(t, r, "Accept", defaultMediaType)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=hello-world.txt")
fmt.Fprint(w, "Hello World")
Expand All @@ -511,6 +511,48 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
}
}

func TestRepositoriesService_DownloadReleaseAsset_FollowMultipleRedirects(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
testMethod(t, r, "GET")
testHeader(t, r, "Accept", defaultMediaType)
// /yo, below will be served as baseURLPath/yo
http.Redirect(w, r, baseURLPath+"/yo", http.StatusMovedPermanently)
})
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
testMethod(t, r, "GET")
testHeader(t, r, "Accept", defaultMediaType)
// /yo2, below will be served as baseURLPath/yo2
http.Redirect(w, r, baseURLPath+"/yo2", http.StatusFound)
})
mux.HandleFunc("/yo2", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=hello-world.txt")
testMethod(t, r, "GET")
testHeader(t, r, "Accept", defaultMediaType)
fmt.Fprint(w, "Hello World")
})

ctx := context.Background()
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
if err != nil {
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
}
content, err := io.ReadAll(reader)
if err != nil {
t.Errorf("Reading Repositories.DownloadReleaseAsset returned error: %v", err)
}
reader.Close()
want := []byte("Hello World")
if !bytes.Equal(want, content) {
t.Errorf("Repositories.DownloadReleaseAsset returned %+v, want %+v", content, want)
}
}

func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)
Expand All @@ -523,7 +565,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testi
})
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", "*/*")
testHeader(t, r, "Accept", defaultMediaType)
w.WriteHeader(http.StatusNotFound)
})

Expand Down
Loading