Skip to content

Commit

Permalink
Add source.tag_regex
Browse files Browse the repository at this point in the history
Match arbitrary tags. Takes precendence over pre_releases.
  • Loading branch information
cjnosal committed Jun 9, 2023
1 parent e47fbbd commit 35d5346
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ differences:
on digest).
</td>
</tr>
<tr>
<td><code>tag_regex</code> <em>(Optional)</em></td>
<td>
Monitor semver tags that match the provided regular expression.
Results are sorted in semver order.
<br>
Takes precedence over <code>pre_releases</code>
</td>
</tr>
<tr>
<td><code>variant</code> <em>(Optional)</em></td>
<td>
Expand Down
57 changes: 57 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,60 @@ var _ = DescribeTable("tracking semver tags",
Versions: []string{"1.0.0", "1.2.1", "2.0.0"},
},
),
Entry("tag regex for final versions",
SemverTagCheckExample{
Tags: map[string]string{
"1.0.0": "random-1",
"1.0.0-alpha.1": "random-2",
"1.2.1": "random-3",
"2.0.0": "random-5",
},

TagRegex: "^[0-9].[0-9].[0-9]$",

Versions: []string{
"1.0.0",
"1.2.1",
"2.0.0",
},
},
),
Entry("tag regex for arbitrary prerelease",
SemverTagCheckExample{
Tags: map[string]string{
"1.0.0": "random-1",
"1.0.0-alpha.1": "random-2",
"1.2.0-dev.1-built.123": "random-3",
"1.2.1": "random-4",
"2.0.0": "random-5",
},

TagRegex: "^[0-9]\\.[0-9]\\.[0-9]-dev\\.[0-9]+.built\\.[0-9]+$",

Versions: []string{
"1.2.0-dev.1-built.123",
},
},
),
Entry("combine tag regex, semver constraint, and variant",
SemverTagCheckExample{
Tags: map[string]string{
"1.1.0-dev.1-built.123-foo": "random-1",
"1.2.0-dev.1-built.123-foo": "random-2",
"1.3.0-dev.1-built.123-foo": "random-3",
"1.3.0-dev.1-built.123-bar": "random-4",
},

TagRegex: "^[0-9]\\.[0-9]\\.[0-9]-dev\\.[0-9]+.built\\.[0-9]+.*$",
Variant: "foo",
SemverConstraint: ">= 1.2.0-0",

Versions: []string{
"1.2.0-dev.1-built.123-foo",
"1.3.0-dev.1-built.123-foo",
},
},
),
)

type SemverTagCheckExample struct {
Expand All @@ -1122,6 +1176,8 @@ type SemverTagCheckExample struct {

SemverConstraint string

TagRegex string

Repository string
RegistryMirror string
WorkingMirror bool
Expand Down Expand Up @@ -1159,6 +1215,7 @@ func (example SemverTagCheckExample) Run() {
PreReleasePrefixes: example.PreReleasePrefixes,
Variant: example.Variant,
SemverConstraint: example.SemverConstraint,
TagRegex: example.TagRegex,
},
}

Expand Down
55 changes: 35 additions & 20 deletions commands/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"sort"
"strings"
"regexp"

"github.com/Masterminds/semver/v3"
resource "github.com/concourse/registry-image-resource"
Expand Down Expand Up @@ -137,6 +138,14 @@ func checkRepository(repo name.Repository, source resource.Source, from *resourc
}
}

var regex *regexp.Regexp
if source.TagRegex != "" {
regex, err = regexp.Compile(source.TagRegex)
if err != nil {
return resource.CheckResponse{}, fmt.Errorf("parse tag regex: %w", err)
}
}

for _, identifier := range tags {
var ver *semver.Version
if identifier == bareTag {
Expand All @@ -162,32 +171,38 @@ func checkRepository(repo name.Repository, source resource.Source, from *resourc
continue
}

pre := ver.Prerelease()
if pre != "" {
// pre-releases not enabled; skip
if !source.PreReleases {
if regex != nil {
if ! regex.MatchString(identifier) {
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
} else {
pre := ver.Prerelease()
if pre != "" {
// pre-releases not enabled; skip
if !source.PreReleases {
continue
}
}

match := false
for _, prefix := range preReleasePrefixes {
if strings.HasPrefix(pre, prefix) {
match = true
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
}
}

match := false
for _, prefix := range preReleasePrefixes {
if strings.HasPrefix(pre, prefix) {
match = true
}
}
if ! match {
// additional variant, not a prerelease segment
continue
}
}
if ! match {
// additional variant, not a prerelease segment
continue
}
}

Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type Source struct {
SemverConstraint string `json:"semver_constraint,omitempty"`

Tag Tag `json:"tag,omitempty"`
TagRegex string `json:"tag_regex,omitempty"`

BasicCredentials
AwsCredentials
Expand Down

0 comments on commit 35d5346

Please sign in to comment.