Skip to content

Commit

Permalink
feat: repo and snapshots packages filter api add 'maximumVersion' que…
Browse files Browse the repository at this point in the history
…ry parameter support

example: `curl http://localhost:8080/api/repos/test/packages\?maximumVersion\=1`

Change-Id: Ie9ffd36146bf017bbb353737f32360f7b73d6b0a
  • Loading branch information
hudeng-go committed Aug 30, 2022
1 parent b3d9055 commit 2e7f501
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
}
}

// filter packages by version
if c.Request.URL.Query().Get("maximumVersion") == "1" {
list.PrepareIndex()
list.ForEach(func(p *deb.Package) error {
versionQ, err := query.Parse(fmt.Sprintf("Name (%s), $Version (<= %s)", p.Name, p.Version))
if err != nil {

Check warning on line 216 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L211-L216

Added lines #L211 - L216 were not covered by tests
fmt.Println("filter packages by version, query string parse err: ", err)
c.AbortWithError(500, fmt.Errorf("unable to parse %s maximum version query string: %s", p.Name, err))
} else {
tmpList, err := list.Filter([]deb.PackageQuery{versionQ}, false,
nil, 0, []string{})

if err == nil {
if tmpList.Len() > 0 {
tmpList.ForEach(func(tp *deb.Package) error {
list.Remove(tp)

Check warning on line 226 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L224-L226

Added lines #L224 - L226 were not covered by tests
return nil
})
list.Add(p)
}
} else {
fmt.Println("filter packages by version, filter err: ", err)
c.AbortWithError(500, fmt.Errorf("unable to get %s maximum version: %s", p.Name, err))
}
}

return nil
})
}

Check warning on line 240 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L233-L240

Added lines #L233 - L240 were not covered by tests
if c.Request.URL.Query().Get("format") == "details" {
list.ForEach(func(p *deb.Package) error {
result = append(result, p)
Expand Down
10 changes: 10 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
Expand Down Expand Up @@ -93,6 +94,15 @@ func (s *ApiSuite) TestGetVersion(c *C) {
c.Check(response.Body.String(), Matches, ".*Version.*")
}

func (s *ApiSuite) TestRepoCreate(c *C) {
body, err := json.Marshal(gin.H{
"Name": "dummy",
})
c.Assert(err, IsNil)
_, err = s.HTTPRequest("POST", "/api/repos", bytes.NewReader(body))
c.Assert(err, IsNil)
}

func (s *ApiSuite) TestTruthy(c *C) {
c.Check(truthy("no"), Equals, false)
c.Check(truthy("n"), Equals, false)
Expand Down
18 changes: 18 additions & 0 deletions api/packages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

import (
. "gopkg.in/check.v1"
)

type PackagesSuite struct {
ApiSuite
}

var _ = Suite(&PackagesSuite{})

func (s *PackagesSuite) TestPackagesGetMaximumVersion(c *C) {
response, err := s.HTTPRequest("GET", "/api/repos/dummy/packages?maximumVersion=1", nil)
c.Assert(err, IsNil)
c.Check(response.Code, Equals, 200)
c.Check(response.Body.String(), Equals, "[]")
}

0 comments on commit 2e7f501

Please sign in to comment.