mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	Add /$count endpoints for NuGet v2 (#22855)
				
					
				
			Fixes #22838 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		@@ -286,9 +286,18 @@ func CommonRoutes(ctx gocontext.Context) *web.Route {
 | 
				
			|||||||
				}, reqPackageAccess(perm.AccessModeWrite))
 | 
									}, reqPackageAccess(perm.AccessModeWrite))
 | 
				
			||||||
				r.Get("/symbols/{filename}/{guid:[0-9a-fA-F]{32}[fF]{8}}/{filename2}", nuget.DownloadSymbolFile)
 | 
									r.Get("/symbols/{filename}/{guid:[0-9a-fA-F]{32}[fF]{8}}/{filename2}", nuget.DownloadSymbolFile)
 | 
				
			||||||
				r.Get("/Packages(Id='{id:[^']+}',Version='{version:[^']+}')", nuget.RegistrationLeafV2)
 | 
									r.Get("/Packages(Id='{id:[^']+}',Version='{version:[^']+}')", nuget.RegistrationLeafV2)
 | 
				
			||||||
				r.Get("/Packages()", nuget.SearchServiceV2)
 | 
									r.Group("/Packages()", func() {
 | 
				
			||||||
				r.Get("/FindPackagesById()", nuget.EnumeratePackageVersionsV2)
 | 
										r.Get("", nuget.SearchServiceV2)
 | 
				
			||||||
				r.Get("/Search()", nuget.SearchServiceV2)
 | 
										r.Get("/$count", nuget.SearchServiceV2Count)
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									r.Group("/FindPackagesById()", func() {
 | 
				
			||||||
 | 
										r.Get("", nuget.EnumeratePackageVersionsV2)
 | 
				
			||||||
 | 
										r.Get("/$count", nuget.EnumeratePackageVersionsV2Count)
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									r.Group("/Search()", func() {
 | 
				
			||||||
 | 
										r.Get("", nuget.SearchServiceV2)
 | 
				
			||||||
 | 
										r.Get("/$count", nuget.SearchServiceV2Count)
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
			}, reqPackageAccess(perm.AccessModeRead))
 | 
								}, reqPackageAccess(perm.AccessModeRead))
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		r.Group("/npm", func() {
 | 
							r.Group("/npm", func() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"code.gitea.io/gitea/models/db"
 | 
						"code.gitea.io/gitea/models/db"
 | 
				
			||||||
@@ -94,8 +95,7 @@ func FeedCapabilityResource(ctx *context.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var searchTermExtract = regexp.MustCompile(`'([^']+)'`)
 | 
					var searchTermExtract = regexp.MustCompile(`'([^']+)'`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Protocol/LegacyFeed/V2FeedQueryBuilder.cs
 | 
					func getSearchTerm(ctx *context.Context) string {
 | 
				
			||||||
func SearchServiceV2(ctx *context.Context) {
 | 
					 | 
				
			||||||
	searchTerm := strings.Trim(ctx.FormTrim("searchTerm"), "'")
 | 
						searchTerm := strings.Trim(ctx.FormTrim("searchTerm"), "'")
 | 
				
			||||||
	if searchTerm == "" {
 | 
						if searchTerm == "" {
 | 
				
			||||||
		// $filter contains a query like:
 | 
							// $filter contains a query like:
 | 
				
			||||||
@@ -106,7 +106,11 @@ func SearchServiceV2(ctx *context.Context) {
 | 
				
			|||||||
			searchTerm = strings.TrimSpace(match[1])
 | 
								searchTerm = strings.TrimSpace(match[1])
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return searchTerm
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Protocol/LegacyFeed/V2FeedQueryBuilder.cs
 | 
				
			||||||
 | 
					func SearchServiceV2(ctx *context.Context) {
 | 
				
			||||||
	skip, take := ctx.FormInt("skip"), ctx.FormInt("take")
 | 
						skip, take := ctx.FormInt("skip"), ctx.FormInt("take")
 | 
				
			||||||
	if skip == 0 {
 | 
						if skip == 0 {
 | 
				
			||||||
		skip = ctx.FormInt("$skip")
 | 
							skip = ctx.FormInt("$skip")
 | 
				
			||||||
@@ -118,7 +122,9 @@ func SearchServiceV2(ctx *context.Context) {
 | 
				
			|||||||
	pvs, total, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
 | 
						pvs, total, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
 | 
				
			||||||
		OwnerID: ctx.Package.Owner.ID,
 | 
							OwnerID: ctx.Package.Owner.ID,
 | 
				
			||||||
		Type:    packages_model.TypeNuGet,
 | 
							Type:    packages_model.TypeNuGet,
 | 
				
			||||||
		Name:       packages_model.SearchValue{Value: searchTerm},
 | 
							Name: packages_model.SearchValue{
 | 
				
			||||||
 | 
								Value: getSearchTerm(ctx),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		IsInternal: util.OptionalBoolFalse,
 | 
							IsInternal: util.OptionalBoolFalse,
 | 
				
			||||||
		Paginator: db.NewAbsoluteListOptions(
 | 
							Paginator: db.NewAbsoluteListOptions(
 | 
				
			||||||
			skip,
 | 
								skip,
 | 
				
			||||||
@@ -145,6 +151,24 @@ func SearchServiceV2(ctx *context.Context) {
 | 
				
			|||||||
	xmlResponse(ctx, http.StatusOK, resp)
 | 
						xmlResponse(ctx, http.StatusOK, resp)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html#_Toc453752351
 | 
				
			||||||
 | 
					func SearchServiceV2Count(ctx *context.Context) {
 | 
				
			||||||
 | 
						count, err := packages_model.CountVersions(ctx, &packages_model.PackageSearchOptions{
 | 
				
			||||||
 | 
							OwnerID: ctx.Package.Owner.ID,
 | 
				
			||||||
 | 
							Type:    packages_model.TypeNuGet,
 | 
				
			||||||
 | 
							Name: packages_model.SearchValue{
 | 
				
			||||||
 | 
								Value: getSearchTerm(ctx),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							IsInternal: util.OptionalBoolFalse,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							apiError(ctx, http.StatusInternalServerError, err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx.PlainText(http.StatusOK, strconv.FormatInt(count, 10))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-for-packages
 | 
					// https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-for-packages
 | 
				
			||||||
func SearchServiceV3(ctx *context.Context) {
 | 
					func SearchServiceV3(ctx *context.Context) {
 | 
				
			||||||
	pvs, count, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
 | 
						pvs, count, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
 | 
				
			||||||
@@ -288,6 +312,25 @@ func EnumeratePackageVersionsV2(ctx *context.Context) {
 | 
				
			|||||||
	xmlResponse(ctx, http.StatusOK, resp)
 | 
						xmlResponse(ctx, http.StatusOK, resp)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html#_Toc453752351
 | 
				
			||||||
 | 
					func EnumeratePackageVersionsV2Count(ctx *context.Context) {
 | 
				
			||||||
 | 
						count, err := packages_model.CountVersions(ctx, &packages_model.PackageSearchOptions{
 | 
				
			||||||
 | 
							OwnerID: ctx.Package.Owner.ID,
 | 
				
			||||||
 | 
							Type:    packages_model.TypeNuGet,
 | 
				
			||||||
 | 
							Name: packages_model.SearchValue{
 | 
				
			||||||
 | 
								ExactMatch: true,
 | 
				
			||||||
 | 
								Value:      strings.Trim(ctx.FormTrim("id"), "'"),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							IsInternal: util.OptionalBoolFalse,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							apiError(ctx, http.StatusInternalServerError, err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx.PlainText(http.StatusOK, strconv.FormatInt(count, 10))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#enumerate-package-versions
 | 
					// https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#enumerate-package-versions
 | 
				
			||||||
func EnumeratePackageVersionsV3(ctx *context.Context) {
 | 
					func EnumeratePackageVersionsV3(ctx *context.Context) {
 | 
				
			||||||
	packageName := ctx.Params("id")
 | 
						packageName := ctx.Params("id")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,7 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/http/httptest"
 | 
						"net/http/httptest"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -109,8 +110,6 @@ func TestPackageNuGet(t *testing.T) {
 | 
				
			|||||||
	url := fmt.Sprintf("/api/packages/%s/nuget", user.Name)
 | 
						url := fmt.Sprintf("/api/packages/%s/nuget", user.Name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	t.Run("ServiceIndex", func(t *testing.T) {
 | 
						t.Run("ServiceIndex", func(t *testing.T) {
 | 
				
			||||||
		defer tests.PrintCurrentTest(t)()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		t.Run("v2", func(t *testing.T) {
 | 
							t.Run("v2", func(t *testing.T) {
 | 
				
			||||||
			defer tests.PrintCurrentTest(t)()
 | 
								defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -374,8 +373,6 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	t.Run("SearchService", func(t *testing.T) {
 | 
						t.Run("SearchService", func(t *testing.T) {
 | 
				
			||||||
		defer tests.PrintCurrentTest(t)()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		cases := []struct {
 | 
							cases := []struct {
 | 
				
			||||||
			Query           string
 | 
								Query           string
 | 
				
			||||||
			Skip            int
 | 
								Skip            int
 | 
				
			||||||
@@ -391,8 +388,6 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		t.Run("v2", func(t *testing.T) {
 | 
							t.Run("v2", func(t *testing.T) {
 | 
				
			||||||
			defer tests.PrintCurrentTest(t)()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			t.Run("Search()", func(t *testing.T) {
 | 
								t.Run("Search()", func(t *testing.T) {
 | 
				
			||||||
				defer tests.PrintCurrentTest(t)()
 | 
									defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -406,6 +401,12 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
					assert.Equal(t, c.ExpectedTotal, result.Count, "case %d: unexpected total hits", i)
 | 
										assert.Equal(t, c.ExpectedTotal, result.Count, "case %d: unexpected total hits", i)
 | 
				
			||||||
					assert.Len(t, result.Entries, c.ExpectedResults, "case %d: unexpected result count", i)
 | 
										assert.Len(t, result.Entries, c.ExpectedResults, "case %d: unexpected result count", i)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										req = NewRequest(t, "GET", fmt.Sprintf("%s/Search()/$count?searchTerm='%s'&skip=%d&take=%d", url, c.Query, c.Skip, c.Take))
 | 
				
			||||||
 | 
										req = AddBasicAuthHeader(req, user.Name)
 | 
				
			||||||
 | 
										resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										assert.Equal(t, strconv.FormatInt(c.ExpectedTotal, 10), resp.Body.String(), "case %d: unexpected total hits", i)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -413,7 +414,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
				defer tests.PrintCurrentTest(t)()
 | 
									defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				for i, c := range cases {
 | 
									for i, c := range cases {
 | 
				
			||||||
					req := NewRequest(t, "GET", fmt.Sprintf("%s/Search()?$filter=substringof('%s',tolower(Id))&$skip=%d&$top=%d", url, c.Query, c.Skip, c.Take))
 | 
										req := NewRequest(t, "GET", fmt.Sprintf("%s/Packages()?$filter=substringof('%s',tolower(Id))&$skip=%d&$top=%d", url, c.Query, c.Skip, c.Take))
 | 
				
			||||||
					req = AddBasicAuthHeader(req, user.Name)
 | 
										req = AddBasicAuthHeader(req, user.Name)
 | 
				
			||||||
					resp := MakeRequest(t, req, http.StatusOK)
 | 
										resp := MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -422,6 +423,12 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
					assert.Equal(t, c.ExpectedTotal, result.Count, "case %d: unexpected total hits", i)
 | 
										assert.Equal(t, c.ExpectedTotal, result.Count, "case %d: unexpected total hits", i)
 | 
				
			||||||
					assert.Len(t, result.Entries, c.ExpectedResults, "case %d: unexpected result count", i)
 | 
										assert.Len(t, result.Entries, c.ExpectedResults, "case %d: unexpected result count", i)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										req = NewRequest(t, "GET", fmt.Sprintf("%s/Packages()/$count?$filter=substringof('%s',tolower(Id))&$skip=%d&$top=%d", url, c.Query, c.Skip, c.Take))
 | 
				
			||||||
 | 
										req = AddBasicAuthHeader(req, user.Name)
 | 
				
			||||||
 | 
										resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										assert.Equal(t, strconv.FormatInt(c.ExpectedTotal, 10), resp.Body.String(), "case %d: unexpected total hits", i)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -512,8 +519,6 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		t.Run("RegistrationLeaf", func(t *testing.T) {
 | 
							t.Run("RegistrationLeaf", func(t *testing.T) {
 | 
				
			||||||
			defer tests.PrintCurrentTest(t)()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			t.Run("v2", func(t *testing.T) {
 | 
								t.Run("v2", func(t *testing.T) {
 | 
				
			||||||
				defer tests.PrintCurrentTest(t)()
 | 
									defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -549,8 +554,6 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	t.Run("PackageService", func(t *testing.T) {
 | 
						t.Run("PackageService", func(t *testing.T) {
 | 
				
			||||||
		defer tests.PrintCurrentTest(t)()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		t.Run("v2", func(t *testing.T) {
 | 
							t.Run("v2", func(t *testing.T) {
 | 
				
			||||||
			defer tests.PrintCurrentTest(t)()
 | 
								defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -563,6 +566,12 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			assert.Len(t, result.Entries, 1)
 | 
								assert.Len(t, result.Entries, 1)
 | 
				
			||||||
			assert.Equal(t, packageVersion, result.Entries[0].Properties.Version)
 | 
								assert.Equal(t, packageVersion, result.Entries[0].Properties.Version)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								req = NewRequest(t, "GET", fmt.Sprintf("%s/FindPackagesById()/$count?id='%s'", url, packageName))
 | 
				
			||||||
 | 
								req = AddBasicAuthHeader(req, user.Name)
 | 
				
			||||||
 | 
								resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								assert.Equal(t, "1", resp.Body.String())
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		t.Run("v3", func(t *testing.T) {
 | 
							t.Run("v3", func(t *testing.T) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user