Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generate-registry): support filtering versions #1918

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cli/generate_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (runner *Runner) newGenerateRegistryCommand() *cli.Command {
Name: "deep",
Usage: "Resolve version_overrides",
},
&cli.StringFlag{
Name: "version-filter",
Usage: "version filter",
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (runner *Runner) setParam(c *cli.Context, commandName string, param *config
param.All = c.Bool("all")
param.Prune = c.Bool("prune")
param.SelectVersion = c.Bool("select-version")
param.VersionFilter = c.String("version-filter")
param.File = c.String("f")
param.LogColor = os.Getenv("AQUA_LOG_COLOR")
param.AQUAVersion = runner.LDFlags.Version
Expand Down
1 change: 1 addition & 0 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type Param struct {
Dest string
HomeDir string
OutTestData string
VersionFilter string
MaxParallelism int
Args []string
Tags map[string]struct{}
Expand Down
15 changes: 12 additions & 3 deletions pkg/controller/generate-registry/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/controller/generate/output"
"github.com/aquaproj/aqua/v2/pkg/expr"
"github.com/aquaproj/aqua/v2/pkg/github"
yaml "github.com/goccy/go-yaml"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -51,7 +52,15 @@ func (ctrl *Controller) GenerateRegistry(ctx context.Context, param *config.Para
}

func (ctrl *Controller) genRegistry(ctx context.Context, param *config.Param, logE *logrus.Entry, pkgName string) error {
pkgInfo, versions := ctrl.getPackageInfo(ctx, logE, pkgName, param.Deep)
var versionFilter *expr.Program
if param.VersionFilter != "" {
a, err := expr.CompileVersionFilter(param.VersionFilter)
if err != nil {
return fmt.Errorf("compile the version-filter: %w", err)
}
versionFilter = a
}
pkgInfo, versions := ctrl.getPackageInfo(ctx, logE, pkgName, param.Deep, versionFilter)
if param.OutTestData != "" {
if err := ctrl.testdataOutputter.Output(&output.Param{
List: listPkgsFromVersions(pkgName, versions),
Expand Down Expand Up @@ -87,7 +96,7 @@ func (ctrl *Controller) getRelease(ctx context.Context, repoOwner, repoName, ver
return release, err //nolint:wrapcheck
}

func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry, arg string, deep bool) (*registry.PackageInfo, []string) {
func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry, arg string, deep bool, versionFilter *expr.Program) (*registry.PackageInfo, []string) {
pkgName, version, _ := strings.Cut(arg, "@")
splitPkgNames := strings.Split(pkgName, "/")
pkgInfo := &registry.PackageInfo{
Expand All @@ -112,7 +121,7 @@ func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry,
pkgInfo.Description = strings.TrimRight(strings.TrimSpace(repo.GetDescription()), ".!?")
}
if deep && version == "" {
return ctrl.getPackageInfoWithVersionOverrides(ctx, logE, pkgName, pkgInfo)
return ctrl.getPackageInfoWithVersionOverrides(ctx, logE, pkgName, pkgInfo, versionFilter)
}
release, err := ctrl.getRelease(ctx, pkgInfo.RepoOwner, pkgInfo.RepoName, version)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/generate-registry/generate_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestController_getPackageInfo(t *testing.T) { //nolint:funlen
Repo: d.repo,
}
ctrl := NewController(nil, gh, nil)
pkgInfo, _ := ctrl.getPackageInfo(ctx, logE, d.pkgName, true)
pkgInfo, _ := ctrl.getPackageInfo(ctx, logE, d.pkgName, true, nil)
if diff := cmp.Diff(d.exp, pkgInfo); diff != "" {
t.Fatal(diff)
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/controller/generate-registry/version_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/expr"
"github.com/aquaproj/aqua/v2/pkg/github"
"github.com/aquaproj/aqua/v2/pkg/util"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -59,20 +60,28 @@ func listPkgsFromVersions(pkgName string, versions []string) []*aqua.Package {
return pkgs
}

func (ctrl *Controller) getPackageInfoWithVersionOverrides(ctx context.Context, logE *logrus.Entry, pkgName string, pkgInfo *registry.PackageInfo) (*registry.PackageInfo, []string) {
func (ctrl *Controller) getPackageInfoWithVersionOverrides(ctx context.Context, logE *logrus.Entry, pkgName string, pkgInfo *registry.PackageInfo, versionFilter *expr.Program) (*registry.PackageInfo, []string) { //nolint:cyclop
ghReleases := ctrl.listReleases(ctx, logE, pkgInfo)
releases := make([]*Release, len(ghReleases))
for i, release := range ghReleases {
releases := make([]*Release, 0, len(ghReleases))
for _, release := range ghReleases {
tag := release.GetTagName()
if versionFilter != nil {
if f, err := expr.EvaluateVersionFilter(versionFilter, tag); err != nil {
logE.WithError(err).WithField("release_version", tag).Debug("evaluate version filter")
continue
} else if !f {
continue
}
}
v, err := version.NewVersion(tag)
if err != nil {
logE.WithField("tag_name", tag).WithError(err).Warn("parse a tag as semver")
}
releases[i] = &Release{
releases = append(releases, &Release{
ID: release.GetID(),
Tag: tag,
Version: v,
}
})
}
sort.Slice(releases, func(i, j int) bool {
r1 := releases[i]
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/generate/github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"strings"

"github.com/antonmedv/expr/vm"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/expr"
"github.com/aquaproj/aqua/v2/pkg/github"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (ctrl *Controller) getVersionFromLatestRelease(ctx context.Context, logE *l

type Filter struct {
Prefix string
Filter *vm.Program
Filter *expr.Program
Constraint string
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/expr/version_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/antonmedv/expr/vm"
)

func CompileVersionFilter(versionFilter string) (*vm.Program, error) {
type Program = vm.Program

func CompileVersionFilter(versionFilter string) (*Program, error) {
return expr.Compile(versionFilter, expr.AsBool(), expr.Env(map[string]interface{}{ //nolint:wrapcheck
"Version": "",
"semver": emptySemver,
Expand All @@ -16,7 +18,7 @@ func CompileVersionFilter(versionFilter string) (*vm.Program, error) {
}))
}

func CompileVersionFilterForTest(versionFilter string) *vm.Program {
func CompileVersionFilterForTest(versionFilter string) *Program {
p, err := expr.Compile(versionFilter, expr.AsBool(), expr.Env(map[string]interface{}{
"Version": "",
"semver": emptySemver,
Expand All @@ -29,7 +31,7 @@ func CompileVersionFilterForTest(versionFilter string) *vm.Program {
return p
}

func EvaluateVersionFilter(prog *vm.Program, v string) (bool, error) {
func EvaluateVersionFilter(prog *Program, v string) (bool, error) {
return evaluateBoolProg(prog, map[string]interface{}{
"Version": v,
"semver": getCompareFunc(v),
Expand Down