Skip to content

Commit

Permalink
Return FileInfo in DownloadFiles (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjamet committed Jan 27, 2020
1 parent 1a53c08 commit dd77f91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 13 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ func authorize(r *http.Request) {

type Matcher func(path string) bool

type RepositoryFile struct {
Path string
FileInfo os.FileInfo
Data []byte
}

// DownloadSelectedRepositoryFiles downloads files from a given repository and granch, given that their name matches regarding the `include` function
func DownloadSelectedRepositoryFiles(c *http.Client, owner, repo, branch string, include Matcher) map[string][]byte {
func DownloadSelectedRepositoryFiles(c *http.Client, owner, repo, branch string, include Matcher) map[string]RepositoryFile {
u := fmt.Sprintf("https://api.github.com/repos/%s/%s/tarball/%s", owner, repo, branch)
core.Debugf("Downloading tarball for repo: %s", u)
req, err := http.NewRequest("GET", u, nil)
Expand Down Expand Up @@ -81,7 +87,7 @@ func DownloadSelectedRepositoryFiles(c *http.Client, owner, repo, branch string,
return nil
}
}
files := map[string][]byte{}
files := map[string]RepositoryFile{}
tr := tar.NewReader(body)
for {
hdr, err := tr.Next()
Expand All @@ -103,7 +109,11 @@ func DownloadSelectedRepositoryFiles(c *http.Client, owner, repo, branch string,
core.Warningf("failed to download repository: %v", err)
return nil
}
files[name] = b.Bytes()
files[name] = RepositoryFile{
Path: name,
FileInfo: hdr.FileInfo(),
Data: b.Bytes(),
}
}
}
return files
Expand Down
4 changes: 3 additions & 1 deletion github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func TestClient(t *testing.T) {

func TestDownload(t *testing.T) {
files := github.DownloadSelectedRepositoryFiles(http.DefaultClient, "actions-go", "toolkit", "09edac1c7d93e0dd7fe5a14dc410fb0b41ea01c4", github.MatchesOneOf("^module.go$"))
assert.Equal(t, map[string][]byte{"module.go": []byte(content)}, files)
assert.Len(t, files, 1)
assert.Equal(t, "module.go", files["module.go"].Path)
assert.Equal(t, []byte(content), files["module.go"].Data)
}

func TestMatchOneOf(t *testing.T) {
Expand Down

0 comments on commit dd77f91

Please sign in to comment.