Skip to content

Commit

Permalink
Add source.pre_release_prefixes
Browse files Browse the repository at this point in the history
Allow the user to opt into checking for prefixes other than the default
alpha/beta/rc.
Opting in additionally allows for prereleases containing '-' delimeters as
allowed by Semver 2.0.0.
  • Loading branch information
cjnosal committed Jun 8, 2023
1 parent 67632cc commit e47fbbd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ differences:
Note however that variants and pre-releases both use the same syntax:
`1.2.3-alpine` is technically also valid syntax for a Semver prerelease. For
this reason, the resource will only consider prerelease data starting with
`alpha`, `beta`, or `rc` as a proper prerelease, treating anything else as
a variant.
`alpha`, `beta`, or `rc` as a proper prerelease, or values provided by
`pre_release_prefixes`, treating anything else as a variant.
</td>
</tr>
<tr>
<td><code>pre_release_prefixes</code> <em>(Optional)</em></td>
<td>
Opt in to additional pre-release prefixes (e.g. `build.3`)
</td>
</tr>
<tr>
Expand Down
62 changes: 58 additions & 4 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,30 @@ var _ = DescribeTable("tracking semver tags",
},
},
),
Entry("prerelease prefixes opted in",
SemverTagCheckExample{
Tags: map[string]string{
"1.0.0-alpha.1": "random-0",
"1.0.0": "random-1",
"1.2.1-beta.1": "random-2",
"1.2.1": "random-3",
"2.0.0-rc.1": "random-4",
"2.0.0": "random-5",
"2.0.0-build.1": "random-6",
},
PreReleases: true,
PreReleasePrefixes: []string{"build"},
Versions: []string{
"1.0.0-alpha.1",
"1.0.0",
"1.2.1-beta.1",
"1.2.1",
"2.0.0-build.1",
"2.0.0-rc.1",
"2.0.0",
},
},
),
Entry("prereleases do not include 'variants'",
SemverTagCheckExample{
Tags: map[string]string{
Expand Down Expand Up @@ -1032,6 +1056,34 @@ var _ = DescribeTable("tracking semver tags",
},
},
),
Entry("opting in to prereleases allows additional '-' suffixes before variant",
SemverTagCheckExample{
Tags: map[string]string{
"1.0.0-build-foo": "random-1",
"1.0.0-rc.1-foo": "random-2",
"1.0.0-alpha.1-foo": "random-3",
"1.0.0-beta.1-foo": "random-4",
"1.0.0-bar-foo": "random-5",
"1.0.0-rc.1-bar-foo": "random-6",
"1.0.0-alpha.1-bar-foo": "random-7",
"1.0.0-beta.1-bar-foo": "random-8",
},

Variant: "foo",
PreReleases: true,
PreReleasePrefixes: []string{"build"},

Versions: []string{
"1.0.0-alpha.1-foo",
"1.0.0-alpha.1-bar-foo",
"1.0.0-beta.1-foo",
"1.0.0-beta.1-bar-foo",
"1.0.0-build-foo",
"1.0.0-rc.1-foo",
"1.0.0-rc.1-bar-foo",
},
},
),
Entry("tries mirror and falls back on original repository",
SemverTagCheckExample{
Tags: map[string]string{
Expand Down Expand Up @@ -1065,6 +1117,7 @@ type SemverTagCheckExample struct {
Tags map[string]string

PreReleases bool
PreReleasePrefixes []string
Variant string

SemverConstraint string
Expand Down Expand Up @@ -1101,10 +1154,11 @@ func (example SemverTagCheckExample) Run() {

req := resource.CheckRequest{
Source: resource.Source{
Repository: repo.Name(),
PreReleases: example.PreReleases,
Variant: example.Variant,
SemverConstraint: example.SemverConstraint,
Repository: repo.Name(),
PreReleases: example.PreReleases,
PreReleasePrefixes: example.PreReleasePrefixes,
Variant: example.Variant,
SemverConstraint: example.SemverConstraint,
},
}

Expand Down
21 changes: 15 additions & 6 deletions commands/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,23 @@ func checkRepository(repo name.Repository, source resource.Source, from *resourc
continue
}

// contains additional variant
if strings.Contains(pre, "-") {
continue
preReleasePrefixes := []string{"alpha", "beta", "rc"}
if source.PreReleasePrefixes != nil && len(source.PreReleasePrefixes) > 0 {
preReleasePrefixes = append(preReleasePrefixes, source.PreReleasePrefixes...)
} else {
if strings.Contains(pre, "-") {
// contains additional variant
continue
}
}

if !strings.HasPrefix(pre, "alpha") &&
!strings.HasPrefix(pre, "beta") &&
!strings.HasPrefix(pre, "rc") {
match := false
for _, prefix := range preReleasePrefixes {
if strings.HasPrefix(pre, prefix) {
match = true
}
}
if ! match {
// additional variant, not a prerelease segment
continue
}
Expand Down
5 changes: 3 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ type Source struct {

Insecure bool `json:"insecure"`

PreReleases bool `json:"pre_releases,omitempty"`
Variant string `json:"variant,omitempty"`
PreReleases bool `json:"pre_releases,omitempty"`
PreReleasePrefixes []string `json:"pre_release_prefixes,omitempty"`
Variant string `json:"variant,omitempty"`

SemverConstraint string `json:"semver_constraint,omitempty"`

Expand Down

0 comments on commit e47fbbd

Please sign in to comment.