Skip to content

Commit 386ce5a

Browse files
committed
Added DownloadContentsWithMeta for issue google#1809
Added an additional method and tests for callers to receive a RepositoryContent when downloading a large file from a GitHub repository. This simplifies future operations (e.g. UpdateFile) involving the requested file.
1 parent bff639e commit 386ce5a

3 files changed

Lines changed: 212 additions & 32 deletions

File tree

‎github/repos_contents.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,36 @@ func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo,
144144
return nil, resp, fmt.Errorf("No file named %s found in %s", filename, dir)
145145
}
146146

147+
// DownloadContentsWithMeta is identical to DownloadContents but additionally
148+
// returns the RepositoryContent of the requested file. This additional data
149+
// is useful for future operations involving the requested file. For merely
150+
// reading the content of a file, DownloadContents is perfectly adequate.
151+
//
152+
// It is possible for the download to result in a failed response when the
153+
// returned error is nil. Callers should check the returned Response status
154+
// code to verify the content is from a successful response.
155+
func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owner, repo, filepath string, opts *RepositoryContentGetOptions) (io.ReadCloser, *RepositoryContent, *Response, error) {
156+
dir := path.Dir(filepath)
157+
filename := path.Base(filepath)
158+
_, dirContents, resp, err := s.GetContents(ctx, owner, repo, dir, opts)
159+
if err != nil {
160+
return nil, nil, resp, err
161+
}
162+
for _, contents := range dirContents {
163+
if *contents.Name == filename {
164+
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
165+
return nil, contents, resp, fmt.Errorf("No download link found for %s", filepath)
166+
}
167+
dlResp, err := s.client.client.Get(*contents.DownloadURL)
168+
if err != nil {
169+
return nil, contents, &Response{Response: dlResp}, err
170+
}
171+
return dlResp.Body, contents, &Response{Response: dlResp}, nil
172+
}
173+
}
174+
return nil, nil, resp, fmt.Errorf("No file named %s found in %s", filename, dir)
175+
}
176+
147177
// GetContents can return either the metadata and content of a single file
148178
// (when path references a file) or the metadata of all the files and/or
149179
// subdirectories of a directory (when path references a directory). To make it

‎github/repos_contents_test.go‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,155 @@ func TestRepositoriesService_DownloadContents_NoFile(t *testing.T) {
247247
}
248248
}
249249

