Skip to content

Commit

Permalink
Merge pull request #65 from rawkode/fix/support-build-version
Browse files Browse the repository at this point in the history
fix: support tags with build info
  • Loading branch information
guineveresaenger authored May 11, 2023
2 parents cfcf047 + f265ebc commit a2c5d77
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/gitversion/gitversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func GetLanguageVersionsWithOptions(opts LanguageVersionsOptions) (*LanguageVers
genericVersion.Major = versionComponents.Semver.Major
genericVersion.Minor = versionComponents.Semver.Minor
genericVersion.Patch = versionComponents.Semver.Patch
genericVersion.Build = versionComponents.Semver.Build

if len(versionComponents.Semver.Pre) == 1 {
genericVersion.Pre = []semver.PRVersion{
versionComponents.Semver.Pre[0],
Expand Down Expand Up @@ -130,12 +132,23 @@ func GetLanguageVersionsWithOptions(opts LanguageVersionsOptions) (*LanguageVers
preVersion = fmt.Sprintf("%s%sdirty", preVersion, separator)
}

buildVersion := []byte{}
if len(genericVersion.Build) > 0 {
buildVersion = append(buildVersion, '+')
buildVersion = append(buildVersion, genericVersion.Build[0]...)

for _, build := range genericVersion.Build[1:] {
buildVersion = append(buildVersion, '.')
buildVersion = append(buildVersion, build...)
}
}

// a base version with the pre release info
baseVersion := fmt.Sprintf("%d.%d.%d", genericVersion.Major, genericVersion.Minor, genericVersion.Patch)

// calculate versions for all languages
version := fmt.Sprintf("%s%s", baseVersion, preVersion)
pythonVersion := fmt.Sprintf("%s%s", baseVersion, pythonPreVersion)
version := fmt.Sprintf("%s%s%s", baseVersion, preVersion, buildVersion)
pythonVersion := fmt.Sprintf("%s%s%s", baseVersion, pythonPreVersion, buildVersion)
jsVersion := fmt.Sprintf("v%s", version)
dotnetVersion := version

Expand Down
48 changes: 48 additions & 0 deletions pkg/gitversion/gitversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,52 @@ func TestGetVersion(t *testing.T) {
require.Equal(t, "v1.0.0-alpha.1", version.JavaScript)
require.Equal(t, "1.0.0a1", version.Python)
})

t.Run("Repo with build info tag", func(t *testing.T) {
repo, err := testRepoCreate()
require.NoError(t, err)

tagSequence := []string{
"v1.0.0+1",
}

repo, err = testRepoWithTags(repo, tagSequence)
require.NoError(t, err)

opts := LanguageVersionsOptions{
Repo: repo,
Commitish: plumbing.Revision("HEAD"),
}
version, err := GetLanguageVersionsWithOptions(opts)
require.NoError(t, err)

require.Equal(t, "1.0.0+1", version.SemVer)
require.Equal(t, "1.0.0+1", version.DotNet)
require.Equal(t, "v1.0.0+1", version.JavaScript)
require.Equal(t, "1.0.0+1", version.Python)
})

t.Run("Repo with complicated build info tag", func(t *testing.T) {
repo, err := testRepoCreate()
require.NoError(t, err)

tagSequence := []string{
"v1.0.0+1abc.345.whoop",
}

repo, err = testRepoWithTags(repo, tagSequence)
require.NoError(t, err)

opts := LanguageVersionsOptions{
Repo: repo,
Commitish: plumbing.Revision("HEAD"),
}
version, err := GetLanguageVersionsWithOptions(opts)
require.NoError(t, err)

require.Equal(t, "1.0.0+1abc.345.whoop", version.SemVer)
require.Equal(t, "1.0.0+1abc.345.whoop", version.DotNet)
require.Equal(t, "v1.0.0+1abc.345.whoop", version.JavaScript)
require.Equal(t, "1.0.0+1abc.345.whoop", version.Python)
})
}

0 comments on commit a2c5d77

Please sign in to comment.