diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-12-21 21:50:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-21 14:50:17 -0600 |
commit | f7258aa42b6229e077be99ec63ee4993558a7004 (patch) | |
tree | 5d6fa0d409dbc2206060c91a7188048632c9e3ad | |
parent | 9a0a4086e27bf1bdbd19da5f173e683065145ea4 (diff) | |
download | gitea-f7258aa42b6229e077be99ec63ee4993558a7004.tar.gz gitea-f7258aa42b6229e077be99ec63ee4993558a7004.zip |
Normalize NuGet package version on upload (#22186) (#22200)
Backport of #22186
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
-rw-r--r-- | modules/packages/nuget/metadata.go | 20 | ||||
-rw-r--r-- | modules/packages/nuget/metadata_test.go | 13 | ||||
-rw-r--r-- | routers/api/packages/nuget/api_v2.go | 2 | ||||
-rw-r--r-- | routers/api/packages/nuget/api_v3.go | 22 |
4 files changed, 36 insertions, 21 deletions
diff --git a/modules/packages/nuget/metadata.go b/modules/packages/nuget/metadata.go index 2b555e47e9..98f5cac17a 100644 --- a/modules/packages/nuget/metadata.go +++ b/modules/packages/nuget/metadata.go @@ -6,8 +6,10 @@ package nuget import ( "archive/zip" + "bytes" "encoding/xml" "errors" + "fmt" "io" "path/filepath" "regexp" @@ -183,7 +185,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 e8c7773e97..2fb517504c 100644 --- a/modules/packages/nuget/metadata_test.go +++ b/modules/packages/nuget/metadata_test.go @@ -147,6 +147,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) { diff --git a/routers/api/packages/nuget/api_v2.go b/routers/api/packages/nuget/api_v2.go index 60a5d9c0e4..31ed7afb52 100644 --- a/routers/api/packages/nuget/api_v2.go +++ b/routers/api/packages/nuget/api_v2.go @@ -345,7 +345,7 @@ func createEntry(l *linkBuilder, pd *packages_model.PackageDescriptor, withNames Content: content, Properties: &FeedEntryProperties{ Version: pd.Version.Version, - NormalizedVersion: normalizeVersion(pd.SemVer), + NormalizedVersion: pd.Version.Version, Authors: metadata.Authors, Dependencies: buildDependencyString(metadata), Description: metadata.Description, diff --git a/routers/api/packages/nuget/api_v3.go b/routers/api/packages/nuget/api_v3.go index bb3e447bd6..62b4c75131 100644 --- a/routers/api/packages/nuget/api_v3.go +++ b/routers/api/packages/nuget/api_v3.go @@ -5,15 +5,11 @@ package nuget import ( - "bytes" - "fmt" "sort" "time" packages_model "code.gitea.io/gitea/models/packages" nuget_module "code.gitea.io/gitea/modules/packages/nuget" - - "github.com/hashicorp/go-version" ) // https://docs.microsoft.com/en-us/nuget/api/service-index#resources @@ -96,8 +92,8 @@ func createRegistrationIndexResponse(l *linkBuilder, pds []*packages_model.Packa { RegistrationPageURL: l.GetRegistrationIndexURL(pds[0].Package.Name), Count: len(pds), - Lower: normalizeVersion(pds[0].SemVer), - Upper: normalizeVersion(pds[len(pds)-1].SemVer), + Lower: pds[0].Version.Version, + Upper: pds[len(pds)-1].Version.Version, Items: items, }, }, @@ -174,7 +170,7 @@ type PackageVersionsResponse struct { func createPackageVersionsResponse(pds []*packages_model.PackageDescriptor) *PackageVersionsResponse { versions := make([]string, 0, len(pds)) for _, pd := range pds { - versions = append(versions, normalizeVersion(pd.SemVer)) + versions = append(versions, pd.Version.Version) } return &PackageVersionsResponse{ @@ -249,15 +245,3 @@ func createSearchResult(l *linkBuilder, pds []*packages_model.PackageDescriptor) RegistrationIndexURL: l.GetRegistrationIndexURL(latest.Package.Name), } } - -// normalizeVersion removes the metadata -func normalizeVersion(v *version.Version) string { - var buf bytes.Buffer - segments := v.Segments64() - fmt.Fprintf(&buf, "%d.%d.%d", segments[0], segments[1], segments[2]) - pre := v.Prerelease() - if pre != "" { - fmt.Fprintf(&buf, "-%s", pre) - } - return buf.String() -} |