[GITEA] feat(nuget): basic manifest download
Refs: https://codeberg.org/forgejo/forgejo/pulls/2222 (cherry picked from commit 5f837efc15f3d1e0d7fbed7fc569251143266584) fix: write xml header (cherry picked from commit a715984a42be9da81c48106d5eae244098ac1108) fix: optional elements and xml schema (cherry picked from commit 6ea6895a3616246e7282aa20d8f010fa931b60ea) fix: pass all other requests to file search (cherry picked from commit 9bfc74833a3b657453b4519573598432a87e3e3c) test: add integration test (cherry picked from commit b798f4ce86daa78e694c5c142e6f5f44938e6cb6) fix: use xmlResponse (cherry picked from commit 7f76df0b246c64fac0eeb115642c8cb6eb676f36) (cherry picked from commit e18d574ca40905aec52fbbe8247ba83fd01874dd)
This commit is contained in:
parent
bfe87e30af
commit
97cc955101
|
@ -71,34 +71,50 @@ type Dependency struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type nuspecPackage struct {
|
type nuspecPackageType struct {
|
||||||
Metadata struct {
|
Name string `xml:"name,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nuspecPackageTypes struct {
|
||||||
|
PackageType []nuspecPackageType `xml:"packageType"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nuspecRepository struct {
|
||||||
|
URL string `xml:"url,attr,omitempty"`
|
||||||
|
Type string `xml:"type,attr,omitempty"`
|
||||||
|
}
|
||||||
|
type nuspecDependency struct {
|
||||||
|
ID string `xml:"id,attr"`
|
||||||
|
Version string `xml:"version,attr"`
|
||||||
|
Exclude string `xml:"exclude,attr,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nuspecGroup struct {
|
||||||
|
TargetFramework string `xml:"targetFramework,attr"`
|
||||||
|
Dependency []nuspecDependency `xml:"dependency"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nuspecDependencies struct {
|
||||||
|
Group []nuspecGroup `xml:"group"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nuspeceMetadata struct {
|
||||||
ID string `xml:"id"`
|
ID string `xml:"id"`
|
||||||
Version string `xml:"version"`
|
Version string `xml:"version"`
|
||||||
Authors string `xml:"authors"`
|
Authors string `xml:"authors"`
|
||||||
RequireLicenseAcceptance bool `xml:"requireLicenseAcceptance"`
|
RequireLicenseAcceptance bool `xml:"requireLicenseAcceptance,omitempty"`
|
||||||
ProjectURL string `xml:"projectUrl"`
|
ProjectURL string `xml:"projectUrl,omitempty"`
|
||||||
Description string `xml:"description"`
|
Description string `xml:"description"`
|
||||||
ReleaseNotes string `xml:"releaseNotes"`
|
ReleaseNotes string `xml:"releaseNotes,omitempty"`
|
||||||
PackageTypes struct {
|
PackageTypes *nuspecPackageTypes `xml:"packageTypes,omitempty"`
|
||||||
PackageType []struct {
|
Repository *nuspecRepository `xml:"repository,omitempty"`
|
||||||
Name string `xml:"name,attr"`
|
Dependencies *nuspecDependencies `xml:"dependencies,omitempty"`
|
||||||
} `xml:"packageType"`
|
}
|
||||||
} `xml:"packageTypes"`
|
|
||||||
Repository struct {
|
type nuspecPackage struct {
|
||||||
URL string `xml:"url,attr"`
|
XMLName xml.Name `xml:"package"`
|
||||||
} `xml:"repository"`
|
Xmlns string `xml:"xmlns,attr"`
|
||||||
Dependencies struct {
|
Metadata nuspeceMetadata `xml:"metadata"`
|
||||||
Group []struct {
|
|
||||||
TargetFramework string `xml:"targetFramework,attr"`
|
|
||||||
Dependency []struct {
|
|
||||||
ID string `xml:"id,attr"`
|
|
||||||
Version string `xml:"version,attr"`
|
|
||||||
Exclude string `xml:"exclude,attr"`
|
|
||||||
} `xml:"dependency"`
|
|
||||||
} `xml:"group"`
|
|
||||||
} `xml:"dependencies"`
|
|
||||||
} `xml:"metadata"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParsePackageMetaData parses the metadata of a Nuget package file
|
// ParsePackageMetaData parses the metadata of a Nuget package file
|
||||||
|
@ -149,23 +165,27 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
packageType := DependencyPackage
|
packageType := DependencyPackage
|
||||||
|
if p.Metadata.PackageTypes != nil {
|
||||||
for _, pt := range p.Metadata.PackageTypes.PackageType {
|
for _, pt := range p.Metadata.PackageTypes.PackageType {
|
||||||
if pt.Name == "SymbolsPackage" {
|
if pt.Name == "SymbolsPackage" {
|
||||||
packageType = SymbolsPackage
|
packageType = SymbolsPackage
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m := &Metadata{
|
m := &Metadata{
|
||||||
Description: p.Metadata.Description,
|
Description: p.Metadata.Description,
|
||||||
ReleaseNotes: p.Metadata.ReleaseNotes,
|
ReleaseNotes: p.Metadata.ReleaseNotes,
|
||||||
Authors: p.Metadata.Authors,
|
Authors: p.Metadata.Authors,
|
||||||
ProjectURL: p.Metadata.ProjectURL,
|
ProjectURL: p.Metadata.ProjectURL,
|
||||||
RepositoryURL: p.Metadata.Repository.URL,
|
|
||||||
RequireLicenseAcceptance: p.Metadata.RequireLicenseAcceptance,
|
RequireLicenseAcceptance: p.Metadata.RequireLicenseAcceptance,
|
||||||
Dependencies: make(map[string][]Dependency),
|
Dependencies: make(map[string][]Dependency),
|
||||||
}
|
}
|
||||||
|
if p.Metadata.Repository != nil {
|
||||||
|
m.RepositoryURL = p.Metadata.Repository.URL
|
||||||
|
}
|
||||||
|
if p.Metadata.Dependencies != nil {
|
||||||
for _, group := range p.Metadata.Dependencies.Group {
|
for _, group := range p.Metadata.Dependencies.Group {
|
||||||
deps := make([]Dependency, 0, len(group.Dependency))
|
deps := make([]Dependency, 0, len(group.Dependency))
|
||||||
for _, dep := range group.Dependency {
|
for _, dep := range group.Dependency {
|
||||||
|
@ -181,6 +201,7 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
|
||||||
m.Dependencies[group.TargetFramework] = deps
|
m.Dependencies[group.TargetFramework] = deps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return &Package{
|
return &Package{
|
||||||
PackageType: packageType,
|
PackageType: packageType,
|
||||||
ID: p.Metadata.ID,
|
ID: p.Metadata.ID,
|
||||||
|
@ -204,3 +225,51 @@ func toNormalizedVersion(v *version.Version) string {
|
||||||
}
|
}
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returning any here because we use a private type and we don't need the type for xml marshalling
|
||||||
|
func GenerateNuspec(pd *Package) any {
|
||||||
|
m := nuspeceMetadata{
|
||||||
|
ID: pd.ID,
|
||||||
|
Version: pd.Version,
|
||||||
|
Authors: pd.Metadata.Authors,
|
||||||
|
Description: pd.Metadata.Description,
|
||||||
|
ProjectURL: pd.Metadata.ProjectURL,
|
||||||
|
RequireLicenseAcceptance: pd.Metadata.RequireLicenseAcceptance,
|
||||||
|
}
|
||||||
|
|
||||||
|
if pd.Metadata.RepositoryURL != "" {
|
||||||
|
m.Repository = &nuspecRepository{
|
||||||
|
URL: pd.Metadata.RepositoryURL,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
groups := len(pd.Metadata.Dependencies)
|
||||||
|
if groups > 0 {
|
||||||
|
m.Dependencies = &nuspecDependencies{
|
||||||
|
Group: make([]nuspecGroup, 0, groups),
|
||||||
|
}
|
||||||
|
|
||||||
|
for tgf, deps := range pd.Metadata.Dependencies {
|
||||||
|
if len(deps) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
gDeps := make([]nuspecDependency, 0, len(deps))
|
||||||
|
for _, dep := range deps {
|
||||||
|
gDeps = append(gDeps, nuspecDependency{
|
||||||
|
ID: dep.ID,
|
||||||
|
Version: dep.Version,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Dependencies.Group = append(m.Dependencies.Group, nuspecGroup{
|
||||||
|
TargetFramework: tgf,
|
||||||
|
Dependency: gDeps,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &nuspecPackage{
|
||||||
|
Xmlns: "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd",
|
||||||
|
Metadata: m,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -387,12 +387,33 @@ func EnumeratePackageVersionsV3(ctx *context.Context) {
|
||||||
ctx.JSON(http.StatusOK, resp)
|
ctx.JSON(http.StatusOK, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-content-nupkg
|
// https://learn.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-manifest-nuspec
|
||||||
|
// https://learn.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-content-nupkg
|
||||||
func DownloadPackageFile(ctx *context.Context) {
|
func DownloadPackageFile(ctx *context.Context) {
|
||||||
packageName := ctx.Params("id")
|
packageName := ctx.Params("id")
|
||||||
packageVersion := ctx.Params("version")
|
packageVersion := ctx.Params("version")
|
||||||
filename := ctx.Params("filename")
|
filename := ctx.Params("filename")
|
||||||
|
|
||||||
|
if filename == fmt.Sprintf("%s.nuspec", packageName) {
|
||||||
|
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNuGet, packageName, packageVersion)
|
||||||
|
if err != nil {
|
||||||
|
apiError(ctx, http.StatusNotFound, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pd, err := packages_model.GetPackageDescriptor(ctx, pv)
|
||||||
|
if err != nil {
|
||||||
|
apiError(ctx, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pkg := &nuget_module.Package{
|
||||||
|
ID: pd.Package.Name,
|
||||||
|
Version: packageVersion,
|
||||||
|
Metadata: pd.Metadata.(*nuget_module.Metadata),
|
||||||
|
}
|
||||||
|
|
||||||
|
xmlResponse(ctx, http.StatusOK, nuget_module.GenerateNuspec(pkg))
|
||||||
|
} else {
|
||||||
s, u, pf, err := packages_service.GetFileStreamByPackageNameAndVersion(
|
s, u, pf, err := packages_service.GetFileStreamByPackageNameAndVersion(
|
||||||
ctx,
|
ctx,
|
||||||
&packages_service.PackageInfo{
|
&packages_service.PackageInfo{
|
||||||
|
@ -415,6 +436,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.ServePackageFile(ctx, s, u, pf)
|
helper.ServePackageFile(ctx, s, u, pf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage creates a new package with the metadata contained in the uploaded nupgk file
|
// UploadPackage creates a new package with the metadata contained in the uploaded nupgk file
|
||||||
|
|
|
@ -353,6 +353,21 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
|
||||||
|
|
||||||
assert.Equal(t, content, resp.Body.Bytes())
|
assert.Equal(t, content, resp.Body.Bytes())
|
||||||
|
|
||||||
|
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s.nuspec", url, packageName, packageVersion, packageName)).
|
||||||
|
AddBasicAuth(user.Name)
|
||||||
|
resp = MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
nuspec := `<?xml version="1.0" encoding="UTF-8"?>` + "\n" +
|
||||||
|
`<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"><metadata>` +
|
||||||
|
`<id>` + packageName + `</id><version>` + packageVersion + `</version><authors>` + packageAuthors + `</authors><description>` + packageDescription + `</description>` +
|
||||||
|
`<dependencies><group targetFramework=".NETStandard2.0">` +
|
||||||
|
// https://github.com/golang/go/issues/21399 go can't generate self-closing tags
|
||||||
|
`<dependency id="Microsoft.CSharp" version="4.5.0"></dependency>` +
|
||||||
|
`</group></dependencies>` +
|
||||||
|
`</metadata></package>`
|
||||||
|
|
||||||
|
assert.Equal(t, nuspec, resp.Body.String())
|
||||||
|
|
||||||
checkDownloadCount(1)
|
checkDownloadCount(1)
|
||||||
|
|
||||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s.%s.snupkg", url, packageName, packageVersion, packageName, packageVersion)).
|
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s.%s.snupkg", url, packageName, packageVersion, packageName, packageVersion)).
|
||||||
|
|
Loading…
Reference in a new issue