Skip to content

Commit cf44be7

Browse files
committed
Refactored DownloadContents method
1 parent 2067d4b commit cf44be7

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

github/repos_contents.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,26 @@ func (s *RepositoriesService) GetReadme(ctx context.Context, owner, repo string,
139139
func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo, filepath string, opts *RepositoryContentGetOptions) (io.ReadCloser, *Response, error) {
140140
dir := path.Dir(filepath)
141141
filename := path.Base(filepath)
142+
fileContent, _, resp, err := s.GetContents(ctx, owner, repo, filepath, opts)
143+
if err != nil {
144+
return nil, resp, err
145+
}
146+
147+
if fileContent != nil {
148+
content, err := fileContent.GetContent()
149+
if err == nil && content != "" {
150+
return io.NopCloser(strings.NewReader(content)), resp, nil
151+
}
152+
}
153+
142154
_, dirContents, resp, err := s.GetContents(ctx, owner, repo, dir, opts)
143155
if err != nil {
144156
return nil, resp, err
145157
}
146158

147159
for _, contents := range dirContents {
148-
if *contents.Name == filename {
149-
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
160+
if contents.GetName() == filename {
161+
if contents.GetDownloadURL() == "" {
150162
return nil, resp, fmt.Errorf("no download link found for %s", filepath)
151163
}
152164

github/repos_contents_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,66 @@ func TestRepositoriesService_GetReadme(t *testing.T) {
127127
})
128128
}
129129

130-
func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
130+
func TestRepositoriesService_DownloadContents_SuccessForFile(t *testing.T) {
131131
t.Parallel()
132132
client, mux, serverURL := setup(t)
133133

134+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
135+
testMethod(t, r, "GET")
136+
fmt.Fprint(w, `{
137+
"type": "file",
138+
"name": "f",
139+
"content": "foo",
140+
"download_url": "`+serverURL+baseURLPath+`/download/f"
141+
}`)
142+
})
143+
144+
ctx := context.Background()
145+
r, resp, err := client.Repositories.DownloadContents(ctx, "o", "r", "d/f", nil)
146+
if err != nil {
147+
t.Errorf("Repositories.DownloadContents returned error: %v", err)
148+
}
149+
150+
if got, want := resp.Response.StatusCode, http.StatusOK; got != want {
151+
t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want)
152+
}
153+
154+
bytes, err := io.ReadAll(r)
155+
if err != nil {
156+
t.Errorf("Error reading response body: %v", err)
157+
}
158+
r.Close()
159+
160+
if got, want := string(bytes), "foo"; got != want {
161+
t.Errorf("Repositories.DownloadContents returned %v, want %v", got, want)
162+
}
163+
164+
const methodName = "DownloadContents"
165+
testBadOptions(t, methodName, func() (err error) {
166+
_, _, err = client.Repositories.DownloadContents(ctx, "\n", "\n", "\n", nil)
167+
return err
168+
})
169+
170+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
171+
got, resp, err := client.Repositories.DownloadContents(ctx, "o", "r", "d/f", nil)
172+
if got != nil {
173+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
174+
}
175+
return resp, err
176+
})
177+
}
178+
179+
func TestRepositoriesService_DownloadContents_SuccessForDirectory(t *testing.T) {
180+
t.Parallel()
181+
client, mux, serverURL := setup(t)
182+
183+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
184+
testMethod(t, r, "GET")
185+
fmt.Fprint(w, `{
186+
"type": "file",
187+
"name": "f"
188+
}`)
189+
})
134190
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
135191
testMethod(t, r, "GET")
136192
fmt.Fprint(w, `[{
@@ -183,6 +239,13 @@ func TestRepositoriesService_DownloadContents_FailedResponse(t *testing.T) {
183239
t.Parallel()
184240
client, mux, serverURL := setup(t)
185241

242+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
243+
testMethod(t, r, "GET")
244+
fmt.Fprint(w, `{
245+
"type": "file",
246+
"name": "f"
247+
}`)
248+
})
186249
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
187250
testMethod(t, r, "GET")
188251
fmt.Fprint(w, `[{
@@ -222,6 +285,14 @@ func TestRepositoriesService_DownloadContents_NoDownloadURL(t *testing.T) {
222285
t.Parallel()
223286
client, mux, _ := setup(t)
224287

288+
mux.HandleFunc("/repos/o/r/contents/d/f", func(w http.ResponseWriter, r *http.Request) {
289+
testMethod(t, r, "GET")
290+
fmt.Fprint(w, `{
291+
"type": "file",
292+
"name": "f",
293+
"content": ""
294+
}`)
295+
})
225296
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
226297
testMethod(t, r, "GET")
227298
fmt.Fprint(w, `[{

0 commit comments

Comments
 (0)