Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement npm search api #1

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/content/doc/packages/npm.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ npm dist-tag add [email protected] release

The tag name must not be a valid version. All tag names which are parsable as a version are rejected.

## Search packages

The registry supports [searching](https://docs.npmjs.com/cli/v7/commands/npm-search/) but does not support special search qualifiers like `author:gitea`.

## Supported commands

```
Expand All @@ -136,4 +140,5 @@ npm publish
npm unpublish
npm dist-tag
npm view
npm search
```
10 changes: 7 additions & 3 deletions modules/packages/npm/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,24 @@ type PackageDistribution struct {

type PackageSearch struct {
Objects []*PackageSearchObject `json:"objects"`
Total int `json:"total"`
Time map[string]time.Time `json:"time,omitempty"`
Total int64 `json:"total"`
}

type PackageSearchObject struct {
Package *PackageSearchPackage `json:"package"`
}

type PackageSearchPackage struct {
Scope string `json:"scope"`
Name string `json:"name"`
Version string `json:"version"`
Date time.Time `json:"date"`
Description string `json:"description"`
Author User `json:"author"`
Publisher User `json:"publisher"`
Maintainers []User `json:"maintainers"`
Keywords []string `json:"keywords,omitempty"`
Links *PackageSearchPackageLinks `json:"links"`
Maintainers []User `json:"maintainers,omitempty"`
}

type PackageSearchPackageLinks struct {
Expand Down
48 changes: 27 additions & 21 deletions routers/api/packages/npm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,37 @@ func createPackageMetadataVersion(registryURL string, pd *packages_model.Package
}
}

func createPackageSearchResponse(registryURL string, pds []*packages_model.PackageDescriptor) *npm_module.PackageSearch {
versions := make([]*npm_module.PackageSearchObject, len(pds))
for i := range pds {
pd := pds[i]
metadata := createPackageMetadataVersion(registryURL, pd)

pkg := &npm_module.PackageSearchPackage{
Name: pd.Package.Name,
Version: pd.Version.Version,
Description: metadata.Description,
Links: &npm_module.PackageSearchPackageLinks{
Registry: registryURL,
Homepage: metadata.Homepage,
Repository: metadata.Repository.URL,
},
Maintainers: metadata.Maintainers,
}
func createPackageSearchResponse(pds []*packages_model.PackageDescriptor, total int64) *npm_module.PackageSearch {
objects := make([]*npm_module.PackageSearchObject, 0, len(pds))
for _, pd := range pds {
metadata := pd.Metadata.(*npm_module.Metadata)

versions[i] = &npm_module.PackageSearchObject{
Package: pkg,
scope := metadata.Scope
if scope == "" {
scope = "unscoped"
}

objects = append(objects, &npm_module.PackageSearchObject{
Package: &npm_module.PackageSearchPackage{
Scope: scope,
Name: metadata.Name,
Version: pd.Version.Version,
Date: pd.Version.CreatedUnix.AsLocalTime(),
Description: metadata.Description,
Author: npm_module.User{Name: metadata.Author},
Publisher: npm_module.User{Name: pd.Owner.Name},
Maintainers: []npm_module.User{}, // npm cli needs this field
Keywords: metadata.Keywords,
Links: &npm_module.PackageSearchPackageLinks{
Registry: pd.FullWebLink(),
Homepage: metadata.ProjectURL,
},
},
})
}

return &npm_module.PackageSearch{
Objects: versions,
Total: len(versions),
Objects: objects,
Total: total,
}
}
10 changes: 7 additions & 3 deletions routers/api/packages/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,17 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo
}

func PackageSearch(ctx *context.Context) {
pvs, _, err := packages_model.SearchLatestVersions(ctx, &packages_model.PackageSearchOptions{
pvs, total, err := packages_model.SearchLatestVersions(ctx, &packages_model.PackageSearchOptions{
OwnerID: ctx.Package.Owner.ID,
Type: packages_model.TypeNpm,
Name: packages_model.SearchValue{
ExactMatch: false,
Value: ctx.Req.URL.Query().Get("text"),
Value: ctx.FormTrim("text"),
},
Paginator: db.NewAbsoluteListOptions(
ctx.FormInt("from"),
ctx.FormInt("size"),
),
})
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
Expand All @@ -372,8 +376,8 @@ func PackageSearch(ctx *context.Context) {
}

resp := createPackageSearchResponse(
setting.AppURL+"api/packages/"+ctx.Package.Owner.Name+"/npm",
pds,
total,
)

ctx.JSON(http.StatusOK, resp)
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/api_packages_npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,37 @@ func TestPackageNpm(t *testing.T) {
test(t, http.StatusOK, packageTag2)
})

t.Run("Search", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

url := fmt.Sprintf("/api/packages/%s/npm/-/v1/search", user.Name)

cases := []struct {
Query string
Skip int
Take int
ExpectedTotal int64
ExpectedResults int
}{
{"", 0, 0, 1, 1},
{"", 0, 10, 1, 1},
{"gitea", 0, 10, 0, 0},
{"test", 0, 10, 1, 1},
{"test", 1, 10, 1, 0},
}

for i, c := range cases {
req := NewRequest(t, "GET", fmt.Sprintf("%s?text=%s&from=%d&size=%d", url, c.Query, c.Skip, c.Take))
resp := MakeRequest(t, req, http.StatusOK)

var result npm.PackageSearch
DecodeJSON(t, resp, &result)

assert.Equal(t, c.ExpectedTotal, result.Total, "case %d: unexpected total hits", i)
assert.Len(t, result.Objects, c.ExpectedResults, "case %d: unexpected result count", i)
}
})

t.Run("Delete", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

Expand Down