Skip to content

Commit

Permalink
fix: Fetching release assets properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Costas Papastathis authored and ForestEckhardt committed Feb 25, 2025
1 parent a57bf55 commit 08ee7c5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
3 changes: 2 additions & 1 deletion github/release_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type ReleaseService struct {
}

type ReleaseAsset struct {
URL string `json:"url"`
URL string `json:"url"`
Name string `json:"name"`
}

type Release struct {
Expand Down
14 changes: 10 additions & 4 deletions remote_buildpack.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package freezer

import "fmt"
import (
"fmt"
)

type RemoteBuildpack struct {
Org string
Repo string
Platform string
Arch string
UncachedKey string
CachedKey string
Offline bool
Version string
}

func NewRemoteBuildpack(org, repo string) RemoteBuildpack {
func NewRemoteBuildpack(org, repo, platform, arch string) RemoteBuildpack {
return RemoteBuildpack{
Org: org,
Repo: repo,
UncachedKey: fmt.Sprintf("%s:%s", org, repo),
CachedKey: fmt.Sprintf("%s:%s:cached", org, repo),
Platform: platform,
Arch: arch,
UncachedKey: fmt.Sprintf("%s:%s:%s:%s", org, repo, platform, arch),
CachedKey: fmt.Sprintf("%s:%s:%s:%s:cached", org, repo, platform, arch),
}
}
25 changes: 23 additions & 2 deletions remote_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r RemoteFetcher) Get(buildpack RemoteBuildpack) (string, error) {
return "", err
}

buildpackCacheDir := filepath.Join(r.buildpackCache.Dir(), buildpack.Org, buildpack.Repo)
buildpackCacheDir := filepath.Join(r.buildpackCache.Dir(), buildpack.Org, buildpack.Repo, buildpack.Platform, buildpack.Arch)
if buildpack.Offline {
buildpackCacheDir = filepath.Join(buildpackCacheDir, "cached")
}
Expand Down Expand Up @@ -95,11 +95,32 @@ func (r RemoteFetcher) Get(buildpack RemoteBuildpack) (string, error) {
if err != nil {
return "", err
}
} else {
} else if buildpack.Platform == "linux" && buildpack.Arch == "amd64" && len(release.Assets) == 2 {
//This if is for backward compatibility
bundle, err = r.gitReleaseFetcher.GetReleaseAsset(release.Assets[0])
if err != nil {
return "", err
}
} else {
var assetName string
if buildpack.Platform == "linux" && buildpack.Arch == "amd64" {
assetName = "" + buildpack.Repo + "-" + tagName + ".cnb"
} else {
assetName = "" + buildpack.Repo + "-" + tagName + "-" + buildpack.Platform + "-" + buildpack.Arch + ".cnb"
}

var downloadAssetIndex int
for i, asset := range release.Assets {
if asset.Name == assetName {
downloadAssetIndex = i
break
}
}

bundle, err = r.gitReleaseFetcher.GetReleaseAsset(release.Assets[downloadAssetIndex])
if err != nil {
return "", err
}
}

path = filepath.Join(buildpackCacheDir, fmt.Sprintf("%s.cnb", tagName))
Expand Down
54 changes: 27 additions & 27 deletions remote_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
}
buildpackCache.GetCall.Returns.Bool = true

remoteBuildpack = freezer.NewRemoteBuildpack("some-org", "some-repo")
remoteBuildpack = freezer.NewRemoteBuildpack("some-org", "some-repo", "some-platform", "some-arch")
remoteBuildpack.Offline = false
remoteBuildpack.Version = "some-version"

Expand Down Expand Up @@ -112,7 +112,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch"))

Expect(buildpackCache.SetCall.CallCount).To(Equal(0))

Expand All @@ -126,7 +126,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Version: "some-other-tag",
}

Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo"), os.ModePerm)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch"), os.ModePerm)).To(Succeed())
})

context("when there is a release artifact present", func() {
Expand All @@ -138,14 +138,14 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch"))

Expect(gitReleaseFetcher.GetReleaseAssetCall.Receives.Asset).To(Equal(github.ReleaseAsset{
URL: "some-url",
}))

Expect(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb"))
Expect(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb"))
Expect(err).ToNot(HaveOccurred())

err = vacation.NewArchive(file).Decompress(filepath.Join(cacheDir, "some-org", "some-repo"))
Expand All @@ -157,7 +157,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")))
})
})

