Skip to content

Commit

Permalink
Ignore deprecated release tag from manifests (#647)
Browse files Browse the repository at this point in the history
Ignore release in manifests and rely on semantic versioning for the release
state of a package.

Use the prerelease parameter when searching for packages in the registry.
  • Loading branch information
jsoriano authored Feb 11, 2022
1 parent 73c24db commit f661b6f
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.17.3
1.17.6
11 changes: 0 additions & 11 deletions cmd/create_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type newPackageAnswers struct {
Title string
Description string
Categories []string
Release string
KibanaVersion string `survey:"kibana_version"`
GithubOwner string `survey:"github_owner"`
}
Expand Down Expand Up @@ -78,15 +77,6 @@ func createPackageCommandAction(cmd *cobra.Command, args []string) error {
},
Validate: survey.Required,
},
{
Name: "release",
Prompt: &survey.Select{
Message: "Release:",
Options: []string{"experimental", "beta", "ga"},
Default: "experimental",
},
Validate: survey.Required,
},
{
Name: "kibana_version",
Prompt: &survey.Input{
Expand Down Expand Up @@ -136,7 +126,6 @@ func createPackageDescriptorFromAnswers(answers newPackageAnswers) archetype.Pac
Owner: packages.Owner{
Github: answers.GithubOwner,
},
Release: answers.Release,
Description: answers.Description,
License: "basic",
Categories: answers.Categories,
Expand Down
55 changes: 52 additions & 3 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strings"

"github.com/Masterminds/semver"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
Expand Down Expand Up @@ -60,7 +61,10 @@ func statusCommandAction(cmd *cobra.Command, args []string) error {
options := registry.SearchOptions{
All: showAll,
KibanaVersion: kibanaVersion,
Experimental: true,
Prerelease: true,

// Deprecated, keeping for compatibility with older versions of the registry.
Experimental: true,
}
packageStatus, err := getPackageStatus(packageName, options)
if err != nil {
Expand Down Expand Up @@ -127,7 +131,7 @@ func print(p *status.PackageStatus, w io.Writer) error {
}

bold.Fprintln(w, "Package Versions:")
table := tablewriter.NewWriter(os.Stdout)
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Environment", "Version", "Release", "Title", "Description"})
table.SetHeaderColor(
twColor(tablewriter.Colors{tablewriter.Bold}),
Expand Down Expand Up @@ -182,7 +186,7 @@ func formatManifest(environment string, manifest packages.PackageManifest, extra
if len(extraVersions) > 0 {
version = fmt.Sprintf("%s (%s)", version, strings.Join(extraVersions, ", "))
}
return []string{environment, version, manifest.Release, manifest.Title, manifest.Description}
return []string{environment, version, releaseFromVersion(manifest.Version), manifest.Title, manifest.Description}
}

// twColor no-ops the color setting if we don't want to colorize the output
Expand All @@ -192,3 +196,48 @@ func twColor(colors tablewriter.Colors) tablewriter.Colors {
}
return colors
}

// releaseFromVersion returns the human-friendly release level based on semantic versioning conventions.
// It does a best-effort mapping, it doesn't do validation.
func releaseFromVersion(version string) string {
const (
previewVersionText = "Technical Preview"
betaVersionText = "Beta"
releaseCandidateText = "Release Candidate"
gaVersion = "GA"
defaultText = betaVersionText
)

conventionPrereleasePrefixes := []struct {
prefix string
text string
}{
{"beta", betaVersionText},
{"rc", releaseCandidateText},
{"preview", previewVersionText},
}

sv, err := semver.NewVersion(version)
if err != nil {
// Ignoring errors on version parsing here, use best-effort defaults.
if strings.HasPrefix(version, "0.") {
return previewVersionText
}
return defaultText
}

if sv.Major() == 0 {
return previewVersionText
}
if sv.Prerelease() == "" {
return gaVersion
}

for _, convention := range conventionPrereleasePrefixes {
if strings.HasPrefix(sv.Prerelease(), convention.prefix) {
return convention.text
}
}

return defaultText
}
132 changes: 132 additions & 0 deletions cmd/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package cmd

import (
"bytes"
"flag"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/packages/status"
)

var generateFlag = flag.Bool("generate", false, "Write golden files")

func fooPackage(version string) packages.PackageManifest {
return packages.PackageManifest{
Name: "foo",
Version: version,
Title: "Foo",
Description: "Foo integration",
}
}

func TestStatusFormatAndPrint(t *testing.T) {
cases := []struct {
title string
pkgStatus *status.PackageStatus
expected string
}{
{
title: "no versions",
pkgStatus: &status.PackageStatus{Name: "foo"},
expected: "./testdata/status-no-versions",
},
{
title: "version-one-stage",
pkgStatus: &status.PackageStatus{
Name: "foo",
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
},
},
expected: "./testdata/status-version-one-stage",
},
{
title: "some versions",
pkgStatus: &status.PackageStatus{
Name: "foo",
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
},
Staging: []packages.PackageManifest{
fooPackage("1.1.0-beta1"),
},
Snapshot: []packages.PackageManifest{
fooPackage("2.0.0-rc1"),
},
},
expected: "./testdata/status-some-versions",
},
{
title: "preview versions",
pkgStatus: &status.PackageStatus{
Name: "foo",
Production: []packages.PackageManifest{
fooPackage("0.9.0"),
},
Staging: []packages.PackageManifest{
fooPackage("1.0.0-preview1"),
},
Snapshot: []packages.PackageManifest{
fooPackage("1.0.0-preview5"),
},
},
expected: "./testdata/status-preview-versions",
},
{
title: "multiple versions in stage",
pkgStatus: &status.PackageStatus{
Name: "foo",
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
},
Staging: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
fooPackage("1.1.0-beta1"),
},
Snapshot: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
fooPackage("1.1.0-beta1"),
fooPackage("2.0.0-rc1"),
},
},
expected: "./testdata/status-multiple-versions-in-stage",
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
var buf bytes.Buffer
err := print(c.pkgStatus, &buf)
require.NoError(t, err)

assertOutputWithFile(t, c.expected, buf.String())
})
}
}

