Skip to content

Commit

Permalink
Add github function to download assets
Browse files Browse the repository at this point in the history
- Also refactor to allow getting a tagged version
  • Loading branch information
zachgersh committed Aug 4, 2015
1 parent 2039bb6 commit b359439
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 124 deletions.
2 changes: 1 addition & 1 deletion check_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"

"github.com/blang/semver"
"github.com/google/go-github/github"
"github.com/zachgersh/go-github/github"
)

type CheckCommand struct {
Expand Down
2 changes: 1 addition & 1 deletion check_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/google/go-github/github"
"github.com/zachgersh/go-github/github"

"github.com/concourse/github-release-resource"
"github.com/concourse/github-release-resource/fakes"
Expand Down
131 changes: 124 additions & 7 deletions fakes/fake_git_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@
package fakes

import (
"io"
"os"
"sync"

"github.com/concourse/github-release-resource"
"github.com/google/go-github/github"
"github.com/zachgersh/go-github/github"
)

type FakeGitHub struct {
ListReleasesStub func() ([]github.RepositoryRelease, error)
listReleasesMutex sync.RWMutex
listReleasesArgsForCall []struct{}
listReleasesReturns struct {
listReleasesReturns struct {
result1 []github.RepositoryRelease
result2 error
}
LatestReleaseStub func() (*github.RepositoryRelease, error)
latestReleaseMutex sync.RWMutex
latestReleaseArgsForCall []struct{}
latestReleaseReturns struct {
result1 *github.RepositoryRelease
result2 error
}
GetReleaseByTagStub func(tag string) (*github.RepositoryRelease, error)
getReleaseByTagMutex sync.RWMutex
getReleaseByTagArgsForCall []struct {
tag string
}
getReleaseByTagReturns struct {
result1 *github.RepositoryRelease
result2 error
}
CreateReleaseStub func(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
createReleaseMutex sync.RWMutex
createReleaseArgsForCall []struct {
Expand All @@ -35,10 +52,10 @@ type FakeGitHub struct {
result1 *github.RepositoryRelease
result2 error
}
ListReleaseAssetsStub func(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
ListReleaseAssetsStub func(release github.RepositoryRelease) ([]github.ReleaseAsset, error)
listReleaseAssetsMutex sync.RWMutex
listReleaseAssetsArgsForCall []struct {
release *github.RepositoryRelease
release github.RepositoryRelease
}
listReleaseAssetsReturns struct {
result1 []github.ReleaseAsset
Expand All @@ -62,6 +79,15 @@ type FakeGitHub struct {
deleteReleaseAssetReturns struct {
result1 error
}
DownloadReleaseAssetStub func(asset *github.ReleaseAsset) (io.ReadCloser, error)
downloadReleaseAssetMutex sync.RWMutex
downloadReleaseAssetArgsForCall []struct {
asset *github.ReleaseAsset
}
downloadReleaseAssetReturns struct {
result1 io.ReadCloser
result2 error
}
}

func (fake *FakeGitHub) ListReleases() ([]github.RepositoryRelease, error) {
Expand Down Expand Up @@ -89,6 +115,64 @@ func (fake *FakeGitHub) ListReleasesReturns(result1 []github.RepositoryRelease,
}{result1, result2}
}

func (fake *FakeGitHub) LatestRelease() (*github.RepositoryRelease, error) {
fake.latestReleaseMutex.Lock()
fake.latestReleaseArgsForCall = append(fake.latestReleaseArgsForCall, struct{}{})
fake.latestReleaseMutex.Unlock()
if fake.LatestReleaseStub != nil {
return fake.LatestReleaseStub()
} else {
return fake.latestReleaseReturns.result1, fake.latestReleaseReturns.result2
}
}

func (fake *FakeGitHub) LatestReleaseCallCount() int {
fake.latestReleaseMutex.RLock()
defer fake.latestReleaseMutex.RUnlock()
return len(fake.latestReleaseArgsForCall)
}

func (fake *FakeGitHub) LatestReleaseReturns(result1 *github.RepositoryRelease, result2 error) {
fake.LatestReleaseStub = nil
fake.latestReleaseReturns = struct {
result1 *github.RepositoryRelease
result2 error
}{result1, result2}
}

func (fake *FakeGitHub) GetReleaseByTag(tag string) (*github.RepositoryRelease, error) {
fake.getReleaseByTagMutex.Lock()
fake.getReleaseByTagArgsForCall = append(fake.getReleaseByTagArgsForCall, struct {
tag string
}{tag})
fake.getReleaseByTagMutex.Unlock()
if fake.GetReleaseByTagStub != nil {
return fake.GetReleaseByTagStub(tag)
} else {
return fake.getReleaseByTagReturns.result1, fake.getReleaseByTagReturns.result2
}
}

func (fake *FakeGitHub) GetReleaseByTagCallCount() int {
fake.getReleaseByTagMutex.RLock()
defer fake.getReleaseByTagMutex.RUnlock()
return len(fake.getReleaseByTagArgsForCall)
}

func (fake *FakeGitHub) GetReleaseByTagArgsForCall(i int) string {
fake.getReleaseByTagMutex.RLock()
defer fake.getReleaseByTagMutex.RUnlock()
return fake.getReleaseByTagArgsForCall[i].tag
}

func (fake *FakeGitHub) GetReleaseByTagReturns(result1 *github.RepositoryRelease, result2 error) {
fake.GetReleaseByTagStub = nil
fake.getReleaseByTagReturns = struct {
result1 *github.RepositoryRelease
result2 error
}{result1, result2}
}

func (fake *FakeGitHub) CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
fake.createReleaseMutex.Lock()
fake.createReleaseArgsForCall = append(fake.createReleaseArgsForCall, struct {
Expand Down Expand Up @@ -155,10 +239,10 @@ func (fake *FakeGitHub) UpdateReleaseReturns(result1 *github.RepositoryRelease,
}{result1, result2}
}

func (fake *FakeGitHub) ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error) {
func (fake *FakeGitHub) ListReleaseAssets(release github.RepositoryRelease) ([]github.ReleaseAsset, error) {
fake.listReleaseAssetsMutex.Lock()
fake.listReleaseAssetsArgsForCall = append(fake.listReleaseAssetsArgsForCall, struct {
release *github.RepositoryRelease
release github.RepositoryRelease
}{release})
fake.listReleaseAssetsMutex.Unlock()
if fake.ListReleaseAssetsStub != nil {
Expand All @@ -174,7 +258,7 @@ func (fake *FakeGitHub) ListReleaseAssetsCallCount() int {
return len(fake.listReleaseAssetsArgsForCall)
}

func (fake *FakeGitHub) ListReleaseAssetsArgsForCall(i int) *github.RepositoryRelease {
func (fake *FakeGitHub) ListReleaseAssetsArgsForCall(i int) github.RepositoryRelease {
fake.listReleaseAssetsMutex.RLock()
defer fake.listReleaseAssetsMutex.RUnlock()
return fake.listReleaseAssetsArgsForCall[i].release
Expand Down Expand Up @@ -254,4 +338,37 @@ func (fake *FakeGitHub) DeleteReleaseAssetReturns(result1 error) {
}{result1}
}

func (fake *FakeGitHub) DownloadReleaseAsset(asset *github.ReleaseAsset) (io.ReadCloser, error) {
fake.downloadReleaseAssetMutex.Lock()
fake.downloadReleaseAssetArgsForCall = append(fake.downloadReleaseAssetArgsForCall, struct {
asset *github.ReleaseAsset
}{asset})
fake.downloadReleaseAssetMutex.Unlock()
if fake.DownloadReleaseAssetStub != nil {
return fake.DownloadReleaseAssetStub(asset)
} else {
return fake.downloadReleaseAssetReturns.result1, fake.downloadReleaseAssetReturns.result2
}
}

func (fake *FakeGitHub) DownloadReleaseAssetCallCount() int {
fake.downloadReleaseAssetMutex.RLock()
defer fake.downloadReleaseAssetMutex.RUnlock()
return len(fake.downloadReleaseAssetArgsForCall)
}

func (fake *FakeGitHub) DownloadReleaseAssetArgsForCall(i int) *github.ReleaseAsset {
fake.downloadReleaseAssetMutex.RLock()
defer fake.downloadReleaseAssetMutex.RUnlock()
return fake.downloadReleaseAssetArgsForCall[i].asset
}

func (fake *FakeGitHub) DownloadReleaseAssetReturns(result1 io.ReadCloser, result2 error) {
fake.DownloadReleaseAssetStub = nil
fake.downloadReleaseAssetReturns = struct {
result1 io.ReadCloser
result2 error
}{result1, result2}
}

var _ resource.GitHub = new(FakeGitHub)
45 changes: 43 additions & 2 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package resource

import (
"errors"
"io"
"net/url"
"os"

"code.google.com/p/goauth2/oauth"

"github.com/google/go-github/github"
"github.com/zachgersh/go-github/github"
)

//go:generate counterfeiter . GitHub

type GitHub interface {
ListReleases() ([]github.RepositoryRelease, error)
LatestRelease() (*github.RepositoryRelease, error)
GetReleaseByTag(tag string) (*github.RepositoryRelease, error)
CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
UpdateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error)

ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
ListReleaseAssets(release github.RepositoryRelease) ([]github.ReleaseAsset, error)
UploadReleaseAsset(release *github.RepositoryRelease, name string, file *os.File) error
DeleteReleaseAsset(asset github.ReleaseAsset) error
DownloadReleaseAsset(asset *github.ReleaseAsset) (io.ReadCloser, error)
}

type GitHubClient struct {
Expand Down Expand Up @@ -73,6 +77,34 @@ func (g *GitHubClient) ListReleases() ([]github.RepositoryRelease, error) {
return releases, nil
}

func (g *GitHubClient) LatestRelease() (*github.RepositoryRelease, error) {
latest, res, err := g.client.Repositories.GetLatestRelease(g.user, g.repository)
if err != nil {
return &github.RepositoryRelease{}, nil
}

err = res.Body.Close()
if err != nil {
return nil, err
}

return latest, nil
}

func (g *GitHubClient) GetReleaseByTag(tag string) (*github.RepositoryRelease, error) {
release, res, err := g.client.Repositories.GetReleaseByTag(g.user, g.repository, tag)
if err != nil {
return &github.RepositoryRelease{}, nil
}

err = res.Body.Close()
if err != nil {
return nil, err
}

return release, nil
}

func (g *GitHubClient) CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
createdRelease, res, err := g.client.Repositories.CreateRelease(g.user, g.repository, release)
if err != nil {
Expand Down Expand Up @@ -144,3 +176,12 @@ func (g *GitHubClient) DeleteReleaseAsset(asset github.ReleaseAsset) error {

return res.Body.Close()
}

func (g *GitHubClient) DownloadReleaseAsset(asset github.ReleaseAsset) (io.ReadCloser, error) {
res, err := g.client.Repositories.DownloadReleaseAsset(g.user, g.repository, *asset.ID)
if err != nil {
return nil, err
}

return res, err
}
47 changes: 19 additions & 28 deletions in_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"

"github.com/google/go-github/github"
"github.com/zachgersh/go-github/github"
)

type InCommand struct {
Expand All @@ -31,32 +29,26 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
return InResponse{}, err
}

releases, err := c.github.ListReleases()
if err != nil {
return InResponse{}, err
}

sort.Sort(byVersion(releases))

if len(releases) == 0 {
return InResponse{}, errors.New("no releases")
}

var foundRelease *github.RepositoryRelease

if request.Version == nil {
foundRelease = &releases[len(releases)-1]
var err error

foundRelease, err = c.github.LatestRelease()
if err != nil {
return InResponse{}, err
}
} else {
for _, release := range releases {
if *release.TagName == request.Version.Tag {
foundRelease = &release
break
}
var err error

foundRelease, err = c.github.GetReleaseByTag(request.Version.Tag)
if err != nil {
return InResponse{}, err
}
}

if foundRelease == nil {
return InResponse{}, fmt.Errorf("could not find release with tag: %s", request.Version.Tag)
return InResponse{}, errors.New("no releases")
}

tagPath := filepath.Join(destDir, "tag")
Expand All @@ -72,13 +64,12 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
return InResponse{}, err
}

assets, err := c.github.ListReleaseAssets(foundRelease)
assets, err := c.github.ListReleaseAssets(*foundRelease)
if err != nil {
return InResponse{}, err
}

for _, asset := range assets {
url := *asset.BrowserDownloadURL
path := filepath.Join(destDir, *asset.Name)

var matchFound bool
Expand All @@ -104,7 +95,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {

fmt.Fprintf(c.writer, "downloading asset: %s\n", *asset.Name)

err := c.downloadFile(url, path)
err := c.downloadFile(&asset, path)
if err != nil {
return InResponse{}, err
}
Expand All @@ -118,20 +109,20 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
}, nil
}

func (c *InCommand) downloadFile(url, destPath string) error {
func (c *InCommand) downloadFile(asset *github.ReleaseAsset, destPath string) error {
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()

resp, err := http.Get(url)
content, err := c.github.DownloadReleaseAsset(asset)
if err != nil {
return err
}
defer resp.Body.Close()
defer content.Close()

_, err = io.Copy(out, resp.Body)
_, err = io.Copy(out, content)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b359439

Please sign in to comment.