From c87677d5f9668df9522852080203d5df84883391 Mon Sep 17 00:00:00 2001 From: Pedro Nuno Santos Date: Mon, 12 Jul 2021 20:52:03 +0100 Subject: [PATCH] fix semver push when image is new Signed-off-by: Pedro Nuno Santos --- commands/out.go | 11 ++++++++++- out_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/commands/out.go b/commands/out.go index 03a8fa1..6ce2a3d 100644 --- a/commands/out.go +++ b/commands/out.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "os" "path/filepath" "strings" @@ -270,7 +271,7 @@ func aliasesToBump(req resource.OutRequest, repo name.Repository, ver *semver.Ve } versions, err := remote.List(repo, opts...) - if err != nil { + if err != nil && !isNewImage(err) { return nil, fmt.Errorf("list repository tags: %w", err) } @@ -343,3 +344,11 @@ func aliasesToBump(req resource.OutRequest, repo name.Repository, ver *semver.Ve return aliases, nil } + +func isNewImage(err error) bool { + if e, ok := err.(*transport.Error); ok && e.StatusCode == http.StatusNotFound { + return e.Errors[0].Code == transport.NameUnknownErrorCode || e.Errors[0].Code == "NOT_FOUND" + } + + return false +} diff --git a/out_test.go b/out_test.go index 4576243..9c5fd99 100644 --- a/out_test.go +++ b/out_test.go @@ -20,6 +20,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/partial" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/remote" + "github.com/google/go-containerregistry/pkg/v1/remote/transport" "github.com/google/go-containerregistry/pkg/v1/tarball" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" @@ -601,6 +602,22 @@ var _ = DescribeTable("pushing semver tags", Error: "no tag specified", }, ), + Entry("bumping aliases with no existing image", + SemverTagPushExample{ + TagsResponseError: &transport.Error{ + StatusCode: http.StatusNotFound, + Errors: []transport.Diagnostic{ + {Code: "NAME_UNKNOWN"}, + }, + }, + + Variant: "", + Version: "1.2.3", + BumpAliases: true, + + PushedTags: []string{"1.2.3", "1.2", "1", "latest"}, + }, + ), Entry("bumping aliases with no existing tags", SemverTagPushExample{ Tags: []string{}, @@ -701,6 +718,22 @@ var _ = DescribeTable("pushing semver tags", PushedTags: []string{"1.2.3-hello", "1.2-hello", "1-hello", "hello"}, }, ), + Entry("bumping variant aliases with no existing image", + SemverTagPushExample{ + TagsResponseError: &transport.Error{ + StatusCode: http.StatusNotFound, + Errors: []transport.Diagnostic{ + {Code: "NOT_FOUND"}, + }, + }, + + Variant: "hello", + Version: "1.2.3", + BumpAliases: true, + + PushedTags: []string{"1.2.3-hello", "1.2-hello", "1-hello", "hello"}, + }, + ), Entry("bumping variant aliases with no existing tags", SemverTagPushExample{ Tags: []string{}, @@ -770,7 +803,8 @@ var _ = DescribeTable("pushing semver tags", ) type SemverTagPushExample struct { - Tags []string + Tags []string + TagsResponseError *transport.Error Variant string @@ -821,13 +855,20 @@ func (example SemverTagPushExample) Run() { ghttp.RespondWith(http.StatusOK, ""), ) + var response http.HandlerFunc + if example.TagsResponseError == nil { + response = ghttp.RespondWithJSONEncoded(http.StatusOK, registryTagsResponse{ + Name: "some-name", + Tags: example.Tags, + }) + } else { + response = ghttp.RespondWithJSONEncoded(example.TagsResponseError.StatusCode, example.TagsResponseError) + } + registry.RouteToHandler( "GET", "/v2/"+repo.RepositoryStr()+"/tags/list", - ghttp.RespondWithJSONEncoded(http.StatusOK, registryTagsResponse{ - Name: "some-name", - Tags: example.Tags, - }), + response, ) registry.RouteToHandler("HEAD", "/v2/test-image/blobs/"+digest.String(), func(w http.ResponseWriter, r *http.Request) {