Skip to content

Commit

Permalink
Merge pull request #291 from pn-santos/fix-semver-first-push
Browse files Browse the repository at this point in the history
Fix semver first image push
  • Loading branch information
xtremerui authored Aug 12, 2021
2 parents 9aef3a9 + c87677d commit 63e5d52
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
11 changes: 10 additions & 1 deletion commands/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
51 changes: 46 additions & 5 deletions out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -770,7 +803,8 @@ var _ = DescribeTable("pushing semver tags",
)

type SemverTagPushExample struct {
Tags []string
Tags []string
TagsResponseError *transport.Error

Variant string

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 63e5d52

Please sign in to comment.