update referrers api (#20068)

Update the referrers API according to the changeset in distribution spec 1.1, available at https://github.com/opencontainers/distribution-spec/pull/491/files.

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2024-03-01 15:25:38 +08:00 committed by GitHub
parent 3782bab80a
commit d25f3556a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 18 deletions

View File

@ -63,20 +63,8 @@ func (r *referrersHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
// Get the artifact by reference
art, err := r.artifactManager.GetByDigest(ctx, repository, reference)
if err != nil {
if errors.IsNotFoundErr(err) {
// If artifact not found, return empty json
newListReferrersOK().WithPayload(nil).WriteResponse(w)
return
}
lib_http.SendError(w, err)
return
}
// Query accessories with matching subject artifact digest
query := q.New(q.KeyWords{"SubjectArtifactDigest": art.Digest, "SubjectArtifactRepo": art.RepositoryName})
query := q.New(q.KeyWords{"SubjectArtifactDigest": reference, "SubjectArtifactRepo": repository})
total, err := r.accessoryManager.Count(ctx, query)
if err != nil {
lib_http.SendError(w, err)

View File

@ -3,7 +3,6 @@ package registry
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
@ -11,7 +10,6 @@ import (
beegocontext "github.com/beego/beego/v2/server/web/context"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/goharbor/harbor/src/lib/errors"
accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model"
basemodel "github.com/goharbor/harbor/src/pkg/accessory/model/base"
"github.com/goharbor/harbor/src/pkg/artifact"
@ -93,8 +91,10 @@ func TestReferrersHandlerEmpty(t *testing.T) {
artifactMock := &arttesting.Manager{}
accessoryMock := &accessorytesting.Manager{}
artifactMock.On("GetByDigest", mock.Anything, mock.Anything, mock.Anything).
Return(nil, errors.NotFoundError(nil))
accessoryMock.On("Count", mock.Anything, mock.Anything).
Return(int64(0), nil)
accessoryMock.On("List", mock.Anything, mock.Anything).
Return([]accessorymodel.Accessory{}, nil)
handler := &referrersHandler{
artifactManager: artifactMock,
@ -109,7 +109,6 @@ func TestReferrersHandlerEmpty(t *testing.T) {
}
index := &ocispec.Index{}
json.Unmarshal([]byte(rec.Body.String()), index)
fmt.Println(index)
if index.SchemaVersion != 0 && len(index.Manifests) != -0 {
t.Errorf("Expected empty response body, but got %s", rec.Body.String())
}