fix replicate reference accessory (#16401)

fixes #16375

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2022-02-23 21:24:15 +08:00 committed by GitHub
parent 490fe4e5b3
commit 6ef4874011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,23 +55,13 @@ func (c *client) listArtifacts(repo string) ([]*model.Artifact, error) {
return nil, err
}
var arts []*model.Artifact
for _, artifact := range artifacts {
art := &model.Artifact{
Type: artifact.Type,
Digest: artifact.Digest,
}
for _, label := range artifact.Labels {
art.Labels = append(art.Labels, label.Name)
}
for _, tag := range artifact.Tags {
art.Tags = append(art.Tags, tag.Name)
}
arts = append(arts, art)
// For Harbor v2 clients, it has to append the accessory objects behind the subject artifact it has.
for _, acc := range artifact.Accessories {
art := &model.Artifact{
Type: artifact.Type,
// append the accessory objects behind the subject artifact if it has.
var getAccessoryArts = func(art *artifact.Artifact) ([]*model.Artifact, error) {
var accArts = []*model.Artifact{}
for _, acc := range art.Accessories {
accArt := &model.Artifact{
Type: art.Type,
Digest: acc.GetData().Digest,
}
tags, err := c.listTags(project, repo, acc.GetData().Digest)
@ -79,9 +69,47 @@ func (c *client) listArtifacts(repo string) ([]*model.Artifact, error) {
return nil, err
}
for _, tag := range tags {
art.Tags = append(art.Tags, tag)
accArt.Tags = append(accArt.Tags, tag)
}
arts = append(arts, art)
accArts = append(accArts, accArt)
}
return accArts, nil
}
for _, artItem := range artifacts {
art := &model.Artifact{
Type: artItem.Type,
Digest: artItem.Digest,
}
for _, label := range artItem.Labels {
art.Labels = append(art.Labels, label.Name)
}
for _, tag := range artItem.Tags {
art.Tags = append(art.Tags, tag.Name)
}
arts = append(arts, art)
// append the accessory of index or individual artifact
accArts, err := getAccessoryArts(artItem)
if err != nil {
return nil, err
}
arts = append(arts, accArts...)
// append the accessory of reference if it has
for _, ref := range artItem.References {
url := fmt.Sprintf("%s/projects/%s/repositories/%s/artifacts/%s?with_accessory=true",
c.BasePath(), project, repo, ref.ChildDigest)
artRef := artifact.Artifact{}
if err := c.C.Get(url, &artRef); err != nil {
return nil, err
}
accArts, err := getAccessoryArts(&artRef)
if err != nil {
return nil, err
}
arts = append(arts, accArts...)
}
}
return arts, nil