Skip to content

Commit

Permalink
update referrers api (#20068)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
wy65701436 committed Mar 1, 2024
1 parent 3782bab commit d25f355
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
14 changes: 1 addition & 13 deletions src/server/registry/referrers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions src/server/registry/referrers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package registry
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

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"
Expand Down Expand Up @@ -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,
Expand All @@ -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())
}
Expand Down

0 comments on commit d25f355

Please sign in to comment.