Skip to content

Commit

Permalink
Add Kibana min/max validation (#99)
Browse files Browse the repository at this point in the history
Improve error reporting for creating and checking a package.
  • Loading branch information
ruflin authored Sep 11, 2019
1 parent f0e3c23 commit 29a05e3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Add validation check that Kibana min/max are valid semver versions. [#99](https://github.com/elastic/integrations-registry/pull/99)

### Changed

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/elastic/go-ucfg v0.7.0
github.com/gorilla/mux v1.7.2
github.com/magefile/mage v1.8.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/magefile/mage v1.8.0 h1:mzL+xIopvPURVBwHG9A50JcjBO+xV3b5iZ7khFRI+5E=
github.com/magefile/mage v1.8.0/go.mod h1:IUDi13rsHje59lecXokTfGX0QIzO45uVPlXnJYsXepA=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func buildPackage(packagesBasePath, path string) error {
// Build package endpoint
p, err := util.NewPackage(".", path)
if err != nil {
return err
return fmt.Errorf("Error creating package: %s: %s", path, err)
}

// Checks if the package is valid
Expand Down
52 changes: 36 additions & 16 deletions util/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"

"github.com/blang/semver"

"gopkg.in/yaml.v2"
Expand All @@ -19,20 +21,24 @@ type Package struct {
Title *string `yaml:"title,omitempty" json:"title,omitempty"`
Version string `yaml:"version" json:"version"`
versionSemVer semver.Version
Description string `yaml:"description" json:"description"`
Type string `yaml:"type" json:"type"`
Categories []string `yaml:"categories" json:"categories"`
Requirement struct {
Kibana struct {
Min string `yaml:"version.min" json:"version.min"`
Max string `yaml:"version.max" json:"version.max"`
minSemVer semver.Version
maxSemVer semver.Version
} `yaml:"kibana" json:"kibana"`
} `yaml:"requirement" json:"requirement"`
Screenshots []Image `yaml:"screenshots,omitempty" json:"screenshots,omitempty"`
Icons []Image `yaml:"icons,omitempty" json:"icons,omitempty"`
Assets []string `yaml:"assets,omitempty" json:"assets,omitempty"`
Description string `yaml:"description" json:"description"`
Type string `yaml:"type" json:"type"`
Categories []string `yaml:"categories" json:"categories"`
Requirement Requirement `yaml:"requirement" json:"requirement"`
Screenshots []Image `yaml:"screenshots,omitempty" json:"screenshots,omitempty"`
Icons []Image `yaml:"icons,omitempty" json:"icons,omitempty"`
Assets []string `yaml:"assets,omitempty" json:"assets,omitempty"`
}

type Requirement struct {
Kibana Kibana `yaml:"kibana" json:"kibana"`
}

type Kibana struct {
Min string `yaml:"version.min" json:"version.min"`
Max string `yaml:"version.max" json:"version.max"`
minSemVer semver.Version
maxSemVer semver.Version
}

type Image struct {
Expand Down Expand Up @@ -80,14 +86,14 @@ func NewPackage(basePath, packageName string) (*Package, error) {
if p.Requirement.Kibana.Max != "" {
p.Requirement.Kibana.maxSemVer, err = semver.Parse(p.Requirement.Kibana.Max)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "invalid Kibana max version: %s", p.Requirement.Kibana.Max)
}
}

if p.Requirement.Kibana.Min != "" {
p.Requirement.Kibana.minSemVer, err = semver.Parse(p.Requirement.Kibana.Min)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "invalid Kibana min version: %s", p.Requirement.Kibana.Min)
}
}

Expand Down Expand Up @@ -197,5 +203,19 @@ func (p *Package) Validate() error {
return fmt.Errorf("no description set")
}

if p.Requirement.Kibana.Max != "" {
_, err := semver.Parse(p.Requirement.Kibana.Max)
if err != nil {
return fmt.Errorf("invalid max kibana version: %s, %s", p.Requirement.Kibana.Max, err)
}
}

if p.Requirement.Kibana.Min != "" {
_, err := semver.Parse(p.Requirement.Kibana.Min)
if err != nil {
return fmt.Errorf("invalid min Kibana version: %s, %s", p.Requirement.Kibana.Min, err)
}
}

return nil
}
32 changes: 32 additions & 0 deletions util/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,42 @@ var packageTests = []struct {
false,
"missing description",
},
{
Package{
Title: &title,
Requirement: Requirement{
Kibana{
Min: "1.2.3",
Max: "bar",
},
},
},
false,
"invalid Kibana max version",
},
{
Package{
Title: &title,
Requirement: Requirement{
Kibana{
Min: "foo",
Max: "4.5.6",
},
},
},
false,
"invalid Kibana min version",
},
{
Package{
Title: &title,
Description: "my description",
Requirement: Requirement{
Kibana{
Min: "1.2.3",
Max: "4.5.6",
},
},
},
true,
"complete",
Expand Down

0 comments on commit 29a05e3

Please sign in to comment.