Skip to content

Commit

Permalink
feat: registry v2 initial
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Feb 4, 2023
1 parent 124eecd commit 9a75641
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 69 deletions.
3 changes: 2 additions & 1 deletion pkg/plugin/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 {
Expand All @@ -33,7 +34,7 @@ func loadResolvers(resolvers ...resolver.Resolver) (map[string]resolver.Resolver
}

func New(config *config.Config) (*Discovery, error) {
resolvers, err := loadResolvers(registry.NewResolver(), github.NewResolver())
resolvers, err := loadResolvers(registryV1.NewResolver(), github.NewResolver(), registry.NewResolver())
if err != nil {
return nil, err
}
Expand Down
59 changes: 12 additions & 47 deletions pkg/plugin/discovery/resolver/registry/registry.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,30 @@
package registry

import (
"errors"
"fmt"
"runtime"
"sort"

"github.com/Masterminds/semver/v3"
"github.com/go-semantic-release/plugin-registry/pkg/client"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
)

type Resolver struct{}
const DefaultEndpoint = "https://registry-staging.go-semantic-release.xyz/api/v2"

func NewResolver() *Resolver {
return &Resolver{}
type Resolver struct {
client *client.Client
}

func (r *Resolver) ResolvePlugin(pluginInfo *plugin.Info) (*resolver.PluginDownloadInfo, error) {
pluginAPIRes, err := getPluginInfo(pluginInfo.NormalizedName)
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")
func NewResolver() *Resolver {
return &Resolver{
client: client.New(DefaultEndpoint),
}
}

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) ResolvePlugin(pluginInfo *plugin.Info) (*resolver.PluginDownloadInfo, error) {
return nil, fmt.Errorf("not implemented")
}

func (r *Resolver) Names() []string {
return []string{"registry"}
// TODO: this should be registry when the registry is ready
return []string{"registry-beta"}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package registry
package registryV1

import (
"encoding/json"
Expand Down
65 changes: 65 additions & 0 deletions pkg/plugin/discovery/resolver/registryV1/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package registryV1

import (
"errors"
"fmt"
"runtime"
"sort"

"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"
)

type Resolver struct{}

func NewResolver() *Resolver {
return &Resolver{}
}

func (r *Resolver) ResolvePlugin(pluginInfo *plugin.Info) (*resolver.PluginDownloadInfo, error) {
pluginAPIRes, err := getPluginInfo(pluginInfo.NormalizedName)
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"}
}
38 changes: 18 additions & 20 deletions pkg/plugin/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,27 @@ func (m *PluginManager) Stop() {
plugin.KillAllPlugins()
}

func (m *PluginManager) FetchAllPlugins() error {
pluginMap := map[string]string{
condition.CIConditionPluginName: m.config.CIConditionPlugin,
provider.PluginName: m.config.ProviderPlugin,
analyzer.CommitAnalyzerPluginName: m.config.CommitAnalyzerPlugin,
generator.ChangelogGeneratorPluginName: m.config.ChangelogGeneratorPlugin,
}
for t, name := range pluginMap {
_, err := m.discovery.FindPlugin(t, name)
if err != nil {
return err
}
}

func (m *PluginManager) getAllPlugins() [][]string {
plugins := make([][]string, 0, 4)
// required plugins
plugins = append(plugins, []string{condition.CIConditionPluginName, m.config.CIConditionPlugin})
plugins = append(plugins, []string{provider.PluginName, m.config.ProviderPlugin})
plugins = append(plugins, []string{analyzer.CommitAnalyzerPluginName, m.config.CommitAnalyzerPlugin})
plugins = append(plugins, []string{generator.ChangelogGeneratorPluginName, m.config.ChangelogGeneratorPlugin})

// optional plugins
for _, pl := range m.config.FilesUpdaterPlugins {
_, err := m.discovery.FindPlugin(updater.FilesUpdaterPluginName, pl)
if err != nil {
return err
}
plugins = append(plugins, []string{updater.FilesUpdaterPluginName, pl})
}

for _, pl := range m.config.HooksPlugins {
_, err := m.discovery.FindPlugin(hooks.PluginName, pl)
plugins = append(plugins, []string{hooks.PluginName, pl})
}
return plugins
}

func (m *PluginManager) FetchAllPlugins() error {
for _, pl := range m.getAllPlugins() {
_, err := m.discovery.FindPlugin(pl[0], pl[1])
if err != nil {
return err
}
Expand Down

0 comments on commit 9a75641

Please sign in to comment.