diff --git a/CHANGELOG.md b/CHANGELOG.md index 745d14e73..85c786ee2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Add list of downloads to /search endpoint. [#512](https://github.com/elastic/package-registry/pull/512) * Apply rule: first package found served. [#546](https://github.com/elastic/package-registry/pull/546) * Implement package watcher. [#553](https://github.com/elastic/package-registry/pull/553) +* Add conditions as future replacement of requirements. [#519](https://github.com/elastic/package-registry/pull/519) ### Deprecated diff --git a/testdata/generated/package.json b/testdata/generated/package.json index 1c87bbc83..2ea8f575b 100644 --- a/testdata/generated/package.json +++ b/testdata/generated/package.json @@ -20,6 +20,9 @@ "azure" ], "release": "ga", + "conditions": { + "kibana.version": "~7.x.x" + }, "requirement": { "kibana": { "versions": "\u003e=7.0.0" diff --git a/testdata/generated/package/base/0.2.0/index.json b/testdata/generated/package/base/0.2.0/index.json index 8dd715235..15253cac9 100644 --- a/testdata/generated/package/base/0.2.0/index.json +++ b/testdata/generated/package/base/0.2.0/index.json @@ -18,6 +18,9 @@ "license": "basic", "categories": [], "release": "ga", + "conditions": { + "kibana.version": "\u003e7.9.0" + }, "requirement": { "kibana": { "versions": "\u003e7.9.0" diff --git a/testdata/generated/package/example/0.0.2/index.json b/testdata/generated/package/example/0.0.2/index.json index 21fdb406b..92d9a7dde 100644 --- a/testdata/generated/package/example/0.0.2/index.json +++ b/testdata/generated/package/example/0.0.2/index.json @@ -19,6 +19,9 @@ "web" ], "release": "beta", + "conditions": { + "kibana.version": "\u003e=6.0.0" + }, "requirement": { "kibana": { "versions": "\u003e=6.0.0" diff --git a/testdata/generated/package/example/1.0.0/index.json b/testdata/generated/package/example/1.0.0/index.json index 1c87bbc83..2ea8f575b 100644 --- a/testdata/generated/package/example/1.0.0/index.json +++ b/testdata/generated/package/example/1.0.0/index.json @@ -20,6 +20,9 @@ "azure" ], "release": "ga", + "conditions": { + "kibana.version": "~7.x.x" + }, "requirement": { "kibana": { "versions": "\u003e=7.0.0" diff --git a/testdata/generated/package/foo/1.0.0/index.json b/testdata/generated/package/foo/1.0.0/index.json index 76af7e68e..78c0cb6cc 100644 --- a/testdata/generated/package/foo/1.0.0/index.json +++ b/testdata/generated/package/foo/1.0.0/index.json @@ -19,6 +19,9 @@ "custom" ], "release": "beta", + "conditions": { + "kibana.version": "\u003e=7.0.0" + }, "requirement": { "kibana": { "versions": "\u003e=7.0.0" diff --git a/testdata/generated/package/longdocs/1.0.4/index.json b/testdata/generated/package/longdocs/1.0.4/index.json index 670acc942..9249bc15c 100644 --- a/testdata/generated/package/longdocs/1.0.4/index.json +++ b/testdata/generated/package/longdocs/1.0.4/index.json @@ -26,6 +26,9 @@ "web" ], "release": "ga", + "conditions": { + "kibana.version": "\u003e6.7.0" + }, "requirement": { "kibana": { "versions": "\u003e6.7.0" diff --git a/testdata/generated/package/multiversion/1.0.3/index.json b/testdata/generated/package/multiversion/1.0.3/index.json index f7471b690..237dccc0c 100644 --- a/testdata/generated/package/multiversion/1.0.3/index.json +++ b/testdata/generated/package/multiversion/1.0.3/index.json @@ -26,6 +26,9 @@ "web" ], "release": "ga", + "conditions": { + "kibana.version": "\u003e6.7.0" + }, "requirement": { "kibana": { "versions": "\u003e6.7.0" diff --git a/testdata/generated/package/multiversion/1.0.4/index.json b/testdata/generated/package/multiversion/1.0.4/index.json index 4a6d8f090..f7a4b3937 100644 --- a/testdata/generated/package/multiversion/1.0.4/index.json +++ b/testdata/generated/package/multiversion/1.0.4/index.json @@ -26,6 +26,9 @@ "web" ], "release": "ga", + "conditions": { + "kibana.version": "\u003e6.7.0" + }, "requirement": { "kibana": { "versions": "\u003e6.7.0" diff --git a/testdata/generated/package/multiversion/1.1.0/index.json b/testdata/generated/package/multiversion/1.1.0/index.json index a91f26e64..99922c7fa 100644 --- a/testdata/generated/package/multiversion/1.1.0/index.json +++ b/testdata/generated/package/multiversion/1.1.0/index.json @@ -26,6 +26,9 @@ "web" ], "release": "ga", + "conditions": { + "kibana.version": "\u003e6.7.0" + }, "requirement": { "kibana": { "versions": "\u003e6.7.0" diff --git a/testdata/generated/package/reference/1.0.0/index.json b/testdata/generated/package/reference/1.0.0/index.json index 4d29d6365..c3b30a462 100644 --- a/testdata/generated/package/reference/1.0.0/index.json +++ b/testdata/generated/package/reference/1.0.0/index.json @@ -27,6 +27,9 @@ "web" ], "release": "ga", + "conditions": { + "kibana.version": "\u003e6.7.0 \u003c7.6.0" + }, "requirement": { "kibana": { "versions": "\u003e6.7.0 \u003c7.6.0" diff --git a/testdata/package/example/1.0.0/manifest.yml b/testdata/package/example/1.0.0/manifest.yml index d28e9bf76..99af2197e 100644 --- a/testdata/package/example/1.0.0/manifest.yml +++ b/testdata/package/example/1.0.0/manifest.yml @@ -10,6 +10,9 @@ release: ga owner.github: "ruflin" +conditions: + kibana.version: "~7.x.x" + requirement: kibana: versions: ">=7.0.0" diff --git a/testdata/package/reference/1.0.0/manifest.yml b/testdata/package/reference/1.0.0/manifest.yml index 2f370f960..accebd8f9 100644 --- a/testdata/package/reference/1.0.0/manifest.yml +++ b/testdata/package/reference/1.0.0/manifest.yml @@ -33,6 +33,9 @@ type: integration owner: github: "ruflin" +conditions: + kibana.version: ">6.7.0 <7.6.0" + requirement: kibana: versions: ">6.7.0 <7.6.0" diff --git a/util/package.go b/util/package.go index 77711b1e2..dbf4827fd 100644 --- a/util/package.go +++ b/util/package.go @@ -59,6 +59,7 @@ type Package struct { versionSemVer *semver.Version Categories []string `config:"categories" json:"categories"` Release string `config:"release,omitempty" json:"release,omitempty"` + Conditions *Conditions `config:"conditions,omitempty" json:"conditions,omitempty" yaml:"conditions,omitempty"` Requirement Requirement `config:"requirement" json:"requirement"` Screenshots []Image `config:"screenshots,omitempty" json:"screenshots,omitempty" yaml:"screenshots,omitempty"` Assets []string `config:"assets,omitempty" json:"assets,omitempty" yaml:"assets,omitempty"` @@ -96,6 +97,11 @@ type Requirement struct { Kibana ProductRequirement `config:"kibana" json:"kibana,omitempty" yaml:"kibana"` } +type Conditions struct { + KibanaVersion string `config:"kibana.version,omitempty" json:"kibana.version,omitempty" yaml:"kibana.version,omitempty"` + kibanaConstraint *semver.Constraints +} + type ProductRequirement struct { Versions string `config:"versions,omitempty" json:"versions,omitempty" yaml:"versions,omitempty"` semVerRange *semver.Constraints @@ -149,7 +155,7 @@ func NewPackage(basePath string) (*Package, error) { var p = &Package{ BasePath: basePath, } - err = manifest.Unpack(p) + err = manifest.Unpack(p, ucfg.PathSep(".")) if err != nil { return nil, err } @@ -184,8 +190,18 @@ func NewPackage(basePath string) (*Package, error) { p.Downloads = []Download{NewDownload(*p, "tar")} - if p.Requirement.Kibana.Versions != "" { - p.Requirement.Kibana.semVerRange, err = semver.NewConstraint(p.Requirement.Kibana.Versions) + // If the new conditions are used, select them over the requirements + if p.Conditions != nil && p.Conditions.KibanaVersion != "" { + p.Conditions.kibanaConstraint, err = semver.NewConstraint(p.Conditions.KibanaVersion) + if err != nil { + return nil, errors.Wrapf(err, "invalid Kibana versions range: %s", p.Conditions.KibanaVersion) + } + // TODO: remove legacy part + } else if p.Requirement.Kibana.Versions != "" { + p.Conditions = &Conditions{ + KibanaVersion: p.Requirement.Kibana.Versions, + } + p.Conditions.kibanaConstraint, err = semver.NewConstraint(p.Requirement.Kibana.Versions) if err != nil { return nil, errors.Wrapf(err, "invalid Kibana versions range: %s", p.Requirement.Kibana.Versions) } @@ -252,16 +268,11 @@ func (p *Package) HasCategory(category string) bool { func (p *Package) HasKibanaVersion(version *semver.Version) bool { // If the version is not specified, it is for all versions - if p.Requirement.Kibana.Versions == "" { + if p.Conditions == nil || version == nil { return true } - if version != nil { - if !p.Requirement.Kibana.semVerRange.Check(version) { - return false - } - } - return true + return p.Conditions.kibanaConstraint.Check(version) } func (p *Package) IsNewerOrEqual(pp Package) bool { diff --git a/util/package_test.go b/util/package_test.go index 993be28dd..8b0f63386 100644 --- a/util/package_test.go +++ b/util/package_test.go @@ -5,8 +5,10 @@ package util import ( + "log" "testing" + "github.com/Masterminds/semver/v3" "github.com/stretchr/testify/assert" ) @@ -127,6 +129,86 @@ func TestValidate(t *testing.T) { } } +var kibanaVersionPackageTests = []struct { + description string + constraint string + kibanaVersion string + check bool +}{ + { + "last major", + ">= 7.0.0", + "6.7.0", + false, + }, + { + "next minor", + ">= 7.0.0", + "7.1.0", + true, + }, + { + "next minor tilde", + "~7", + "7.1.0", + true, + }, + { + "next minor tilde, x", + "~7.x.x", + "7.1.0", + true, + }, + { + "next minor tilde, not matching", + "~7.0.0", + "7.1.0", + false, + }, + { + "next minor tilde, matching", + "~7.0.x", + "7.0.2", + true, + }, + { + "inside major, not", + "^7.6.0", + "7.0.2", + false, + }, + { + "inside major", + "^7.6.0", + "7.12.2", + true, + }, +} + +func TestHasKibanaVersion(t *testing.T) { + for _, tt := range kibanaVersionPackageTests { + t.Run(tt.description, func(t *testing.T) { + + constraint, err := semver.NewConstraint(tt.constraint) + assert.NoError(t, err) + + p := Package{ + Conditions: &Conditions{ + kibanaConstraint: constraint, + }, + } + + kibanaVersion, err := semver.NewVersion(tt.kibanaVersion) + assert.NoError(t, err) + + check := p.HasKibanaVersion(kibanaVersion) + log.Println(check) + assert.Equal(t, tt.check, check) + + }) + } +} + func BenchmarkNewPackage(b *testing.B) { for i := 0; i < b.N; i++ { _, err := NewPackage("../testdata/package/reference/1.0.0")