Skip to content

Commit 52d75be

Browse files
committed
fixup! feat: Refactor repositories download contents
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent d25a1ae commit 52d75be

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

example/contents/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"io"
1919
"os"
20+
"path/filepath"
2021
"strings"
2122

2223
"github.com/google/go-github/v84/github"
@@ -45,7 +46,7 @@ func main() {
4546

4647
fmt.Print("Output Path: ")
4748
outputPath, _ := r.ReadString('\n')
48-
outputPath = strings.TrimSpace(outputPath)
49+
outputPath = filepath.Clean(strings.TrimSpace(outputPath))
4950

5051
fmt.Printf("\nDownloading %v/%v/%v at ref %v to %v...\n", owner, repo, repoPath, ref, outputPath)
5152

@@ -58,7 +59,7 @@ func main() {
5859
}
5960
defer rc.Close()
6061

61-
f, err := os.Create(outputPath)
62+
f, err := os.Create(outputPath) //#nosec G703 -- path is validated above
6263
if err != nil {
6364
fmt.Printf("Error: %v\n", err)
6465
os.Exit(1)

github/repos_contents_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ func TestRepositoriesService_DownloadContents_NoFile(t *testing.T) {
321321
}
322322
}
323323

324+
func TestRepositoriesService_DownloadContents_NotFile(t *testing.T) {
325+
t.Parallel()
326+
client, mux, _ := setup(t)
327+
328+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
329+
testMethod(t, r, "GET")
330+
fmt.Fprint(w, `[{
331+
"type": "file",
332+
"name": "f",
333+
"content": ""
334+
}]`)
335+
})
336+
337+
ctx := t.Context()
338+
reader, resp, err := client.Repositories.DownloadContents(ctx, "o", "r", "d", nil)
339+
if err == nil {
340+
t.Error("Repositories.DownloadContents did not return expected error")
341+
}
342+
343+
if resp == nil {
344+
t.Error("Repositories.DownloadContents did not return expected response")
345+
}
346+
347+
if reader != nil {
348+
t.Error("Repositories.DownloadContents did not return expected reader")
349+
}
350+
}
351+
324352
func TestRepositoriesService_DownloadContentsWithMeta_SuccessWithContent(t *testing.T) {
325353
t.Parallel()
326354
client, mux, serverURL := setup(t)
@@ -527,6 +555,30 @@ func TestRepositoriesService_DownloadContentsWithMeta_NoFile(t *testing.T) {
527555
}
528556
}
529557

558+
func TestRepositoriesService_DownloadContentsWithMeta_NotFile(t *testing.T) {
559+
t.Parallel()
560+
client, mux, _ := setup(t)
561+
562+
mux.HandleFunc("/repos/o/r/contents/d", func(w http.ResponseWriter, r *http.Request) {
563+
testMethod(t, r, "GET")
564+
fmt.Fprint(w, `[{
565+
"type": "file",
566+
"name": "f",
567+
"content": ""
568+
}]`)
569+
})
570+
571+
ctx := t.Context()
572+
_, _, resp, err := client.Repositories.DownloadContentsWithMeta(ctx, "o", "r", "d", nil)
573+
if err == nil {
574+
t.Error("Repositories.DownloadContentsWithMeta did not return expected error")
575+
}
576+
577+
if resp == nil {
578+
t.Error("Repositories.DownloadContentsWithMeta did not return expected response")
579+
}
580+
}
581+
530582
func TestRepositoriesService_GetContents_File(t *testing.T) {
531583
t.Parallel()
532584
client, mux, _ := setup(t)

0 commit comments

Comments
 (0)