From 9111603ff938cf6c69bd5f066ae930d186088388 Mon Sep 17 00:00:00 2001
From: Christoph Witzko <github@christophwitzko.com>
Date: Fri, 10 Feb 2023 12:26:46 +0100
Subject: [PATCH] feat: remove legacy plugin resolver

---
 pkg/config/config.go                          |  2 +-
 pkg/plugin/discovery/discovery.go             |  2 -
 .../registry/{registry.go => resolver.go}     |  0
 .../discovery/resolver/registryV1/api.go      | 56 -------------
 .../discovery/resolver/registryV1/registry.go | 81 -------------------
 5 files changed, 1 insertion(+), 140 deletions(-)
 rename pkg/plugin/discovery/resolver/registry/{registry.go => resolver.go} (100%)
 delete mode 100644 pkg/plugin/discovery/resolver/registryV1/api.go
 delete mode 100644 pkg/plugin/discovery/resolver/registryV1/registry.go

diff --git a/pkg/config/config.go b/pkg/config/config.go
index 6cac675..0d5f0ed 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -197,7 +197,7 @@ func SetFlags(cmd *cobra.Command) {
 	cmd.Flags().Bool("show-progress", false, "shows the plugin download progress")
 	cmd.Flags().String("config", "", "config file (default is .semrelrc)")
 	cmd.Flags().Bool("allow-maintained-version-on-default-branch", false, "allow configuring the maintained version on the default branch")
-	cmd.Flags().String("plugin-resolver", "registry", "which resolver should be used to resolve plugins (registry-v1, registry[-v2] or github)")
+	cmd.Flags().String("plugin-resolver", "registry", "which resolver should be used to resolve plugins (registry[-v2] or github)")
 	cmd.Flags().Bool("plugin-resolver-disable-batch-prefetch", false, "plugins should not be batch prefetched using the registry")
 	cmd.Flags().String("plugin-resolver-endpoint", "", "explicitly specify the resolver endpoint that should be used for resolving the plugin dependencies")
 	cmd.Flags().SortFlags = true
diff --git a/pkg/plugin/discovery/discovery.go b/pkg/plugin/discovery/discovery.go
index a402d95..4cd82a0 100644
--- a/pkg/plugin/discovery/discovery.go
+++ b/pkg/plugin/discovery/discovery.go
@@ -9,7 +9,6 @@ import (
 	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
 	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver/github"
 	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver/registry"
-	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver/registryV1"
 )
 
 type Discovery struct {
@@ -36,7 +35,6 @@ func loadResolvers(resolvers ...resolver.Resolver) (map[string]resolver.Resolver
 func New(config *config.Config) (*Discovery, error) {
 	resolvers, err := loadResolvers(
 		registry.NewResolver(config.PluginResolverEndpoint),
-		registryV1.NewResolver(config.PluginResolverEndpoint),
 		github.NewResolver(),
 	)
 	if err != nil {
diff --git a/pkg/plugin/discovery/resolver/registry/registry.go b/pkg/plugin/discovery/resolver/registry/resolver.go
similarity index 100%
rename from pkg/plugin/discovery/resolver/registry/registry.go
rename to pkg/plugin/discovery/resolver/registry/resolver.go
diff --git a/pkg/plugin/discovery/resolver/registryV1/api.go b/pkg/plugin/discovery/resolver/registryV1/api.go
deleted file mode 100644
index 2ce8dbc..0000000
--- a/pkg/plugin/discovery/resolver/registryV1/api.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package registryV1
-
-import (
-	"encoding/json"
-	"errors"
-	"fmt"
-	"net/http"
-	"runtime"
-	"time"
-)
-
-type apiPluginAsset struct {
-	FileName string
-	URL      string
-	OS       string
-	Arch     string
-	Checksum string
-}
-
-type apiPluginRelease struct {
-	CreatedAt time.Time
-	Assets    []*apiPluginAsset
-}
-
-func (r *apiPluginRelease) getMatchingAsset() *apiPluginAsset {
-	for _, a := range r.Assets {
-		if a.OS == runtime.GOOS && a.Arch == runtime.GOARCH {
-			return a
-		}
-	}
-	return nil
-}
-
-type apiPlugin struct {
-	LatestRelease string
-	Versions      map[string]*apiPluginRelease
-}
-
-func getPluginInfo(endpoint, name string) (*apiPlugin, error) {
-	res, err := http.Get(fmt.Sprintf("%s/plugins/%s.json", endpoint, name))
-	if err != nil {
-		return nil, err
-	}
-	defer res.Body.Close()
-	if res.StatusCode == 404 {
-		return nil, fmt.Errorf("plugin not found: %s", name)
-	}
-	if res.StatusCode < 200 || res.StatusCode >= 300 {
-		return nil, errors.New("invalid response")
-	}
-	var plugin *apiPlugin
-	if err := json.NewDecoder(res.Body).Decode(&plugin); err != nil {
-		return nil, err
-	}
-	return plugin, nil
-}
diff --git a/pkg/plugin/discovery/resolver/registryV1/registry.go b/pkg/plugin/discovery/resolver/registryV1/registry.go
deleted file mode 100644
index 2f4b2bc..0000000
--- a/pkg/plugin/discovery/resolver/registryV1/registry.go
+++ /dev/null
@@ -1,81 +0,0 @@
-package registryV1
-
-import (
-	"errors"
-	"fmt"
-	"runtime"
-	"sort"
-	"strings"
-
-	"github.com/Masterminds/semver/v3"
-	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
-	"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
-)
-
-const DefaultEndpoint = "https://plugins.go-semantic-release.xyz/api/v1"
-
-var _ resolver.Resolver = &Resolver{}
-
-type Resolver struct {
-	endpoint string
-}
-
-func NewResolver(endpoint string) *Resolver {
-	if endpoint == "" {
-		endpoint = DefaultEndpoint
-	}
-	endpoint = strings.TrimSuffix(endpoint, "/")
-	if !strings.HasSuffix(endpoint, "/api/v1") {
-		endpoint = fmt.Sprintf("%s/api/v1", endpoint)
-	}
-	return &Resolver{
-		endpoint: endpoint,
-	}
-}
-
-func (r *Resolver) ResolvePlugin(pluginInfo *plugin.Info) (*resolver.PluginDownloadInfo, error) {
-	pluginAPIRes, err := getPluginInfo(r.endpoint, pluginInfo.ShortNormalizedName)
-	if err != nil {
-		return nil, err
-	}
-
-	foundVersion := ""
-	if pluginInfo.Constraint == nil {
-		foundVersion = pluginAPIRes.LatestRelease
-	} else {
-		versions := make(semver.Collection, 0)
-		for v := range pluginAPIRes.Versions {
-			pv, err := semver.NewVersion(v)
-			if err != nil {
-				return nil, err
-			}
-			versions = append(versions, pv)
-		}
-		sort.Sort(sort.Reverse(versions))
-		for _, v := range versions {
-			if pluginInfo.Constraint.Check(v) {
-				foundVersion = v.String()
-				break
-			}
-		}
-	}
-
-	if foundVersion == "" {
-		return nil, errors.New("version not found")
-	}
-
-	releaseAsset := pluginAPIRes.Versions[foundVersion].getMatchingAsset()
-	if releaseAsset == nil {
-		return nil, fmt.Errorf("a matching plugin was not found for %s/%s", runtime.GOOS, runtime.GOARCH)
-	}
-	return &resolver.PluginDownloadInfo{
-		URL:      releaseAsset.URL,
-		Checksum: releaseAsset.Checksum,
-		FileName: releaseAsset.FileName,
-		Version:  foundVersion,
-	}, nil
-}
-
-func (r *Resolver) Names() []string {
-	return []string{"registry-v1"}
-}
-- 
GitLab