250+
func TestRepositoriesService_DownloadContentsWithMeta_Success(t *testing.T) {
251+
client, mux, serverURL, teardown := setup()
252+
defer teardown()
253+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
254+
testMethod(t, r, "GET")
255+
fmt.Fprint(w, `[{
256+
"type": "file",
257+
"name": "f",
258+
"download_url": "`+serverURL+baseURLPath+`/download/f"
259+
}]`)
260+
})
261+
mux.HandleFunc("/download/f", func(w http.ResponseWriter, r *http.Request) {
262+
testMethod(t, r, "GET")
263+
fmt.Fprint(w, "foo")
264+
})
265+
266+
ctx := context.Background()
267+
r, c, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
268+
if err != nil {
269+
t.Errorf("Repositories.DownloadContentsWithMeta returned error: %v", err)
270+
}
271+
272+
if got, want := resp.Response.StatusCode, http.StatusOK; got != want {
273+
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
274+
}
275+
276+
bytes, err := ioutil.ReadAll(r)
277+
if err != nil {
278+
t.Errorf("Error reading response body: %v", err)
279+
}
280+
r.Close()
281+
282+
if got, want := string(bytes), "foo"; got != want {
283+
t.Errorf("Repositories.DownloadContentsWithMeta returned %v, want %v", got, want)
284+
}
285+
286+
if c != nil && c.Name != nil {
287+
if got, want := *c.Name, "f"; got != want {
288+
t.Errorf("Repositories.DownloadContentsWithMeta returned content name %v, want %v", got, want)
289+
}
290+
} else {
291+
t.Errorf("Returned RepositoryContent is null")
292+
}
293+
294+
const methodName = "DownloadContentsWithMeta"
295+
testBadOptions(t, methodName, func() (err error) {
296+
_, _, _, err = client.Repositories.DownloadContentsWithMeta(ctx, "\n", "\n", "\n", nil)
297+
return err
298+
})
299+
300+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
301+
got, cot, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
302+
if got != nil {
303+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
304+
}
305+
if cot != nil {
306+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, cot)
307+
}
308+
return resp, err
309+
})
310+
}
311+
312+
func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing.T) {
313+
client, mux, serverURL, teardown := setup()
314+
defer teardown()
315+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
316+
testMethod(t, r, "GET")
317+
fmt.Fprint(w, `[{
318+
"type": "file",
319+
"name": "f",
320+
"download_url": "`+serverURL+baseURLPath+`/download/f"
321+
}]`)
322+
})
323+
mux.HandleFunc("/download/f", func(w http.ResponseWriter, r *http.Request) {
324+
testMethod(t, r, "GET")
325+
w.WriteHeader(http.StatusInternalServerError)
326+
fmt.Fprint(w, "foo error")
327+
})
328+
329+
ctx := context.Background()
330+
r, c, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
331+
if err != nil {
332+
t.Errorf("Repositories.DownloadContentsWithMeta returned error: %v", err)
333+
}
334+
335+
if got, want := resp.Response.StatusCode, http.StatusInternalServerError; got != want {
336+
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
337+
}
338+
339+
bytes, err := ioutil.ReadAll(r)
340+
if err != nil {
341+
t.Errorf("Error reading response body: %v", err)
342+
}
343+
r.Close()
344+
345+
if got, want := string(bytes), "foo error"; got != want {
346+
t.Errorf("Repositories.DownloadContentsWithMeta returned %v, want %v", got, want)
347+
}
348+
349+
if c != nil && c.Name != nil {
350+
if got, want := *c.Name, "f"; got != want {
351+
t.Errorf("Repositories.DownloadContentsWithMeta returned content name %v, want %v", got, want)
352+
}
353+
} else {
354+
t.Errorf("Returned RepositoryContent is null")
355+
}
356+
}
357+
358+
func TestRepositoriesService_DownloadContentsWithMeta_NoDownloadURL(t *testing.T) {
359+
client, mux, _, teardown := setup()
360+
defer teardown()
361+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
362+
testMethod(t, r, "GET")
363+
fmt.Fprint(w, `[{
364+
"type": "file",
365+
"name": "f",
366+
}]`)
367+
})
368+
369+
ctx := context.Background()
370+
_, _, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
371+
if err == nil {
372+
t.Errorf("Repositories.DownloadContentsWithMeta did not return expected error")
373+
}
374+
375+
if resp == nil {
376+
t.Errorf("Repositories.DownloadContentsWithMeta did not return expected response")
377+
}
378+
}
379+
380+
func TestRepositoriesService_DownloadContentsWithMeta_NoFile(t *testing.T) {
381+
client, mux, _, teardown := setup()
382+
defer teardown()
383+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
384+
testMethod(t, r, "GET")
385+
fmt.Fprint(w, `[]`)
386+
})
387+
388+
ctx := context.Background()
389+
_, _, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d/f", nil)
390+
if err == nil {
391+
t.Errorf("Repositories.DownloadContentsWithMeta did not return expected error")
392+
}
393+
394+
if resp == nil {
395+
t.Errorf("Repositories.DownloadContentsWithMeta did not return expected response")
396+
}
397+
}
398+
250399
func TestRepositoriesService_GetContents_File(t *testing.T) {
251400
client, mux, _, teardown := setup()
252401
defer teardown()

‎update-urls/main.go‎

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,39 @@ var (
5555
// skipMethods holds methods which are skipped because they do not have GitHub v3
5656
// API URLs or are otherwise problematic in parsing, discovering, and/or fixing.
5757
skipMethods = map[string]bool{
58-
"ActionsService.DownloadArtifact": true,
59-
"AdminService.CreateOrg": true,
60-
"AdminService.CreateUser": true,
61-
"AdminService.CreateUserImpersonation": true,
62-
"AdminService.DeleteUserImpersonation": true,
63-
"AdminService.GetAdminStats": true,
64-
"AdminService.RenameOrg": true,
65-
"AdminService.RenameOrgByName": true,
66-
"AdminService.UpdateTeamLDAPMapping": true,
67-
"AdminService.UpdateUserLDAPMapping": true,
68-
"AppsService.FindRepositoryInstallationByID": true,
69-
"AuthorizationsService.CreateImpersonation": true,
70-
"AuthorizationsService.DeleteImpersonation": true,
71-
"IssueImportService.CheckStatus": true,
72-
"IssueImportService.CheckStatusSince": true,
73-
"IssueImportService.Create": true,
74-
"MarketplaceService.marketplaceURI": true,
75-
"OrganizationsService.GetByID": true,
76-
"RepositoriesService.DeletePreReceiveHook": true,
77-
"RepositoriesService.DownloadContents": true,
78-
"RepositoriesService.GetArchiveLink": true,
79-
"RepositoriesService.GetByID": true,
80-
"RepositoriesService.GetPreReceiveHook": true,
81-
"RepositoriesService.ListPreReceiveHooks": true,
82-
"RepositoriesService.UpdatePreReceiveHook": true,
83-
"SearchService.search": true,
84-
"TeamsService.ListTeamMembersByID": true,
85-
"UsersService.DemoteSiteAdmin": true,
86-
"UsersService.GetByID": true,
87-
"UsersService.PromoteSiteAdmin": true,
88-
"UsersService.Suspend": true,
89-
"UsersService.Unsuspend": true,
58+
"ActionsService.DownloadArtifact": true,
59+
"AdminService.CreateOrg": true,
60+
"AdminService.CreateUser": true,
61+
"AdminService.CreateUserImpersonation": true,
62+
"AdminService.DeleteUserImpersonation": true,
63+
"AdminService.GetAdminStats": true,
64+
"AdminService.RenameOrg": true,
65+
"AdminService.RenameOrgByName": true,
66+
"AdminService.UpdateTeamLDAPMapping": true,
67+
"AdminService.UpdateUserLDAPMapping": true,
68+
"AppsService.FindRepositoryInstallationByID": true,
69+
"AuthorizationsService.CreateImpersonation": true,
70+
"AuthorizationsService.DeleteImpersonation": true,
71+
"IssueImportService.CheckStatus": true,
72+
"IssueImportService.CheckStatusSince": true,
73+
"IssueImportService.Create": true,
74+
"MarketplaceService.marketplaceURI": true,
75+
"OrganizationsService.GetByID": true,
76+
"RepositoriesService.DeletePreReceiveHook": true,
77+
"RepositoriesService.DownloadContents": true,
78+
"RepositoriesService.DownloadContentsWithMeta": true,
79+
"RepositoriesService.GetArchiveLink": true,
80+
"RepositoriesService.GetByID": true,
81+
"RepositoriesService.GetPreReceiveHook": true,
82+
"RepositoriesService.ListPreReceiveHooks": true,
83+
"RepositoriesService.UpdatePreReceiveHook": true,
84+
"SearchService.search": true,
85+
"TeamsService.ListTeamMembersByID": true,
86+
"UsersService.DemoteSiteAdmin": true,
87+
"UsersService.GetByID": true,
88+
"UsersService.PromoteSiteAdmin": true,
89+
"UsersService.Suspend": true,
90+
"UsersService.Unsuspend": true,
9091
}
9192

9293
helperOverrides = map[string]overrideFunc{

0 commit comments

Comments
 (0)