From 52d4a68d86e4fad43cb1d2dedd7e421bc9501266 Mon Sep 17 00:00:00 2001 From: Marcin Bilski Date: Wed, 22 Dec 2021 15:35:09 +0100 Subject: [PATCH] Prevent tagging existing remote versions. --- pkg/api/api.go | 9 ++++++--- pkg/vcs/git.go | 4 ++-- pkg/vcs/tags.go | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 09c97e2..8f5a5ae 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -352,7 +352,10 @@ func (api *api) tag(w http.ResponseWriter, r *http.Request, module, version stri return } - taggable.Tag(vcs.Version(version), req.Short) - - // TODO(bilus): Response + err = taggable.Tag(r.Context(), vcs.Version(version), req.Short) + if err != nil { + api.log("api.tag", "module", module, "version", version, "error", err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } } diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index 42941d3..89d43c3 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -144,10 +144,10 @@ func isVendoredPackage(name string) bool { func (g *gitVCS) Zip(ctx context.Context, version Version) (io.ReadCloser, error) { dirName := g.module + "@" + string(version) - return g.zipUnder(ctx, version, dirName) + return g.zipAs(ctx, version, dirName) } -func (g *gitVCS) zipUnder(ctx context.Context, version Version, dirName string) (io.ReadCloser, error) { +func (g *gitVCS) zipAs(ctx context.Context, version Version, dirName string) (io.ReadCloser, error) { g.log("gitVCS.Zip", "module", g.module, "version", version) ci, err := g.commit(ctx, version) if err != nil { diff --git a/pkg/vcs/tags.go b/pkg/vcs/tags.go index f198686..55310b0 100644 --- a/pkg/vcs/tags.go +++ b/pkg/vcs/tags.go @@ -22,7 +22,7 @@ func NewEphemeralTagStorage() *EphemeralTagStorage { } } -func (s *EphemeralTagStorage) Tag(module string, semVer Version, short string) { +func (s *EphemeralTagStorage) Tag(module string, semVer Version, short string) error { tags := s.tagsByModule[module] tmp := tags[:0] for _, t := range tags { @@ -31,6 +31,7 @@ func (s *EphemeralTagStorage) Tag(module string, semVer Version, short string) { } } s.tagsByModule[module] = append(tmp, ephemeralTag{semVer, short}) + return nil } func (s *EphemeralTagStorage) tags(module string) []ephemeralTag { @@ -46,7 +47,7 @@ type taggableVCS struct { } type Taggable interface { - Tag(semVer Version, short string) + Tag(ctx context.Context, semVer Version, short string) error } // NewGitWithEphemeralTags return a go-git VCS client implementation that @@ -61,8 +62,15 @@ func NewGitWithEphemeralTags(l logger, dir string, module string, auth Auth, sto } } -func (v *taggableVCS) Tag(semVer Version, short string) { - v.storage.Tag(v.module, semVer, short) +func (v *taggableVCS) Tag(ctx context.Context, semVer Version, short string) error { + remoteVersions, err := v.wrapped.List(ctx) + if err != nil { + return err + } + if versionExists(remoteVersions, semVer) { + return fmt.Errorf("remote version %s already exists for module %s", semVer, v.module) + } + return v.storage.Tag(v.module, semVer, short) } func (v *taggableVCS) List(ctx context.Context) ([]Version, error) { @@ -110,7 +118,7 @@ func (v *taggableVCS) Zip(ctx context.Context, version Version) (io.ReadCloser, } // Zip must contain the ephemeral version. dirName := v.module + "@" + string(version) - return v.wrapped.zipUnder(ctx, version2, dirName) + return v.wrapped.zipAs(ctx, version2, dirName) } func (v *taggableVCS) resolveVersion(ctx context.Context, version Version) (Version, error) {