Skip to content

Commit 635e269

Browse files
committed
Merge branch 'main' into no-xerrors
2 parents ec65d52 + c4378bf commit 635e269

5 files changed

Lines changed: 88 additions & 27 deletions

File tree

.github/workflows/push.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
- run: go build github.com/loilo-inc/canarycage/cli/cage
2323
- run: go test ./... -coverprofile=coverage.txt -covermode=count
2424
- uses: codecov/codecov-action@v5
25+
# skip if pr from dependabot, as it doesn't have access to secrets
26+
if: github.actor != 'dependabot[bot]'
2527
env:
2628
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2729
with:

cli/cage/upgrade/upgrade.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,12 @@ func (u *upgrader) Upgrade(ctx context.Context) error {
5252
// ignore current version if it's not a valid semver
5353
l.Infof("upgrading from %s to %s", u.CurrentVersion, latestRelease.GetTagName())
5454
var version = latestRelease.GetTagName()
55-
var checksumAsset *github.ReleaseAsset
56-
var binaryAsset *github.ReleaseAsset
57-
checksumAssetName := fmt.Sprintf("canarycage_%s_checksums.txt", version)
58-
binariAssetName := fmt.Sprintf("canarycage_%s_%s.zip", runtime.GOOS, runtime.GOARCH)
59-
for _, asset := range latestRelease.Assets {
60-
if asset.GetName() == checksumAssetName {
61-
checksumAsset = asset
62-
}
63-
if asset.GetName() == binariAssetName {
64-
binaryAsset = asset
65-
}
66-
}
67-
if checksumAsset == nil || binaryAsset == nil {
68-
return fmt.Errorf("failed to find assets for version %s", version)
55+
checksumAsset, binaryAsset, err := findAssets(latestRelease)
56+
if err != nil {
57+
return err
6958
}
7059
l.Infof("downloading checksums...")
71-
checksum, err := parseChecksums(checksumAsset.GetBrowserDownloadURL(), binariAssetName)
60+
checksum, err := parseChecksums(checksumAsset.GetBrowserDownloadURL(), binaryAsset.GetName())
7261
if err != nil {
7362
return err
7463
}
@@ -93,6 +82,24 @@ func (u *upgrader) Upgrade(ctx context.Context) error {
9382
return nil
9483
}
9584

85+
func findAssets(release *github.RepositoryRelease) (checksumAsset *github.ReleaseAsset, binaryAsset *github.ReleaseAsset, err error) {
86+
version := strings.TrimPrefix(release.GetTagName(), "v")
87+
checksumAssetName := fmt.Sprintf("canarycage_%s_checksums.txt", version)
88+
binaryAssetName := fmt.Sprintf("canarycage_%s_%s.zip", runtime.GOOS, runtime.GOARCH)
89+
for _, asset := range release.Assets {
90+
if asset.GetName() == checksumAssetName {
91+
checksumAsset = asset
92+
}
93+
if asset.GetName() == binaryAssetName {
94+
binaryAsset = asset
95+
}
96+
}
97+
if checksumAsset == nil || binaryAsset == nil {
98+
return nil, nil, fmt.Errorf("failed to find assets for version %s", release.GetTagName())
99+
}
100+
return checksumAsset, binaryAsset, nil
101+
}
102+
96103
func findLatestRelease(cont context.Context, pre bool) (*github.RepositoryRelease, error) {
97104
client := github.NewClient(nil)
98105
releases, _, err := client.Repositories.ListReleases(cont, "loilo-inc", "canarycage", nil)

cli/cage/upgrade/upgrade_test.go

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,25 @@ func TestUpgrade(t *testing.T) {
101101
}
102102
assertUpgraded(t, tmpDir, "0.2.0")
103103
})
104+
t.Run("should error if no assets found", func(t *testing.T) {
105+
httpmock.Activate(t)
106+
httpmock.RegisterResponder("GET", "https://api.github.com/repos/loilo-inc/canarycage/releases",
107+
httpmock.NewJsonResponderOrPanic(200, []*github.RepositoryRelease{
108+
{
109+
TagName: github.String("0.2.0"),
110+
Assets: []*github.ReleaseAsset{
111+
{Name: github.String("some_other_file.txt")},
112+
},
113+
},
114+
}))
115+
u := NewUpgrader(logDI, &cageapp.UpgradeCmdInput{
116+
CurrentVersion: "0.1.0",
117+
})
118+
err := u.Upgrade(t.Context())
119+
assert.EqualError(t, err, "failed to find assets for version 0.2.0")
120+
})
104121
t.Run("parse checksum error", func(t *testing.T) {
105-
httpmock.Activate()
106-
defer httpmock.DeactivateAndReset()
107-
122+
httpmock.Activate(t)
108123
httpmock.RegisterResponder("GET", "https://api.github.com/repos/loilo-inc/canarycage/releases",
109124
httpmock.NewJsonResponderOrPanic(200, makeReleases("0.1.0", "0.2.0")))
110125
httpmock.RegisterResponder("GET", "https://localhost/0.2.0/canarycage_0.2.0_checksums.txt",
@@ -115,6 +130,47 @@ func TestUpgrade(t *testing.T) {
115130
err := u.Upgrade(t.Context())
116131
assert.EqualError(t, err, "invalid checksum line: invalid")
117132
})
133+
}
134+
135+
func Test_findAssets(t *testing.T) {
136+
t.Run("should return assets", func(t *testing.T) {
137+
release := &github.RepositoryRelease{
138+
TagName: github.String("0.2.0"),
139+
Assets: []*github.ReleaseAsset{
140+
makeAsset("0.2.0", "canarycage_0.2.0_checksums.txt"),
141+
makeAsset("0.2.0", binaryAssetName),
142+
},
143+
}
144+
checksumAsset, binaryAsset, err := findAssets(release)
145+
assert.NoError(t, err)
146+
assert.Equal(t, "canarycage_0.2.0_checksums.txt", checksumAsset.GetName())
147+
assert.Equal(t, binaryAssetName, binaryAsset.GetName())
148+
})
149+
t.Run("should trim v from tag name", func(t *testing.T) {
150+
release := &github.RepositoryRelease{
151+
TagName: github.String("v0.2.0"),
152+
Assets: []*github.ReleaseAsset{
153+
makeAsset("v0.2.0", "canarycage_0.2.0_checksums.txt"),
154+
makeAsset("v0.2.0", binaryAssetName),
155+
},
156+
}
157+
checksumAsset, binaryAsset, err := findAssets(release)
158+
assert.NoError(t, err)
159+
assert.Equal(t, "canarycage_0.2.0_checksums.txt", checksumAsset.GetName())
160+
assert.Equal(t, binaryAssetName, binaryAsset.GetName())
161+
})
162+
t.Run("should return error if assets not found", func(t *testing.T) {
163+
release := &github.RepositoryRelease{
164+
TagName: github.String("0.2.0"),
165+
Assets: []*github.ReleaseAsset{
166+
makeAsset("0.2.0", "some_other_file.txt"),
167+
},
168+
}
169+
checksumAsset, binaryAsset, err := findAssets(release)
170+
assert.Nil(t, checksumAsset)
171+
assert.Nil(t, binaryAsset)
172+
assert.EqualError(t, err, "failed to find assets for version 0.2.0")
173+
})
118174

119175
}
120176

@@ -144,8 +200,7 @@ func Test_findLatestRelease(t *testing.T) {
144200
assert.EqualError(t, err, "no releases found")
145201
})
146202
t.Run("should return error if ListReleases failed", func(t *testing.T) {
147-
httpmock.Activate()
148-
defer httpmock.DeactivateAndReset()
203+
httpmock.Activate(t)
149204
httpmock.RegisterResponder("GET", "https://api.github.com/repos/loilo-inc/canarycage/releases",
150205
httpmock.NewErrorResponder(fmt.Errorf("error")))
151206
release, err := findLatestRelease(t.Context(), false)
@@ -182,10 +237,7 @@ func makeReleases(tags ...string) []*github.RepositoryRelease {
182237
func registerResponses(
183238
t *testing.T,
184239
candidates ...string) {
185-
186-
httpmock.Activate()
187-
t.Cleanup(httpmock.DeactivateAndReset)
188-
240+
httpmock.Activate(t)
189241
respond := func(req *http.Request) (*http.Response, error) {
190242
f, err := os.Open("testdata" + req.URL.Path)
191243
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25.5
55
require (
66
github.com/aws/aws-sdk-go-v2 v1.41.1
77
github.com/aws/aws-sdk-go-v2/config v1.32.7
8-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.283.0
8+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.285.0
99
github.com/aws/aws-sdk-go-v2/service/ecr v1.55.1
1010
github.com/aws/aws-sdk-go-v2/service/ecs v1.71.0
1111
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.54.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 h1:WWLqlh79iO48yLkj1v
1414
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17/go.mod h1:EhG22vHRrvF8oXSTYStZhJc1aUgKtnJe+aOiFEV90cM=
1515
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 h1:WKuaxf++XKWlHWu9ECbMlha8WOEGm0OUEZqm4K/Gcfk=
1616
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4/go.mod h1:ZWy7j6v1vWGmPReu0iSGvRiise4YI5SkR3OHKTZ6Wuc=
17-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.283.0 h1:o1GTyhiyvSEy7uMiD9rImR4SQLrAQ2y6q1HE4cCU8E4=
18-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.283.0/go.mod h1:Uy+C+Sc58jozdoL1McQr8bDsEvNFx+/nBY+vpO1HVUY=
17+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.285.0 h1:cRZQsqCy59DSJmvmUYzi9K+dutysXzfx6F+fkcIHtOk=
18+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.285.0/go.mod h1:Uy+C+Sc58jozdoL1McQr8bDsEvNFx+/nBY+vpO1HVUY=
1919
github.com/aws/aws-sdk-go-v2/service/ecr v1.55.1 h1:B7f9R99lCF83XlolTg6d6Lvghyto+/VU83ZrneAVfK8=
2020
github.com/aws/aws-sdk-go-v2/service/ecr v1.55.1/go.mod h1:cpYRXx5BkmS3mwWRKPbWSPKmyAUNL7aLWAPiiinwk/U=
2121
github.com/aws/aws-sdk-go-v2/service/ecs v1.71.0 h1:MzP/ElwTpINq+hS80ZQz4epKVnUTlz8Sz+P/AFORCKM=

0 commit comments

Comments
 (0)