From c827d3246da004c3d89caa336b4e377d87c58eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Fri, 15 Nov 2024 18:30:02 +0100 Subject: [PATCH] feat(github): mark pre-releases correctly In theory every forge can support this, but right now only GitHub allows one to define a release as "pre-release". Closes #45 --- internal/versioning/semver.go | 13 ++++++++++++ internal/versioning/semver_test.go | 34 ++++++++++++++++++++++++++++++ internal/versioning/versioning.go | 1 + releaserpleaser.go | 4 ++-- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/versioning/semver.go b/internal/versioning/semver.go index e6a58c7..07e0fdc 100644 --- a/internal/versioning/semver.go +++ b/internal/versioning/semver.go @@ -110,3 +110,16 @@ func parseSemverWithDefault(tag *git.Tag) (semver.Version, error) { return parsedVersion, nil } + +func (s semVer) IsPrerelease(version string) bool { + semVersion, err := parseSemverWithDefault(&git.Tag{Hash: "", Name: version}) + if err != nil { + return false + } + + if len(semVersion.Pre) > 0 { + return true + } + + return false +} diff --git a/internal/versioning/semver_test.go b/internal/versioning/semver_test.go index 702d31c..936c258 100644 --- a/internal/versioning/semver_test.go +++ b/internal/versioning/semver_test.go @@ -388,3 +388,37 @@ func TestVersionBumpFromCommits(t *testing.T) { }) } } + +func TestSemVer_IsPrerelease(t *testing.T) { + tests := []struct { + name string + version string + want bool + }{ + { + name: "empty string", + version: "", + want: false, + }, + { + name: "stable version", + version: "v1.0.0", + want: false, + }, + { + name: "pre-release version", + version: "v1.0.0-rc.1+foo", + want: true, + }, + { + name: "invalid version", + version: "ajfkdafjdsfj", + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, SemVer.IsPrerelease(tt.version), "IsSemverPrerelease(%v)", tt.version) + }) + } +} diff --git a/internal/versioning/versioning.go b/internal/versioning/versioning.go index 182983f..a4915ff 100644 --- a/internal/versioning/versioning.go +++ b/internal/versioning/versioning.go @@ -8,6 +8,7 @@ import ( type Strategy interface { NextVersion(git.Releases, VersionBump, NextVersionType) (string, error) + IsPrerelease(version string) bool } type VersionBump conventionalcommits.VersionBump diff --git a/releaserpleaser.go b/releaserpleaser.go index 77f81f6..88b2dbb 100644 --- a/releaserpleaser.go +++ b/releaserpleaser.go @@ -122,10 +122,10 @@ func (rp *ReleaserPleaser) createPendingRelease(ctx context.Context, pr *release return err } - // TODO: pre-release & latest + // TODO: Check if version should be marked latest logger.DebugContext(ctx, "Creating release on forge") - err = rp.forge.CreateRelease(ctx, *pr.ReleaseCommit, version, changelogText, false, true) + err = rp.forge.CreateRelease(ctx, *pr.ReleaseCommit, version, changelogText, rp.versioning.IsPrerelease(version), true) if err != nil { return fmt.Errorf("failed to create release on forge: %w", err) }