func assertOutputWithFile(t *testing.T, path string, out string) {
if *generateFlag {
err := ioutil.WriteFile(path, []byte(out), 0644)
require.NoError(t, err)
}

d, err := ioutil.ReadFile(path)
require.NoError(t, err)

assert.Equal(t, string(d), out)
}
13 changes: 13 additions & 0 deletions cmd/testdata/status-multiple-versions-in-stage
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: foo
Package Versions:
+-------------+--------------------------------+-------------------+-------+-----------------+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION |
+-------------+--------------------------------+-------------------+-------+-----------------+
| Snapshot | 2.0.0-rc1 (1.0.0, 1.0.1, | Release Candidate | Foo | Foo integration |
| | 1.0.2, 1.1.0-beta1) | | | |
+-------------+--------------------------------+-------------------+-------+-----------------+
| Staging | 1.1.0-beta1 (1.0.0, 1.0.1, | Beta | Foo | Foo integration |
| | 1.0.2) | | | |
+-------------+--------------------------------+-------------------+-------+-----------------+
| Production | 1.0.2 (1.0.0, 1.0.1) | GA | Foo | Foo integration |
+-------------+--------------------------------+-------------------+-------+-----------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-no-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: foo
Package Versions:
+-------------+---------+---------+-------+-------------+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION |
+-------------+---------+---------+-------+-------------+
| Snapshot | - | - | - | - |
+-------------+---------+---------+-------+-------------+
| Staging | - | - | - | - |
+-------------+---------+---------+-------+-------------+
| Production | - | - | - | - |
+-------------+---------+---------+-------+-------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-preview-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: foo
Package Versions:
+-------------+----------------+-------------------+-------+-----------------+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION |
+-------------+----------------+-------------------+-------+-----------------+
| Snapshot | 1.0.0-preview5 | Technical Preview | Foo | Foo integration |
+-------------+----------------+-------------------+-------+-----------------+
| Staging | 1.0.0-preview1 | Technical Preview | Foo | Foo integration |
+-------------+----------------+-------------------+-------+-----------------+
| Production | 0.9.0 | Technical Preview | Foo | Foo integration |
+-------------+----------------+-------------------+-------+-----------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-some-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: foo
Package Versions:
+-------------+-------------+-------------------+-------+-----------------+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION |
+-------------+-------------+-------------------+-------+-----------------+
| Snapshot | 2.0.0-rc1 | Release Candidate | Foo | Foo integration |
+-------------+-------------+-------------------+-------+-----------------+
| Staging | 1.1.0-beta1 | Beta | Foo | Foo integration |
+-------------+-------------+-------------------+-------+-----------------+
| Production | 1.0.0 | GA | Foo | Foo integration |
+-------------+-------------+-------------------+-------+-----------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-version-one-stage
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: foo
Package Versions:
+-------------+---------+---------+-------+-----------------+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION |
+-------------+---------+---------+-------+-----------------+
| Snapshot | - | - | - | - |
+-------------+---------+---------+-------+-----------------+
| Staging | - | - | - | - |
+-------------+---------+---------+-------+-----------------+
| Production | 1.0.0 | GA | Foo | Foo integration |
+-------------+---------+---------+-------+-----------------+
1 change: 0 additions & 1 deletion internal/packages/archetype/package_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type: {{.Manifest.Type}}
categories:{{range $category := .Manifest.Categories}}
- {{$category}}
{{- end}}
release: {{.Manifest.Release}}
conditions:
kibana.version: "{{.Manifest.Conditions.Kibana.Version}}"
screenshots:
Expand Down
8 changes: 0 additions & 8 deletions internal/packages/archetype/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ func TestPackage(t *testing.T) {
pd := createPackageDescriptorForTest()
pd.Manifest.Version = ""

err := createAndCheckPackage(t, pd)
require.Error(t, err)
})
t.Run("missing-release", func(t *testing.T) {
pd := createPackageDescriptorForTest()
pd.Manifest.Release = ""

err := createAndCheckPackage(t, pd)
require.Error(t, err)
})
Expand Down Expand Up @@ -74,7 +67,6 @@ func createPackageDescriptorForTest() PackageDescriptor {
Owner: packages.Owner{
Github: "mtojek",
},
Release: "experimental",
Description: "This package has been generated by a Go unit test.",
License: "basic",
Categories: []string{"aws", "custom"},
Expand Down
1 change: 0 additions & 1 deletion internal/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ type PackageManifest struct {
PolicyTemplates []PolicyTemplate `config:"policy_templates" json:"policy_templates" yaml:"policy_templates"`
Vars []Variable `config:"vars" json:"vars" yaml:"vars"`
Owner Owner `config:"owner" json:"owner" yaml:"owner"`
Release string `config:"release" json:"release" yaml:"release"`
Description string `config:"description" json:"description" yaml:"description"`
License string `config:"license" json:"license" yaml:"license"`
Categories []string `config:"categories" json:"categories" yaml:"categories"`
Expand Down
5 changes: 4 additions & 1 deletion internal/registry/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import (

// SearchOptions specify the query parameters without the package name for the search API
type SearchOptions struct {
Experimental bool `url:"experimental"`
Prerelease bool `url:"prerelease"`
All bool `url:"all"`
KibanaVersion string `url:"kibana.version,omitempty"`

// Deprecated
Experimental bool `url:"experimental"`
}

// searchQuery specify the package and query parameters for the search API
Expand Down

0 comments on commit f661b6f

Please sign in to comment.