Expand All @@ -170,18 +170,18 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:cached"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch:cached"))

Expect(gitReleaseFetcher.GetReleaseTarballCall.Receives.Url).To(Equal("some-tarball-url"))

Expect(packager.ExecuteCall.Receives.BuildpackDir).To(Equal(downloadDir))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "cached", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "cached", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Version).To(Equal("some-tag"))
Expect(packager.ExecuteCall.Receives.Cached).To(BeTrue())

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "cached", "some-tag.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "cached", "some-tag.cnb")))
})
})
})
Expand Down Expand Up @@ -240,20 +240,20 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch"))

Expect(gitReleaseFetcher.GetReleaseTarballCall.Receives.Url).To(Equal("some-tarball-url"))

Expect(packager.ExecuteCall.Receives.BuildpackDir).To(Equal(downloadDir))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Version).To(Equal("some-tag"))
Expect(packager.ExecuteCall.Receives.Cached).To(BeFalse())

Expect(packager.ExecuteCall.Returns.Error).To(BeNil())

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")))
})
})

Expand All @@ -266,20 +266,20 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:cached"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch:cached"))

Expect(gitReleaseFetcher.GetReleaseTarballCall.Receives.Url).To(Equal("some-tarball-url"))

Expect(packager.ExecuteCall.Receives.BuildpackDir).To(Equal(downloadDir))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "cached", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "cached", "some-tag.cnb")))
Expect(packager.ExecuteCall.Receives.Version).To(Equal("some-tag"))
Expect(packager.ExecuteCall.Receives.Cached).To(BeTrue())

Expect(packager.ExecuteCall.Returns.Error).To(BeNil())

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "cached", "some-tag.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "cached", "some-tag.cnb")))
})
})
})
Expand All @@ -303,8 +303,8 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
URL: "some-url",
}))

Expect(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb"))
Expect(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb"))
Expect(err).ToNot(HaveOccurred())

err = vacation.NewArchive(file).Decompress(filepath.Join(cacheDir, "some-org", "some-repo"))
Expand All @@ -316,7 +316,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-tag.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-tag.cnb")))
})
})

Expand All @@ -336,7 +336,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Version: "some-other-tag",
}

Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo"), os.ModePerm)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch"), os.ModePerm)).To(Succeed())
})

it("removes the v from the tag", func() {
Expand All @@ -346,26 +346,26 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
Expect(gitReleaseFetcher.GetCall.Receives.Org).To(Equal("some-org"))
Expect(gitReleaseFetcher.GetCall.Receives.Repo).To(Equal("some-repo"))

Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo"))
Expect(buildpackCache.GetCall.Receives.Key).To(Equal("some-org:some-repo:some-platform:some-arch"))

Expect(gitReleaseFetcher.GetReleaseAssetCall.Receives.Asset).To(Equal(github.ReleaseAsset{
URL: "some-url",
}))

Expect(filepath.Join(cacheDir, "some-org", "some-repo", "1.2.3.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "1.2.3.cnb"))
Expect(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "1.2.3.cnb")).To(BeAnExistingFile())
file, err := os.Open(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "1.2.3.cnb"))
Expect(err).ToNot(HaveOccurred())

err = vacation.NewArchive(file).Decompress(filepath.Join(cacheDir, "some-org", "some-repo"))
err = vacation.NewArchive(file).Decompress(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch"))
Expect(err).ToNot(HaveOccurred())

content, err := os.ReadFile(filepath.Join(cacheDir, "some-org", "some-repo", "some-file"))
content, err := os.ReadFile(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "some-file"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some content"))

Expect(buildpackCache.SetCall.CallCount).To(Equal(1))

Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "1.2.3.cnb")))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch", "1.2.3.cnb")))
})
})

Expand Down Expand Up @@ -481,7 +481,7 @@ func testRemoteFetcher(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
buildpackCache.SetCall.Returns.Error = errors.New("failed to set new cache entry")

Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo"), os.ModePerm)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(cacheDir, "some-org", "some-repo", "some-platform", "some-arch"), os.ModePerm)).To(Succeed())
})

it("returns an error", func() {
Expand Down

0 comments on commit 08ee7c5

Please sign in to comment.