Skip to content

Commit

Permalink
Merge pull request #276 from concourse/semver-constraint
Browse files Browse the repository at this point in the history
allow setting a semver constraint
  • Loading branch information
aoldershaw authored Apr 29, 2021
2 parents 7e65e92 + d5477c4 commit 9f1b2aa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ differences:
would be used for tags like `1.2.3-stretch`. This is typically used *without*
`tag` - if it is set, this value will only used for pushing, not checking.

* `semver_constraint`: *Optional.* Constrain the returned semver tags according
to a semver constraint, e.g. `"~1.2.x"`, `">= 1.2 < 3.0.0 || >= 4.2.3"`.
Follows the rules outlined in https://github.com/Masterminds/semver#checking-version-constraints.

* `username` and `password`: *Optional.* A username and password to use when
authenticating to the registry. Must be specified for private repos or when
using `put`.
Expand Down
23 changes: 20 additions & 3 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,20 @@ var _ = DescribeTable("tracking semver tags",
Versions: []string{"1.0.0", "1.2.1", "2.0.0"},
},
),
Entry("semver constraint",
SemverTagCheckExample{
Tags: map[string]string{
"1.0.0": "random-1",
"1.2.1": "random-3",
"1.2.2": "random-4",
"2.0.0": "random-5",
// Does not include bare tag
"latest": "random-6",
},
SemverConstraint: "1.2.x",
Versions: []string{"1.2.1", "1.2.2"},
},
),
Entry("prereleases ignored by default",
SemverTagCheckExample{
Tags: map[string]string{
Expand Down Expand Up @@ -1109,6 +1123,8 @@ type SemverTagCheckExample struct {
PreReleases bool
Variant string

SemverConstraint string

Repository string
RegistryMirror string
WorkingMirror bool
Expand Down Expand Up @@ -1141,9 +1157,10 @@ func (example SemverTagCheckExample) Run() {

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

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

var constraint *semver.Constraints
if source.SemverConstraint != "" {
constraint, err = semver.NewConstraint(source.SemverConstraint)
if err != nil {
return resource.CheckResponse{}, fmt.Errorf("parse semver constraint: %w", err)
}
}

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

if constraint != nil && !constraint.Check(ver) {
// semver constraint not met
continue
}

pre := ver.Prerelease()
if pre != "" {
// pre-releases not enabled; skip
Expand Down Expand Up @@ -246,7 +259,7 @@ func checkRepository(repo name.Repository, source resource.Source, from *resourc
digest := tagDigests[latestTag]

_, existsAsSemver := digestVersions[digest]
if !existsAsSemver {
if !existsAsSemver && constraint == nil {
response = append(response, resource.Version{
Tag: latestTag,
Digest: digest,
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type Source struct {
PreReleases bool `json:"pre_releases,omitempty"`
Variant string `json:"variant,omitempty"`

SemverConstraint string `json:"semver_constraint,omitempty"`

Tag Tag `json:"tag,omitempty"`

BasicCredentials
Expand Down

0 comments on commit 9f1b2aa

Please sign in to comment.