Skip to content

Commit

Permalink
feat: Add calver modifier and prerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
David MICHENEAU committed Oct 30, 2024
1 parent 1025b73 commit 2edef2d
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 66 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type (
Name string `json:"name"`

// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=calver-major;calver-minor;calver-patch;semver-major;semver-minor;semver-patch;regex;always
// +kubebuilder:validation:Enum=calver-major;calver-minor;calver-patch;calver-modifier;calver-prerelease;semver-major;semver-minor;semver-patch;regex;always
Type rules.Name `json:"type"`

// +kubebuilder:validation:Optional
Expand Down
52 changes: 48 additions & 4 deletions docs/rules/calver.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,80 @@ hide:
The `calver` rule allows you to define a rule that will be executed when the image is updated with a new calver version.
It follows the [Calendar Versioning](https://calver.org/) specification.
A lot of options are available to match the version you want to update.
The format of the version [following this logic regex](https://regex101.com/r/25eVYJ/2).

The format of the version [following this logic regex](https://regex101.com/r/25eVYJ/7).
```regex
^([0-9]{4}|[0-9]{2})(\.[0-9]{1,2})?(\.[0-9]{1,2})?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?$
```

Format:
```s
# YYYY.MM.XX
2024
2024-dev
2024.01
2024.01-dev
2024.01.01
2024.01.01-dev
# YY.MM.XX
24
24-dev
24.01
24.01-dev
# YY.M.X
24.1
24.1.1
24.1.1-dev
2024.01.01-dev.prerelease
```


* `calver-major`: Update the image with the latest major version.
* `calver-minor`: Update the image with the latest minor version.
* `calver-patch`: Update the image with the latest patch version.
* `calver-modifier`: Update the image with the latest modifier version.(dev, alpha, beta, rc, etc.)
* `calver-prerelease`: Update the image with the latest prerelease version.

**`calver-major`** is the most restrictive and will only update the image when the major version is updated [calver documentation](https://calver.org/).
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.*.* # (1)
Match: >=2025.*.* # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).

**`calver-minor`** is less restrictive and will update the image when the minor version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.1.* <2 # (1)
Match: >=2024.1.* # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).

**`calver-patch`** is the least restrictive and will update the image when the patch version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.0.1 <2024.1.0 # (1)
Match: >=2024.0.1 and < 2024.1.0 # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).

**`calver-modifier`** is the least restrictive and will update the image when the modifier version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0-dev
Match: >2024.0.0-dev and < 2024.0.1-* # (1)
```

**`calver-prerelease`** is the least restrictive and will update the image when the prerelease version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0-dev.1
Match: >2024.0.0-dev.1 and < 2024.0.0-*.1 #
2. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).
## Who to use
Create an `Image` resource with the `calver` rule.
Expand Down
55 changes: 55 additions & 0 deletions internal/rules/calver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@ type (
calverPatch struct {
rule
}

// calverModifier - The modifier is an optional part of the version.
calverModifier struct {
rule
}

// calverPrerelease - The prerelease is an optional part of the version.
calverPrerelease struct {
rule
}
)

func init() {
register(CalverMajor, &calverMajor{})
register(CalverMinor, &calverMinor{})
register(CalverPatch, &calverPatch{})
register(CalverModifier, &calverModifier{})
register(CalverPrerelease, &calverPrerelease{})
}

func (c *calverMajor) Evaluate() (matchWithRule bool, newTag string, err error) {
Expand Down Expand Up @@ -94,3 +106,46 @@ func (c *calverPatch) Evaluate() (matchWithRule bool, newTag string, err error)

return false, "", nil
}

func (c *calverModifier) Evaluate() (matchWithRule bool, newTag string, err error) {
actualCV, err := vc.NewCalVerStr(c.actualTag)
if err != nil {
log.WithError(err).WithField("tag", c.actualTag).Error("Error parsing actual tag")
return false, "", err
}

for _, t := range c.tags {
cv, err := vc.NewCalVerStr(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing tag")
continue
}
if cv.Gt(actualCV) && cv.Patch() == actualCV.Patch() && cv.Minor() == actualCV.Minor() && cv.Major() == actualCV.Major() {
return true, t, nil
}
}

return false, "", nil
}

func (c *calverPrerelease) Evaluate() (matchWithRule bool, newTag string, err error) {
actualCV, err := vc.NewCalVerStr(c.actualTag)
if err != nil {
log.WithError(err).WithField("tag", c.actualTag).Error("Error parsing actual tag")
return false, "", err
}

for _, t := range c.tags {
cv, err := vc.NewCalVerStr(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing tag")
continue
}

if cv.Prerelease() > actualCV.Prerelease() && cv.Patch() == actualCV.Patch() && cv.Minor() == actualCV.Minor() && cv.Major() == actualCV.Major() {
return true, t, nil
}
}

return false, "", nil
}
Loading

0 comments on commit 2edef2d

Please sign in to comment.