Filter out accessory from list artifact results (#17231)

Fixed #17145
1, Filter out the accessory from the artifact list.
2, Disable the display func of the accessory interface, currently this will not impact any kind of accessory, like signature and nydus. If we'd like to introduce it, it needs to resolve the pagiation issue of artifact list.

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2022-07-26 00:38:05 +08:00 committed by GitHub
parent 2fece8c9ea
commit 02eae9dede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 38 deletions

View File

@ -240,17 +240,7 @@ func (c *controller) ensureArtifact(ctx context.Context, repository, digest stri
}
func (c *controller) Count(ctx context.Context, query *q.Query) (int64, error) {
if query != nil {
// ignore the page number and size
query = &q.Query{
Keywords: query.Keywords,
}
}
arts, err := c.List(ctx, query, nil)
if err != nil {
return int64(0), err
}
return int64(len(arts)), nil
return c.artMgr.Count(ctx, query)
}
func (c *controller) List(ctx context.Context, query *q.Query, option *Option) ([]*Artifact, error) {
@ -260,15 +250,8 @@ func (c *controller) List(ctx context.Context, query *q.Query, option *Option) (
}
var res []*Artifact
// Only the displayed accessory will in the artifact list
for _, art := range arts {
accs, err := c.accessoryMgr.List(ctx, q.New(q.KeyWords{"ArtifactID": art.ID, "digest": art.Digest}))
if err != nil {
return nil, err
}
if len(accs) == 0 || (len(accs) > 0 && accs[0].Display()) {
res = append(res, c.assembleArtifact(ctx, art, option))
}
res = append(res, c.assembleArtifact(ctx, art, option))
}
return res, nil
}

View File

@ -40,8 +40,8 @@ import (
model_tag "github.com/goharbor/harbor/src/pkg/tag/model/tag"
tagtesting "github.com/goharbor/harbor/src/testing/controller/tag"
ormtesting "github.com/goharbor/harbor/src/testing/lib/orm"
accessorytesting "github.com/goharbor/harbor/src/testing/pkg/accessory"
"github.com/goharbor/harbor/src/testing/pkg/accessory"
accessorytesting "github.com/goharbor/harbor/src/testing/pkg/accessory"
arttesting "github.com/goharbor/harbor/src/testing/pkg/artifact"
artrashtesting "github.com/goharbor/harbor/src/testing/pkg/artifactrash"
"github.com/goharbor/harbor/src/testing/pkg/blob"
@ -264,26 +264,10 @@ func (c *controllerTestSuite) TestEnsure() {
}
func (c *controllerTestSuite) TestCount() {
c.artMgr.On("List", mock.Anything, mock.Anything).Return([]*artifact.Artifact{
{
ID: 1,
RepositoryID: 1,
},
}, nil)
acc := &basemodel.Default{
Data: accessorymodel.AccessoryData{
ID: 1,
ArtifactID: 2,
SubArtifactID: 1,
Type: accessorymodel.TypeCosignSignature,
},
}
c.accMgr.On("List", mock.Anything, mock.Anything).Return([]accessorymodel.Accessory{
acc,
}, nil)
c.artMgr.On("Count", mock.Anything, mock.Anything).Return(int64(1), nil)
total, err := c.ctl.Count(nil, nil)
c.Require().Nil(err)
c.Equal(int64(0), total)
c.Equal(int64(1), total)
}
func (c *controllerTestSuite) TestList() {

View File

@ -104,6 +104,7 @@ func (d *dao) List(ctx context.Context, query *q.Query) ([]*Artifact, error) {
if _, err = qs.All(&artifacts); err != nil {
return nil, err
}
return artifacts, nil
}
func (d *dao) Get(ctx context.Context, id int64) (*Artifact, error) {
@ -294,6 +295,10 @@ func querySetter(ctx context.Context, query *q.Query) (beegoorm.QuerySeter, erro
if err != nil {
return nil, err
}
qs, err = setAccessoryQuery(qs, query)
if err != nil {
return nil, err
}
return qs, nil
}
@ -405,3 +410,12 @@ func setLabelQuery(qs beegoorm.QuerySeter, query *q.Query) (beegoorm.QuerySeter,
qs = qs.FilterRaw("id", fmt.Sprintf(`IN (%s)`, strings.Join(collections, " INTERSECT ")))
return qs, nil
}
// filter out the accessory for results
func setAccessoryQuery(qs beegoorm.QuerySeter, query *q.Query) (beegoorm.QuerySeter, error) {
if query == nil {
return qs, nil
}
qs = qs.FilterRaw("id", "not in (select artifact_id from artifact_accessory)")
return qs, nil
}