summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-12-21 04:20:23 +0100
committerGitHub <noreply@github.com>2022-12-20 22:20:23 -0500
commit86ace4b5c2cd6e02eae6814a81992c2591b6448d (patch)
treee68f970477c46a833f69f65a65ed180e726d18f5 /modules
parentfe6608f72bc2c71e30665126b7257650987d8c73 (diff)
downloadgitea-86ace4b5c2cd6e02eae6814a81992c2591b6448d.tar.gz
gitea-86ace4b5c2cd6e02eae6814a81992c2591b6448d.zip
Normalize NuGet package version on upload (#22186)
Fixes #22178 After this change upload versions with different semver metadata are treated as the same version and trigger a duplicated version error. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules')
-rw-r--r--modules/packages/nuget/metadata.go20
-rw-r--r--modules/packages/nuget/metadata_test.go13
2 files changed, 32 insertions, 1 deletions
diff --git a/modules/packages/nuget/metadata.go b/modules/packages/nuget/metadata.go
index dc72d0982a..033421af96 100644
--- a/modules/packages/nuget/metadata.go
+++ b/modules/packages/nuget/metadata.go
@@ -5,8 +5,10 @@ package nuget
import (
"archive/zip"
+ "bytes"
"encoding/xml"
"errors"
+ "fmt"
"io"
"path/filepath"
"regexp"
@@ -182,7 +184,23 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
return &Package{
PackageType: packageType,
ID: p.Metadata.ID,
- Version: v.String(),
+ Version: toNormalizedVersion(v),
Metadata: m,
}, nil
}
+
+// https://learn.microsoft.com/en-us/nuget/concepts/package-versioning#normalized-version-numbers
+// https://github.com/NuGet/NuGet.Client/blob/dccbd304b11103e08b97abf4cf4bcc1499d9235a/src/NuGet.Core/NuGet.Versioning/VersionFormatter.cs#L121
+func toNormalizedVersion(v *version.Version) string {
+ var buf bytes.Buffer
+ segments := v.Segments64()
+ fmt.Fprintf(&buf, "%d.%d.%d", segments[0], segments[1], segments[2])
+ if len(segments) > 3 && segments[3] > 0 {
+ fmt.Fprintf(&buf, ".%d", segments[3])
+ }
+ pre := v.Prerelease()
+ if pre != "" {
+ fmt.Fprint(&buf, "-", pre)
+ }
+ return buf.String()
+}
diff --git a/modules/packages/nuget/metadata_test.go b/modules/packages/nuget/metadata_test.go
index 74c3e7dfaa..bba2bff4a5 100644
--- a/modules/packages/nuget/metadata_test.go
+++ b/modules/packages/nuget/metadata_test.go
@@ -146,6 +146,19 @@ func TestParseNuspecMetaData(t *testing.T) {
assert.Len(t, deps, 1)
assert.Equal(t, dependencyID, deps[0].ID)
assert.Equal(t, dependencyVersion, deps[0].Version)
+
+ t.Run("NormalizedVersion", func(t *testing.T) {
+ np, err := ParseNuspecMetaData(strings.NewReader(`<?xml version="1.0" encoding="utf-8"?>
+<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
+ <metadata>
+ <id>test</id>
+ <version>1.04.5.2.5-rc.1+metadata</version>
+ </metadata>
+</package>`))
+ assert.NoError(t, err)
+ assert.NotNil(t, np)
+ assert.Equal(t, "1.4.5.2-rc.1", np.Version)
+ })
})
t.Run("Symbols Package", func(t *testing.T) {