Skip to content

Commit

Permalink
Use semver from masterminds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Oct 23, 2023
1 parent 0dcca81 commit 63a4025
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/metal-stack/metal-robot
go 1.21

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/atedja/go-multilock v0.0.0-20170315063113-31d195f255fb
github.com/blang/semver/v4 v4.0.0
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0
github.com/go-git/go-billy/v5 v5.5.0
// IMPORTANT: keep this version as long as https://github.com/go-git/go-git/issues/328 is open
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
Expand All @@ -51,8 +53,6 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/atedja/go-multilock v0.0.0-20170315063113-31d195f255fb h1:osgVLyVcO3XUghYODTtWUjV7O+vmwX70MMtUO2Jiq9E=
github.com/atedja/go-multilock v0.0.0-20170315063113-31d195f255fb/go.mod h1:fFkhuoU3CiRTrgW+OwPwWffezYpVbEF5CREAwKuieRc=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0 h1:yUmoVv70H3J4UOqxqsee39+KlXxNEDfTbAp8c/qULKk=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0/go.mod h1:fmPmvCiBWhJla3zDv9ZTQSZc8AbwyRnGW1yg5ep1Pcs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
9 changes: 7 additions & 2 deletions pkg/webhooks/github/actions/aggregate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"errors"

"github.com/Masterminds/semver/v3"
"github.com/atedja/go-multilock"
"github.com/blang/semver/v4"
v3 "github.com/google/go-github/v56/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
Expand Down Expand Up @@ -113,12 +113,17 @@ func (r *AggregateReleases) AggregateRelease(ctx context.Context, p *AggregateRe

tag := p.TagName
trimmed := strings.TrimPrefix(tag, "v")
_, err := semver.Make(trimmed)
parsedVersion, err := semver.NewVersion(trimmed)
if err != nil {
r.logger.Infow("skip applying release actions to aggregation repo because not a valid semver release tag", "target-repo", r.repoName, "source-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

if parsedVersion.Prerelease() != "" {
r.logger.Infow("skip applying release actions to aggregation repo because is a pre-release", "target-repo", r.repoName, "source-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

// preventing concurrent git repo modifications
var once sync.Once
r.lock.Lock()
Expand Down
15 changes: 7 additions & 8 deletions pkg/webhooks/github/actions/release_drafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"strings"

"github.com/blang/semver/v4"
"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v56/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
Expand Down Expand Up @@ -142,7 +142,7 @@ func (r *releaseDrafter) draft(ctx context.Context, p *releaseDrafterParams) err
return nil
}
trimmedVersion := strings.TrimPrefix(componentTag, "v")
componentSemver, err := semver.Parse(trimmedVersion)
componentSemver, err := semver.NewVersion(trimmedVersion)
if err != nil {
r.logger.Debugw("skip adding release draft because tag is not semver compatible", "repo", p.RepositoryName, "release", componentTag)
return nil //nolint:nilerr
Expand All @@ -158,7 +158,7 @@ func (r *releaseDrafter) draft(ctx context.Context, p *releaseDrafterParams) err
return r.createOrUpdateRelease(ctx, infos, body, p)
}

func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, component string, componentVersion semver.Version, componentBody *string, releaseURL string) string {
func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, component string, componentVersion *semver.Version, componentBody *string, releaseURL string) string {
m := markdown.Parse(priorBody)

releaseSection := ensureReleaseSection(m, r.draftHeadline)
Expand Down Expand Up @@ -213,8 +213,8 @@ func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, compone
groups := utils.RegexCapture(utils.SemanticVersionMatcher, section.Heading)
old := groups["full_match"]
old = strings.TrimPrefix(old, "v")
oldVersion, err := semver.Parse(old)
if err == nil && componentVersion.GT(oldVersion) {
oldVersion, err := semver.NewVersion(old)
if err == nil && componentVersion.GreaterThan(oldVersion) {
// in this case we need to merge contents together and update the headline
section.Heading = heading
section.AppendContent(body)
Expand Down Expand Up @@ -404,12 +404,11 @@ func (r *releaseDrafter) guessNextVersionFromLatestRelease(ctx context.Context)
groups := utils.RegexCapture(utils.SemanticVersionMatcher, *latest.TagName)
t := groups["full_match"]
t = strings.TrimPrefix(t, "v")
latestTag, err := semver.Parse(t)
latestTag, err := semver.NewVersion(t)
if err != nil {
r.logger.Warnw("latest release of repository was not a semver tag", "repository", r.repoName, "latest-tag", *latest.TagName)
} else {
latestTag.Patch = latestTag.Patch + 1
return "v" + latestTag.String(), nil
return "v" + latestTag.IncPatch().String(), nil
}
}
return "v0.0.1", nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhooks/github/actions/release_drafter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package actions
import (
"testing"

"github.com/blang/semver/v4"
"github.com/Masterminds/semver/v3"
"github.com/google/go-cmp/cmp"
v3 "github.com/google/go-github/v56/github"
"go.uber.org/zap/zaptest"
Expand All @@ -16,7 +16,7 @@ func TestReleaseDrafter_updateReleaseBody(t *testing.T) {
headline string
priorBody string
component string
componentVersion semver.Version
componentVersion *semver.Version
componentBody *string
releaseURL string

Expand Down
4 changes: 2 additions & 2 deletions pkg/webhooks/github/actions/yaml_translate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"errors"

"github.com/Masterminds/semver/v3"
"github.com/atedja/go-multilock"
"github.com/blang/semver/v4"
v3 "github.com/google/go-github/v56/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (r *yamlTranslateReleases) translateRelease(ctx context.Context, p *yamlTra

tag := p.TagName
trimmed := strings.TrimPrefix(tag, "v")
_, err := semver.Make(trimmed)
_, err := semver.NewVersion(trimmed)
if err != nil {
r.logger.Infow("skip applying translate release actions to aggregation repo because not a valid semver release tag", "target-repo", r.repoName, "source-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
Expand Down
8 changes: 4 additions & 4 deletions pkg/webhooks/modifiers/file-patchers/yaml_path_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/blang/semver/v4"
"github.com/Masterminds/semver/v3"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/metal-stack/metal-robot/pkg/utils"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (p YAMLPathPatch) Apply(cn ContentReader, cw ContentWriter, newValue string
if p.versionCompare {
trimmedValue := strings.TrimPrefix(newValue, "v")

newVersion, err := semver.Parse(trimmedValue)
newVersion, err := semver.NewVersion(trimmedValue)
if err != nil {
return err
}
Expand All @@ -73,9 +73,9 @@ func (p YAMLPathPatch) Apply(cn ContentReader, cw ContentWriter, newValue string
}

old = strings.TrimPrefix(old, "v")
oldVersion, err := semver.Parse(old)
oldVersion, err := semver.NewVersion(old)
if err == nil {
if !newVersion.GT(oldVersion) {
if !newVersion.GreaterThan(oldVersion) {
return nil
}
}
Expand Down

0 comments on commit 63a4025

Please sign in to comment.