Skip to content

Commit

Permalink
Deprecate the release tag (#256)
Browse files Browse the repository at this point in the history
It is optional now, and considered GA when not set.
Semver pre-release tags should be used now to publish non-ga versions.

More information in #225
  • Loading branch information
jsoriano authored Feb 3, 2022
1 parent 83d1d2b commit 2c21cae
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 12 deletions.
98 changes: 98 additions & 0 deletions code/go/internal/validator/semantic/validate_prerelease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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 semantic

import (
"fmt"
"regexp"
"strings"

"github.com/Masterminds/semver/v3"

ve "github.com/elastic/package-spec/code/go/internal/errors"
)

var (
// Prereleases allowed as literals.
literalPrereleases = []string{
// For convenience with previous recommendations
"next",
"SNAPSHOT",
}

// Prereleases allowed, potentially with additional numbering.
numberedPrereleases = []string{
"beta",
"rc",
"preview",
}
)

// ValidatePrerelease validates additional restrictions on the prerelease tags.
func ValidatePrerelease(pkgRoot string) ve.ValidationErrors {
manifestVersion, err := readManifestVersion(pkgRoot)
if err != nil {
return ve.ValidationErrors{err}
}

err = validatePrerelease(manifestVersion)
if err != nil {
return ve.ValidationErrors{err}
}

return nil
}

func validatePrerelease(manifestVersion string) error {
version, err := semver.NewVersion(manifestVersion)
if err != nil {
return err
}

if version.Major() == 0 && version.Prerelease() != "" {
return fmt.Errorf("versions below 1.0.0 are considered technical previews, please remove prerelease tag (version: %s)", manifestVersion)
}

if err := validatePrereleaseTag(version.Prerelease()); err != nil {
return err
}

return nil
}

// prereleaseNumberPattern is the pattern that the number after the prerelease tag must match.
// It has to start with a number, hyphen or dot, and end with number or letter.
const prereleaseNumberPattern = "(([0-9]|[.-][0-9A-Za-z])([0-9A-Za-z-.]*[0-9A-Za-z])?)?"

func validatePrereleaseTag(tag string) error {
if tag == "" {
return nil
}

for _, literal := range literalPrereleases {
if tag == literal {
return nil
}
}

for _, numbered := range numberedPrereleases {
if tag == numbered {
return nil
}

pattern := regexp.MustCompile(fmt.Sprintf("^%s%s$", numbered, prereleaseNumberPattern))
if pattern.MatchString(tag) {
return nil
}
}

return ve.ValidationErrors{
fmt.Errorf("prerelease tag (%s) should be one of [%s], or one of [%s] followed by numbers",
tag,
strings.Join(literalPrereleases, ", "),
strings.Join(numberedPrereleases, ", "),
),
}
}
59 changes: 59 additions & 0 deletions code/go/internal/validator/semantic/validate_prerelease_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 semantic

import (
"testing"

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

func TestValidatePrerelease(t *testing.T) {
cases := []struct {
version string
valid bool
}{
{"0.1.0", true},
{"0.1.0-beta", false},
{"0.1.0-rc1", false},
{"1.0.0-beta", true},
{"1.0.0-beta1", true},
{"1.0.0-beta-1.0", true},
{"1.0.0-beta.42", true},
{"1.0.0-beta.alpha", true},
{"1.0.0-beta+20220202", true},
{"1.0.0-beta2+20220202", true},
{"1.0.0-rc1", true},
{"1.0.0-preview1", true},
{"1.0.0-betapreview", false},
{"1.0.0-alphabeta", false},
{"1.0.0-123", false},
{"1.0.0-123", false},
{"1.0.0-beta.", false},
{"1.0.0-beta..", false},
{"1.0.0-beta-", false},
{"1.0.0-beta--", false},
{"1.0.0-beta---", false},
{"1.0.0-beta1.", false},
{"1.0.0-beta1-", false},
{"1.0.0-.", false},
{"1.0.0--", false},

// For convenience with previous recommendations
{"1.0.0-SNAPSHOT", true},
{"1.0.0-next", true},
}

for _, c := range cases {
t.Run(c.version, func(t *testing.T) {
err := validatePrerelease(c.version)
if c.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
1 change: 1 addition & 0 deletions code/go/internal/validator/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s Spec) ValidatePackage(pkg Package) ve.ValidationErrors {
rules := validationRules{
semantic.ValidateKibanaObjectIDs,
semantic.ValidateVersionIntegrity,
semantic.ValidatePrerelease,
semantic.ValidateFieldGroups,
semantic.ValidateDimensionFields,
}
Expand Down
7 changes: 6 additions & 1 deletion test/packages/good/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
- version: 1.0.0
changes:
- description: LTS version
type: enhancement
link: https://github.com/elastic/package-spec/pull/256
- version: 1.0.0-next
changes:
- description: release candidate
Expand All @@ -18,4 +23,4 @@
changes:
- description: initial release
type: enhancement
link: https://github.com/elastic/package-spec/pull/131
link: https://github.com/elastic/package-spec/pull/131
1 change: 0 additions & 1 deletion test/packages/good/data_stream/foo/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
title: Nginx access logs
type: logs
release: experimental
streams:
- input: logfile
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
title: Data stream using Kubernetes service deployer for tests
type: logs
release: experimental
streams:
- input: logfile
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
title: Data stream using Kubernetes service deployer for tests (without custom definitions)
type: logs
release: experimental
streams:
- input: logfile
vars:
Expand Down
3 changes: 1 addition & 2 deletions test/packages/good/data_stream/pe/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
title: pe sample
type: logs
release: experimental
streams:
- input: logfile
vars:
Expand All @@ -20,4 +19,4 @@ streams:
show_user: false
default: /server-status
title: Nginx access logs
description: Collect Nginx access logs
description: Collect Nginx access logs
5 changes: 2 additions & 3 deletions test/packages/good/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
format_version: 1.0.4
name: good
title: GOOD
title: Good package
description: This package is good.
version: 0.1.2
release: beta
version: 1.0.0
conditions:
kibana.version: '^7.9.0'
policy_templates:
Expand Down
6 changes: 6 additions & 0 deletions versions/1/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- description: Report error when a directory name inside a package contains hyphens (-).
type: enhancement
link: https://github.com/elastic/package-spec/pull/259
- description: Deprecate the release attribute, make it optional and set to ga by default.
type: enhancement
link: https://github.com/elastic/package-spec/pull/256
- description: Restrict the allowed prerelease labels in sematic versions.
type: breaking-change
link: https://github.com/elastic/package-spec/pull/256
- version: 1.3.0
changes:
- description: Support wait-for-data timeout
Expand Down
9 changes: 6 additions & 3 deletions versions/1/manifest.spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ spec:
- src
- title
version:
description: Version of the package, following semantic versioning. It can include pre-release labels.
type: string
pattern: '^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$'
examples:
- "1.0.0"
- "1.0.0-beta1"
- "1.0.0-SNAPSHOT"
- "1.0.0-dev.1"
- "1.0.0-next"
properties:
format_version:
description: The version of the package specification format used by this package.
Expand Down Expand Up @@ -144,12 +146,14 @@ spec:
examples:
- integration
release:
description: The stability of the package.
description: The stability of the package (deprecated, use prerelease tags in the version).
deprecated: true # See https://github.com/elastic/package-spec/issues/225
type: string
enum:
- experimental
- beta
- ga
default: ga
examples:
- experimental
categories:
Expand Down Expand Up @@ -295,5 +299,4 @@ spec:
- title
- description
- version
- release
- owner

0 comments on commit 2c21cae

Please sign in to comment.